From 003acd35ed8c0dbf2a4a4d23b1073d7e1dd735c0 Mon Sep 17 00:00:00 2001 From: "M.Goldshteyn" Date: Fri, 6 Sep 2024 09:43:36 +0300 Subject: [PATCH] backup name --- create_db.sh.template | 2 +- migration.sh | 33 +++++++++++++-------------------- 2 files changed, 14 insertions(+), 21 deletions(-) diff --git a/create_db.sh.template b/create_db.sh.template index 8d21803..0184268 100644 --- a/create_db.sh.template +++ b/create_db.sh.template @@ -11,4 +11,4 @@ su - postgres -c "PGPASSWORD=\"DESTINATION_POSTGRES_PASSWORD\" psql -U postgres set "PGPASSWORD=\"DESTINATION_POSTGRES_PASSWORD\"" -su - postgres -c "pg_restore -j 2 -d DESTINATION_DB_NAME Backup_name" \ No newline at end of file +su - postgres -c "pg_restore -j 2 -d DESTINATION_DB_NAME DB_BACKUP" \ No newline at end of file diff --git a/migration.sh b/migration.sh index cdef31d..ecbe2b2 100644 --- a/migration.sh +++ b/migration.sh @@ -1,5 +1,11 @@ #!/bin/bash -#apt update && apt install sshpass -y + + +if [ $(dpkg-query -W -f='${Status}' sshpass 2>/dev/null | grep -c "ok installed") -eq 0 ]; +then + apt update && apt-get install sshpass; +fi + set -a # export variables from config.txt test source config.txt set +a # stop exporting @@ -11,16 +17,16 @@ echo $DATE systemctl is-active --quiet platform5.service && systemctl stop platform5 systemctl is-active --quiet platform6.service && systemctl stop platform6 -Backup_name=$SOURCE_DB_NAME.migration.$DATE.sql +DB_BACKUP=$SOURCE_DB_NAME.migration.$DATE.sql echo "backup name:" $Backup_name db_backup() { echo EVENT_STORE=$EVENT_STORE if [[ "$EVENT_STORE" == 0 ]]; then echo Making backup without event_store - PGPASSWORD="$SOURCE_DB_PASSWORD" pg_dump -Fc -T event_store -U postgres $SOURCE_DB_NAME > /var/backups/$SOURCE_DB_NAME.migration.$DATE.sql + PGPASSWORD="$SOURCE_DB_PASSWORD" pg_dump -Fc -T event_store -U postgres $SOURCE_DB_NAME > /var/backups/$DB_BACKUP elif [[ "$EVENT_STORE" == 1 ]]; then echo Making backup with event_store - PGPASSWORD="$SOURCE_DB_PASSWORD" pg_dump -Fc -U postgres $SOURCE_DB_NAME > /var/backups/$SOURCE_DB_NAME.migration.$DATE.sql + PGPASSWORD="$SOURCE_DB_PASSWORD" pg_dump -Fc -U postgres $SOURCE_DB_NAME > /var/backups/$DB_BACKUP elif [[ "$EVENT_STORE" == 2 ]]; then echo "don't make db backup" else echo "EVENT_STORE value must be 0 or 1 or 2" && exit @@ -30,7 +36,7 @@ send_backup() { if [[ "$EVENT_STORE" == 0 ]] or [[ "$EVENT_STORE" == 1 ]]; then echo "send dump to the destination" echo Sending db backup to destination server - cd /var/backups/ && yes | sshpass -p "$DESTINATION_DATABASE_PASSWORD" scp -P $DESTINATION_DATABASE_PORT -o StrictHostKeyChecking=no $SOURCE_DB_NAME.migration.$DATE.sql $DESTINATION_DATABASE_USER@$DESTINATION_DATABASE_IP:~/ + cd /var/backups/ && yes | sshpass -p "$DESTINATION_DATABASE_PASSWORD" scp -P $DESTINATION_DATABASE_PORT -o StrictHostKeyChecking=no DB_BACKUP $DESTINATION_DATABASE_USER@$DESTINATION_DATABASE_IP:~/ cd - else echo "no backup, nothing to send" && exit fi @@ -38,10 +44,11 @@ send_backup() { create_db() { sed -e "s/DESTINATION_DB_NAME/${DESTINATION_DB_NAME}/" \ -e "s/DESTINATION_POSTGRES_PASSWORD/${DESTINATION_POSTGRES_PASSWORD}/"\ + -e "s/DB_BACKUP/${$DB_BACKUP}/"\ create_db.sh.template > create_db.sh - sshpass -p "$DESTINATION_DATABASE_PASSWORD" ssh -p $DESTINATION_DATABASE_PORT $DESTINATION_DATABASE_USER@$DESTINATION_DATABASE_IP 'bash -s' < create_db.sh } + #restore_db(){ # if [[ "$EVENT_STORE" == 0 ]] or [[ "$EVENT_STORE" == 1 ]]; then # fi @@ -81,19 +88,5 @@ fonts_rsync(){ fi - -#Создание БД на локалке -sed -e "s/DESTINATION_DB_NAME/${DESTINATION_DB_NAME}/" \ - -e "s/DESTINATION_POSTGRES_PASSWORD/${DESTINATION_POSTGRES_PASSWORD}/"\ - create_db.sh.template > create_db.sh - -sshpass -p "$DESTINATION_DATABASE_PASSWORD" ssh -p $DESTINATION_DATABASE_PORT $DESTINATION_DATABASE_USER@$DESTINATION_DATABASE_IP 'bash -s' < create_db.sh - - - -#evt_full_rsync -#evt_common_files_rsync -#fonts_rsync - systemctl is-active --quiet platform5.service && systemctl start platform5 systemctl is-active --quiet platform6.service && systemctl start platform6 \ No newline at end of file