From c0506f26e09eb2bf334b9b1b0cfc3c301ae2ece0 Mon Sep 17 00:00:00 2001 From: "M.Goldshteyn" Date: Fri, 6 Sep 2024 09:14:15 +0300 Subject: [PATCH] restore --- config.txt | 5 ++--- create_db.sh | 3 ++- create_db.sh.template | 3 +++ migration.sh | 45 ++++++++++++++++++++++++++++++------------- task.txt | 8 ++++---- 5 files changed, 43 insertions(+), 21 deletions(-) diff --git a/config.txt b/config.txt index 0c308d6..ee8e597 100644 --- a/config.txt +++ b/config.txt @@ -23,12 +23,11 @@ SOURCE_DB_PASSWORD=anna5kanna #Имя БД на исходном сервере SOURCE_DB_NAME=mynewdb -#Имя БД на локальном сервере -DESTINATION_DB_NAME=locality409 - #Номер evt на исходном сервере SOURCE_EVT=evt4 +#Имя БД на локалке. Будет создана БД с таким именем +DESTINATION_DB_NAME=locality409 ############################################### diff --git a/create_db.sh b/create_db.sh index 7403739..c7570be 100644 --- a/create_db.sh +++ b/create_db.sh @@ -7,4 +7,5 @@ su - postgres -c "PGPASSWORD=\"anna5kanna\" psql -U postgres -c \"create databa LC_COLLATE = 'ru_RU.UTF-8' LC_CTYPE = 'ru_RU.UTF-8' TEMPLATE = template0 - CONNECTION LIMIT = -1;\"" \ No newline at end of file + CONNECTION LIMIT = -1;\"" + diff --git a/create_db.sh.template b/create_db.sh.template index 5e099ff..8d21803 100644 --- a/create_db.sh.template +++ b/create_db.sh.template @@ -9,3 +9,6 @@ su - postgres -c "PGPASSWORD=\"DESTINATION_POSTGRES_PASSWORD\" psql -U postgres TEMPLATE = template0 CONNECTION LIMIT = -1;\"" + +set "PGPASSWORD=\"DESTINATION_POSTGRES_PASSWORD\"" +su - postgres -c "pg_restore -j 2 -d DESTINATION_DB_NAME Backup_name" \ No newline at end of file diff --git a/migration.sh b/migration.sh index 1a5c694..cdef31d 100644 --- a/migration.sh +++ b/migration.sh @@ -11,29 +11,45 @@ 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 +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 - 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 - elif [[ "$EVENT_STORE" == 2 ]]; then - echo "Не делаем бэкап" - else echo "EVENT_STORE value must be 0 or 1" && exit - fi + 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 + 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 + elif [[ "$EVENT_STORE" == 2 ]]; then + echo "don't make db backup" + else echo "EVENT_STORE value must be 0 or 1 or 2" && exit + fi } -send_backup(){ +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 - else echo "no backup, nothing to send" && exit + fi +} +create_db() { + 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 } +#restore_db(){ +# if [[ "$EVENT_STORE" == 0 ]] or [[ "$EVENT_STORE" == 1 ]]; then +# fi +#} db_backup send_backup +create_db +#restore_db evt_full_rsync(){ echo "send $SOURCE_EVT to the destination" @@ -61,7 +77,7 @@ fonts_rsync(){ elif [[ "$EVT_COMMON" == 2 ]]; then echo "Не скачиваю ничего из storage" evt_full_rsync - else echo "EVENT_COMMON value must be 0 or 1 o 2" && exit + else echo "EVENT_COMMON value must be 0 or 1 or 2" && exit fi @@ -78,3 +94,6 @@ sshpass -p "$DESTINATION_DATABASE_PASSWORD" ssh -p $DESTINATION_DATABASE_PORT $D #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 diff --git a/task.txt b/task.txt index 4a0da13..4ede29b 100644 --- a/task.txt +++ b/task.txt @@ -1,11 +1,11 @@ -1. Стоп сервера -2. Снятие дампа (с евент стором/без евент стора) ++1. Стоп сервера ++2. Снятие дампа (с евент стором/без евент стора) -5. Отправка его на удаленный сервер/стянуть с удаленного сервера -6. Создание базы ++5. Отправка его на удаленный сервер ++6. Создание базы 7. Вотсановление дампа