From 643e0a34fb0014db4bb442ba7c2c1928edd555f5 Mon Sep 17 00:00:00 2001 From: simon Date: Sat, 3 Dec 2022 09:27:25 +0700 Subject: [PATCH] add db helper scripts --- deploy.sh | 2 ++ helper_scripts/db_backup.sh | 34 ++++++++++++++++++++++++++++++++++ helper_scripts/db_sync.sh | 32 ++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100755 helper_scripts/db_backup.sh create mode 100755 helper_scripts/db_sync.sh diff --git a/deploy.sh b/deploy.sh index 121bb53..7e85266 100755 --- a/deploy.sh +++ b/deploy.sh @@ -9,6 +9,7 @@ function rebuild_test { rsync -a --progress --delete docker-compose_testing.yml $test_host:docker/docker-compose.yml rsync -a --progress --delete tubearchivist $test_host:docker rsync -a --progress --delete env $test_host:docker + rsync -a --progress --delete helper_scripts $test_host: rsync -a --progress --delete builder/ $test_host:builder ssh "$test_host" "mkdir -p builder/clone" ssh "$test_host" 'docker compose -f docker/docker-compose.yml up -d --build' @@ -20,6 +21,7 @@ function docker_publish { rsync -a --progress --delete docker-compose_production.yml $public_host:docker/docker-compose.yml rsync -a --progress --delete tubearchivist $public_host:docker rsync -a --progress --delete env $public_host:docker + rsync -a --progress --delete helper_scripts $public_host: rsync -a --progress --delete builder/ $public_host:builder ssh "$public_host" "mkdir -p builder/clone" diff --git a/helper_scripts/db_backup.sh b/helper_scripts/db_backup.sh new file mode 100755 index 0000000..223760c --- /dev/null +++ b/helper_scripts/db_backup.sh @@ -0,0 +1,34 @@ +#!/bin/bash +# postgres backup and log management + +time_stamp=$(date '+%Y%m%d') +time_date=$(date '+%Y%m%d_%H%M%S') +backup_dir="$HOME/backup" + +# backup +docker exec postgres pg_dump -U archivist | gzip > "$backup_dir/pg_$time_stamp.gz" + +# rotate +find "$backup_dir" -type f -name "pg_*.gz" | sort -r | tail -n +7 | xargs --no-run-if-empty trash + +# log +log_file="$backup_dir/pg_status.log" + +query1="SELECT schemaname AS table_schema, \ + relname AS table_name, \ + pg_size_pretty(pg_relation_size(relid)) AS data_size \ +FROM pg_catalog.pg_statio_user_tables \ +ORDER BY pg_relation_size(relid) DESC;" + +query2="SELECT schemaname,relname,n_live_tup \ + FROM pg_stat_user_tables \ + ORDER BY n_live_tup DESC;" + +echo "postgres dump run at $time_date" > "$log_file" +echo "table size" >> "$log_file" +docker exec postgres psql -U archivist -c "$query1" >> "$log_file" +echo "row count" >> "$log_file" +docker exec postgres psql -U archivist -c "$query2" >> "$log_file" + +## +exit 0 diff --git a/helper_scripts/db_sync.sh b/helper_scripts/db_sync.sh new file mode 100755 index 0000000..cf911a9 --- /dev/null +++ b/helper_scripts/db_sync.sh @@ -0,0 +1,32 @@ +#!/bin/bash +# sync production db to local testing vm + +remote_host="vps3" +local_host="tubearchivist-website.local" + +echo "------------------------------------------------------------" +echo "sync db from $remote_host to $local_host" +echo "------------------------------------------------------------" + +# download +printf "\n -> backup\n" +ssh $remote_host 'docker exec postgres pg_dump -U archivist | gzip > backup.gz' +printf "\n -> download\n" +rsync --progress -r --delete-after -e ssh $remote_host:backup.gz /tmp/backup.gz + +# sync +printf "\n -> sync\n" +rsync --progress -r --delete-after /tmp/backup.gz -e ssh $local_host:backup +ssh $local_host 'gzip -df backup/backup.gz' + +# replace +printf "\n -> replace\n" +ssh $local_host "docker exec -i postgres psql -U archivist -c 'DROP TABLE IF EXISTS ta_docker_stats;'" +ssh $local_host "docker exec -i postgres psql -U archivist -c 'DROP TABLE IF EXISTS ta_release;'" +ssh $local_host "docker exec -i postgres psql -U archivist -c 'DROP TABLE IF EXISTS ta_roadmap;'" +ssh $local_host 'docker exec -i postgres psql -U archivist -d archivist < backup/backup' +ssh $local_host "trash backup/backup" +printf "\n -> done\n" + +## +exit 0