You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
4.7 KiB
105 lines
4.7 KiB
#!/bin/bash |
|
|
|
#install sshpass if not installed |
|
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 |
|
source config.txt |
|
set +a # stop exporting |
|
|
|
DATE="$(date +%Y-%m-%d_%H-%M-%S)" |
|
echo $DATE |
|
|
|
#1. We are on the source (e.g. main) |
|
systemctl is-active --quiet platform5.service && systemctl stop platform5 |
|
systemctl is-active --quiet platform6.service && systemctl stop platform6 |
|
|
|
DB_BACKUP=$SOURCE_DB_NAME.migration.$DATE.sql |
|
echo "backup name:" $DB_BACKUP |
|
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/$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/$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 |
|
fi |
|
} |
|
send_backup() { |
|
if [[ "$EVENT_STORE" == 0 ]] || [[ "$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 $DB_BACKUP $DESTINATION_DATABASE_USER@$DESTINATION_DATABASE_IP:/var/backups |
|
cd - |
|
else echo "no backup, nothing to send" |
|
fi |
|
} |
|
create_and_restore_db() { |
|
if [[ "$EVENT_STORE" == 0 ]] || [[ "$EVENT_STORE" == 1 ]]; then |
|
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 |
|
fi |
|
} |
|
|
|
change_config(){ |
|
sshpass -p "$DESTINATION_PASSWORD" ssh $DESTINATION_USER@$DESTINATION_IP -p $DESTINATION_PORT "sed -i \"s|\(jdbc:postgresql://localhost:5432/\)[^?]*|\1${DESTINATION_DB_NAME}|g\" $DESTINATION_PLATFORM_PATH/conf/db.conf" |
|
} |
|
db_backup |
|
send_backup |
|
create_and_restore_db |
|
change_config |
|
|
|
evt_full_rsync(){ |
|
echo "send $SOURCE_EVT to the destination" |
|
sshpass -p "$DESTINATION_PASSWORD" rsync -e "ssh -p $DESTINATION_PORT" -avz --ignore-existing $SOURCE_PLATFORM_PATH/bin/storage/1/$SOURCE_EVT/ $DESTINATION_USER@$DESTINATION_IP:$DESTINATION_PLATFORM_PATH/bin/storage/1/$SOURCE_EVT/ |
|
} |
|
|
|
evt_common_files_rsync(){ |
|
echo "send files from $SOURCE_EVT/common to the destination" |
|
yes | sshpass -p "$DESTINATION_PASSWORD" ssh $DESTINATION_USER@$DESTINATION_IP -p $DESTINATION_PORT "mkdir -p $DESTINATION_PLATFORM_PATH/bin/storage/1/$SOURCE_EVT/common/" |
|
sshpass -p "$DESTINATION_PASSWORD" rsync -e "ssh -p $DESTINATION_PORT -o StrictHostKeyChecking=no" -avz --ignore-existing -f"- */" -f"+ *" $SOURCE_PLATFORM_PATH/bin/storage/1/$SOURCE_EVT/common/ $DESTINATION_USER@$DESTINATION_IP:$DESTINATION_PLATFORM_PATH/bin/storage/1/$SOURCE_EVT/common/ |
|
} |
|
|
|
evt_specific_dirs_rsync(){ |
|
IFS=',' read -r -a array <<< "$FOLDERS" |
|
|
|
for folder in "${array[@]}"; do |
|
#rsync -avz "$SOURCE_PLATFORM_PATH/bin/storage/1/$SOURCE_EVT/$folder" user@remote_host:/remote/path/ |
|
sshpass -p "$DESTINATION_PASSWORD" rsync -e "ssh -p $DESTINATION_PORT" -avz --ignore-existing $SOURCE_PLATFORM_PATH/bin/storage/1/$SOURCE_EVT/$folder $DESTINATION_USER@$DESTINATION_IP:$DESTINATION_PLATFORM_PATH/bin/storage/1/$SOURCE_EVT/ |
|
done |
|
} |
|
|
|
fonts_rsync(){ |
|
echo "Send fonts to the destination" |
|
sshpass -p "$DESTINATION_PASSWORD" ssh $DESTINATION_USER@$DESTINATION_IP -p $DESTINATION_PORT "mkdir -p $DESTINATION_PLATFORM_PATH/bin/storage/1/fonts/" |
|
sshpass -p "$DESTINATION_PASSWORD" rsync -e "ssh -p $DESTINATION_PORT" -avz --ignore-existing $SOURCE_PLATFORM_PATH/bin/storage/1/fonts/ $DESTINATION_USER@$DESTINATION_IP:$DESTINATION_PLATFORM_PATH/bin/storage/1/fonts/ |
|
} |
|
fonts_rsync |
|
|
|
if [[ "$EVT_COMMON" == 0 ]]; then |
|
echo "Download only files from evt/common" |
|
evt_common_files_rsync |
|
elif [[ "$EVT_COMMON" == 1 ]]; then |
|
echo "Download full evt" |
|
evt_full_rsync |
|
elif [[ "$EVT_COMMON" == 2 ]]; then |
|
echo "Nothing to download from storage" |
|
elif [[ "$EVT_COMMON" == 3 ]]; then |
|
echo "Download specified folders" |
|
evt_specific_dirs_rsync |
|
else echo "EVENT_COMMON value must be 0 or 1 or 2" && exit |
|
fi |
|
|
|
|
|
systemctl is-active --quiet platform5.service || systemctl start platform5 |
|
systemctl is-active --quiet platform6.service || systemctl start platform6 |