Browse Source

restore

master
M.Goldshteyn 1 year ago
parent
commit
c0506f26e0
  1. 5
      config.txt
  2. 1
      create_db.sh
  3. 3
      create_db.sh.template
  4. 27
      migration.sh
  5. 8
      task.txt

5
config.txt

@ -23,12 +23,11 @@ SOURCE_DB_PASSWORD=anna5kanna
#Имя БД на исходном сервере #Имя БД на исходном сервере
SOURCE_DB_NAME=mynewdb SOURCE_DB_NAME=mynewdb
#Имя БД на локальном сервере
DESTINATION_DB_NAME=locality409
#Номер evt на исходном сервере #Номер evt на исходном сервере
SOURCE_EVT=evt4 SOURCE_EVT=evt4
#Имя БД на локалке. Будет создана БД с таким именем
DESTINATION_DB_NAME=locality409
############################################### ###############################################

1
create_db.sh

@ -8,3 +8,4 @@ su - postgres -c "PGPASSWORD=\"anna5kanna\" psql -U postgres -c \"create databa
LC_CTYPE = 'ru_RU.UTF-8' LC_CTYPE = 'ru_RU.UTF-8'
TEMPLATE = template0 TEMPLATE = template0
CONNECTION LIMIT = -1;\"" CONNECTION LIMIT = -1;\""

3
create_db.sh.template

@ -9,3 +9,6 @@ su - postgres -c "PGPASSWORD=\"DESTINATION_POSTGRES_PASSWORD\" psql -U postgres
TEMPLATE = template0 TEMPLATE = template0
CONNECTION LIMIT = -1;\"" CONNECTION LIMIT = -1;\""
set "PGPASSWORD=\"DESTINATION_POSTGRES_PASSWORD\""
su - postgres -c "pg_restore -j 2 -d DESTINATION_DB_NAME Backup_name"

27
migration.sh

@ -11,6 +11,8 @@ echo $DATE
systemctl is-active --quiet platform5.service && systemctl stop platform5 systemctl is-active --quiet platform5.service && systemctl stop platform5
systemctl is-active --quiet platform6.service && systemctl stop platform6 systemctl is-active --quiet platform6.service && systemctl stop platform6
Backup_name=$SOURCE_DB_NAME.migration.$DATE.sql
echo "backup name:" $Backup_name
db_backup() { db_backup() {
echo EVENT_STORE=$EVENT_STORE echo EVENT_STORE=$EVENT_STORE
if [[ "$EVENT_STORE" == 0 ]]; then if [[ "$EVENT_STORE" == 0 ]]; then
@ -20,20 +22,34 @@ db_backup() {
echo Making backup with event_store 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/$SOURCE_DB_NAME.migration.$DATE.sql
elif [[ "$EVENT_STORE" == 2 ]]; then elif [[ "$EVENT_STORE" == 2 ]]; then
echo "Не делаем бэкап" echo "don't make db backup"
else echo "EVENT_STORE value must be 0 or 1" && exit else echo "EVENT_STORE value must be 0 or 1 or 2" && exit
fi fi
} }
send_backup(){ send_backup() {
if [[ "$EVENT_STORE" == 0 ]] or [[ "$EVENT_STORE" == 1 ]]; then if [[ "$EVENT_STORE" == 0 ]] or [[ "$EVENT_STORE" == 1 ]]; then
echo "send dump to the destination" echo "send dump to the destination"
echo Sending db backup to destination server 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 $SOURCE_DB_NAME.migration.$DATE.sql $DESTINATION_DATABASE_USER@$DESTINATION_DATABASE_IP:~/
cd - cd -
else echo "no backup, nothing to send" && exit 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 db_backup
send_backup send_backup
create_db
#restore_db
evt_full_rsync(){ evt_full_rsync(){
echo "send $SOURCE_EVT to the destination" echo "send $SOURCE_EVT to the destination"
@ -61,7 +77,7 @@ fonts_rsync(){
elif [[ "$EVT_COMMON" == 2 ]]; then elif [[ "$EVT_COMMON" == 2 ]]; then
echo "Не скачиваю ничего из storage" echo "Не скачиваю ничего из storage"
evt_full_rsync 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 fi
@ -78,3 +94,6 @@ sshpass -p "$DESTINATION_DATABASE_PASSWORD" ssh -p $DESTINATION_DATABASE_PORT $D
#evt_full_rsync #evt_full_rsync
#evt_common_files_rsync #evt_common_files_rsync
#fonts_rsync #fonts_rsync
systemctl is-active --quiet platform5.service && systemctl start platform5
systemctl is-active --quiet platform6.service && systemctl start platform6

8
task.txt

@ -1,11 +1,11 @@
1. Стоп сервера +1. Стоп сервера
2. Снятие дампа (с евент стором/без евент стора) +2. Снятие дампа (с евент стором/без евент стора)
5. Отправка его на удаленный сервер/стянуть с удаленного сервера +5. Отправка его на удаленный сервер
6. Создание базы +6. Создание базы
7. Вотсановление дампа 7. Вотсановление дампа

Loading…
Cancel
Save