Browse Source

backup name

master
M.Goldshteyn 1 year ago
parent
commit
003acd35ed
  1. 2
      create_db.sh.template
  2. 33
      migration.sh

2
create_db.sh.template

@ -11,4 +11,4 @@ su - postgres -c "PGPASSWORD=\"DESTINATION_POSTGRES_PASSWORD\" psql -U postgres
set "PGPASSWORD=\"DESTINATION_POSTGRES_PASSWORD\"" set "PGPASSWORD=\"DESTINATION_POSTGRES_PASSWORD\""
su - postgres -c "pg_restore -j 2 -d DESTINATION_DB_NAME Backup_name" su - postgres -c "pg_restore -j 2 -d DESTINATION_DB_NAME DB_BACKUP"

33
migration.sh

@ -1,5 +1,11 @@
#!/bin/bash #!/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 set -a # export variables from config.txt test
source config.txt source config.txt
set +a # stop exporting set +a # stop exporting
@ -11,16 +17,16 @@ 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 DB_BACKUP=$SOURCE_DB_NAME.migration.$DATE.sql
echo "backup name:" $Backup_name 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
echo Making backup without event_store 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 elif [[ "$EVENT_STORE" == 1 ]]; then
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/$DB_BACKUP
elif [[ "$EVENT_STORE" == 2 ]]; then elif [[ "$EVENT_STORE" == 2 ]]; then
echo "don't make db backup" echo "don't make db backup"
else echo "EVENT_STORE value must be 0 or 1 or 2" && exit 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 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 DB_BACKUP $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 fi
@ -38,10 +44,11 @@ send_backup() {
create_db() { create_db() {
sed -e "s/DESTINATION_DB_NAME/${DESTINATION_DB_NAME}/" \ sed -e "s/DESTINATION_DB_NAME/${DESTINATION_DB_NAME}/" \
-e "s/DESTINATION_POSTGRES_PASSWORD/${DESTINATION_POSTGRES_PASSWORD}/"\ -e "s/DESTINATION_POSTGRES_PASSWORD/${DESTINATION_POSTGRES_PASSWORD}/"\
-e "s/DB_BACKUP/${$DB_BACKUP}/"\
create_db.sh.template > create_db.sh 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 sshpass -p "$DESTINATION_DATABASE_PASSWORD" ssh -p $DESTINATION_DATABASE_PORT $DESTINATION_DATABASE_USER@$DESTINATION_DATABASE_IP 'bash -s' < create_db.sh
} }
#restore_db(){ #restore_db(){
# if [[ "$EVENT_STORE" == 0 ]] or [[ "$EVENT_STORE" == 1 ]]; then # if [[ "$EVENT_STORE" == 0 ]] or [[ "$EVENT_STORE" == 1 ]]; then
# fi # fi
@ -81,19 +88,5 @@ fonts_rsync(){
fi 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 platform5.service && systemctl start platform5
systemctl is-active --quiet platform6.service && systemctl start platform6 systemctl is-active --quiet platform6.service && systemctl start platform6
Loading…
Cancel
Save