add db helper scripts

This commit is contained in:
simon 2022-12-03 09:27:25 +07:00
parent 69d40f4124
commit 643e0a34fb
No known key found for this signature in database
GPG Key ID: 2C15AA5E89985DD4
3 changed files with 68 additions and 0 deletions

View File

@ -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"

34
helper_scripts/db_backup.sh Executable file
View File

@ -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

32
helper_scripts/db_sync.sh Executable file
View File

@ -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