#!/bin/bash ### DO NOT EDIT THIS FILE source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh" app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd) source "$app_dir/etc/config.sh" usage="Usage: fetch_latest_db.sh [live|stage|local_live|local_stage] (1,0:fetchNewFlag) (1,0:withGdprFlag)" env="$(getArgument "$1" "$usage" "live stage local_live local_stage")" fetch_new="$(getArgument "$2" "$usage" "1 0")" with_gdpr="$(getArgument "$3" "$usage" "1 0")" is_remote="$(if [ "$env" == "live" ] || [ "$env" == "stage" ]; then echo 1; else echo 0; fi)" currentDate=$(date '+%Y-%m-%d_%H:%M:%S') echo echoMainTitle "Backup $env system database" database_name="$(getConfig "$env" "database_name")" if [ "$is_remote" == "1" ] then ssh_user="$(getConfig "$env" "ssh_user")" ssh_domain="$(getConfig "$env" "ssh_domain")" addSSHKey "$env" ignoredTablesArguments=("$(buildIgnoredTablesArguments "$env" "${gdpr_tables[*]}")") echo "Backup structure ..." has_fetched="$(if [ -f "$app_dir/var/latest/${env}_structure.sql.gz" ]; then echo 1; else echo 0; fi)" if [ "$fetch_new" == "1" ] || [ "$has_fetched" == "0" ] then scp "$app_dir/etc/$env.my.cnf" "$ssh_user"@"$ssh_domain":"~/$env.my.cnf" ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/$env.my.cnf --no-tablespaces --no-data \ \"$database_name\" | LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | \ sed -e 's/,NO_AUTO_CREATE_USER//' | gzip -9 > ~/${env}_structure.sql.gz" scp "$ssh_user"@"$ssh_domain":"~/${env}_structure.sql.gz" "$app_dir/backup/database/$currentDate-${env}_structure.sql.gz" ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_structure.sql.gz" fi echo "Backup data ..." has_fetched="$(if [ -f "$app_dir/var/latest/${env}_data.sql.gz" ]; then echo 1; else echo 0; fi)" if [ "$fetch_new" == "1" ] || [ "$has_fetched" == "0" ] then ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/${env}.my.cnf --no-tablespaces --no-create-info \ --skip-triggers ${ignoredTablesArguments[*]} \"$database_name\" | LANG=C LC_CTYPE=C LC_ALL=C \ sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > ~/${env}_data.sql.gz" scp "$ssh_user"@"$ssh_domain":"~/${env}_data.sql.gz" "$app_dir/backup/database/$currentDate-${env}_data.sql.gz" ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_data.sql.gz" fi if [ "$with_gdpr" == "1" ] then includedTables=("$(buildIncludedTables "${gdprTables[@]}")") echo "Backup GDPR ..." has_fetched="$(if [ -f "$app_dir/var/latest/${env}_gdpr_data.sql.gz" ]; then echo 1; else echo 0; fi)" if [ "$fetch_new" == "1" ] || [ "$has_fetched" == "0" ] then ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/${env}.my.cnf --no-tablespaces --no-create-info --skip-triggers \ \"$database_name\" ${includedTables[*]} | LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \ ~/${env}_gdpr_data.sql.gz" scp "$ssh_user"@"$ssh_domain":"~/${env}_gdpr_data.sql.gz" "$app_dir/backup/database/$currentDate-${env}_gdpr_data.sql.gz" ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_gdpr_data.sql.gz" fi fi ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}.my.cnf" else echo "Backup structure ..." mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --hex-blob --no-data "$database_name" | \ LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | sed -e 's/,NO_AUTO_CREATE_USER//' | gzip -9 > \ "$app_dir/backup/database/${currentDate}-${env}_structure.sql.gz" echo "Backup data ..." mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --no-create-info --skip-triggers --hex-blob \ ${ignoredTablesArguments[*]} "$database_name" | \ LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \ "$app_dir/backup/database/${currentDate}-${env}_data.sql.gz" if [ "$with_gdpr" == "1" ] then includedTables=("$(buildIncludedTables "${gdprTables[@]}")") echo "Backup gdpr ..." mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --no-create-info --skip-triggers --hex-blob \ "$database_name" ${includedTables[*]} | \ LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \ "$app_dir/backup/database/${currentDate}-${env}_gdpr_data.sql.gz" fi fi echoFinal "... database backed up" echo "Running postscript ..." postScript "$app_dir/bin/postscripts/functions/backup_db.sh"