From 5fa1fe7b843aec499188bd5e25816c962d3f9999 Mon Sep 17 00:00:00 2001 From: Rustam Tagaev Date: Wed, 16 Oct 2024 16:08:07 +0300 Subject: [PATCH] feature/DO-1037/add_scripts_for_postgres (!7) Reviewed-on: https://git.avroid.tech/DevOps/avroid_scripts/pulls/7 --- backup_scripts/postgres/pre_backup.sh | 8 ----- backup_scripts/postgres/pre_backup_docker.sh | 29 +++++++++++++++++++ .../postgres/pre_backup_for_backup_test.sh | 7 +++++ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 backup_scripts/postgres/pre_backup_docker.sh diff --git a/backup_scripts/postgres/pre_backup.sh b/backup_scripts/postgres/pre_backup.sh index 45cca88..ab11ebd 100644 --- a/backup_scripts/postgres/pre_backup.sh +++ b/backup_scripts/postgres/pre_backup.sh @@ -3,7 +3,6 @@ DATE=$(date +%F) DIRBACKUP="/data/backups" DBUSER="postgres" -TEST_BACKUP_SERVER="h-license-manager-backup-server.avroid.cloud" get_leader() { curl -s 127.0.0.1:8008 | jq -j ".role" @@ -29,20 +28,13 @@ createBackup() { bzip2 -9 "${DIRBACKUP}/${db}-${DATE}".sql - copyBackupToTestServer "${DIRBACKUP}/${db}-${DATE}".sql.bz2 ${TEST_BACKUP_SERVER} - echo "END database bzip2: ${db}" done pg_dumpall -p 15432 -U postgres --roles-only > "${DIRBACKUP}/roles-${DATE}".sql - copyBackupToTestServer "${DIRBACKUP}/roles-${DATE}".sql ${TEST_BACKUP_SERVER} } -copyBackupToTestServer() { - rsync $1 $2::backup || true -} [[ $(ls -A ${DIRBACKUP}) ]] 2>/dev/null && echo "dir ${DIRBACKUP} not empty" && exit 123 createBackup -copyBackupToTestServer \ No newline at end of file diff --git a/backup_scripts/postgres/pre_backup_docker.sh b/backup_scripts/postgres/pre_backup_docker.sh new file mode 100644 index 0000000..ab2546a --- /dev/null +++ b/backup_scripts/postgres/pre_backup_docker.sh @@ -0,0 +1,29 @@ +#!/bin/bash + +DATE=$(date +%F) +DIRBACKUP="/data/backups" +DBUSER="postgres" + +getDbName() { + docker exec postgresql bash -c "psql -lA -x -U postgres -p 5432 -d postgres | \ + grep Name | grep -v -P 'template0|template1' | cut -f 2 -d '|'" +} + +DBLIST=$(getDbName) + +createBackup() { + for db in $DBLIST; do + echo "====================================" + echo "START create backup database: ${db}" + + docker exec postgresql bash -c "pg_dump -Fc -U ${DBUSER} -p 5432 -d ${db}" > "${DIRBACKUP}/${db}-${DATE}".sql + + bzip2 -9 "${DIRBACKUP}/${db}-${DATE}".sql + + echo "END database bzip2: ${db}" + done +} + +[[ $(ls -A ${DIRBACKUP}) ]] 2>/dev/null && echo "dir ${DIRBACKUP} not empty" && exit 123 + +createBackup diff --git a/backup_scripts/postgres/pre_backup_for_backup_test.sh b/backup_scripts/postgres/pre_backup_for_backup_test.sh index cd3ce4d..bedf4d7 100644 --- a/backup_scripts/postgres/pre_backup_for_backup_test.sh +++ b/backup_scripts/postgres/pre_backup_for_backup_test.sh @@ -3,6 +3,7 @@ DATE=$(date +%F) DIRBACKUP="/data/backups" DBUSER="postgres" +TEST_BACKUP_SERVER="h-license-manager-backup-server.avroid.cloud" get_leader() { curl -s 127.0.0.1:8008 | jq -j ".role" @@ -28,12 +29,18 @@ createBackup() { bzip2 -9 "${DIRBACKUP}/${db}-${DATE}".sql + copyBackupToTestServer "${DIRBACKUP}/${db}-${DATE}".sql.bz2 ${TEST_BACKUP_SERVER} + echo "END database bzip2: ${db}" done pg_dumpall -p 15432 -U postgres --roles-only > "${DIRBACKUP}/roles-${DATE}".sql + copyBackupToTestServer "${DIRBACKUP}/roles-${DATE}".sql ${TEST_BACKUP_SERVER} } +copyBackupToTestServer() { + rsync $1 $2::backup || true +} [[ $(ls -A ${DIRBACKUP}) ]] 2>/dev/null && echo "dir ${DIRBACKUP} not empty" && exit 123