Ein für Shopware 6 vorbereitet Systemmanagment.
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.

103 lines
4.6 KiB

  1. #!/bin/bash
  2. ### DO NOT EDIT THIS FILE
  3. source "$(dirname "${BASH_SOURCE[0]}")/../../includes/includes.sh"
  4. app_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")/../../.." &> /dev/null && pwd)
  5. source "$app_dir/etc/config.sh"
  6. usage="Usage: fetch_latest_db.sh [live|stage|local_live|local_stage] (1,0:fetchNewFlag) (1,0:withGdprFlag)"
  7. env="$(getArgument "$1" "$usage" "live stage local_live local_stage")"
  8. fetch_new="$(getArgument "$2" "$usage" "1 0")"
  9. with_gdpr="$(getArgument "$3" "$usage" "1 0")"
  10. is_remote="$(if [ "$env" == "live" ] || [ "$env" == "stage" ]; then echo 1; else echo 0; fi)"
  11. currentDate=$(date '+%Y-%m-%d_%H:%M:%S')
  12. echo
  13. echoMainTitle "Backup $env system database"
  14. database_name="$(getConfig "$env" "database_name")"
  15. if [ "$is_remote" == "1" ]
  16. then
  17. ssh_user="$(getConfig "$env" "ssh_user")"
  18. ssh_domain="$(getConfig "$env" "ssh_domain")"
  19. addSSHKey "$env"
  20. ignoredTablesArguments=("$(buildIgnoredTablesArguments "$env" "${gdpr_tables[*]}")")
  21. echo "Backup structure ..."
  22. has_fetched="$(if [ -f "$app_dir/var/latest/${env}_structure.sql.gz" ]; then echo 1; else echo 0; fi)"
  23. if [ "$fetch_new" == "1" ] || [ "$has_fetched" == "0" ]
  24. then
  25. scp "$app_dir/etc/$env.my.cnf" "$ssh_user"@"$ssh_domain":"~/$env.my.cnf"
  26. ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/$env.my.cnf --no-tablespaces --no-data \
  27. \"$database_name\" | LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | \
  28. sed -e 's/,NO_AUTO_CREATE_USER//' | gzip -9 > ~/${env}_structure.sql.gz"
  29. scp "$ssh_user"@"$ssh_domain":"~/${env}_structure.sql.gz" "$app_dir/backup/database/$currentDate-${env}_structure.sql.gz"
  30. ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_structure.sql.gz"
  31. fi
  32. echo "Backup data ..."
  33. has_fetched="$(if [ -f "$app_dir/var/latest/${env}_data.sql.gz" ]; then echo 1; else echo 0; fi)"
  34. if [ "$fetch_new" == "1" ] || [ "$has_fetched" == "0" ]
  35. then
  36. ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/${env}.my.cnf --no-tablespaces --no-create-info \
  37. --skip-triggers ${ignoredTablesArguments[*]} \"$database_name\" | LANG=C LC_CTYPE=C LC_ALL=C \
  38. sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > ~/${env}_data.sql.gz"
  39. scp "$ssh_user"@"$ssh_domain":"~/${env}_data.sql.gz" "$app_dir/backup/database/$currentDate-${env}_data.sql.gz"
  40. ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_data.sql.gz"
  41. fi
  42. if [ "$with_gdpr" == "1" ]
  43. then
  44. includedTables=("$(buildIncludedTables "${gdprTables[@]}")")
  45. echo "Backup GDPR ..."
  46. has_fetched="$(if [ -f "$app_dir/var/latest/${env}_gdpr_data.sql.gz" ]; then echo 1; else echo 0; fi)"
  47. if [ "$fetch_new" == "1" ] || [ "$has_fetched" == "0" ]
  48. then
  49. ssh "$ssh_user"@"$ssh_domain" "mysqldump --defaults-extra-file=~/${env}.my.cnf --no-tablespaces --no-create-info --skip-triggers \
  50. \"$database_name\" ${includedTables[*]} | LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \
  51. ~/${env}_gdpr_data.sql.gz"
  52. scp "$ssh_user"@"$ssh_domain":"~/${env}_gdpr_data.sql.gz" "$app_dir/backup/database/$currentDate-${env}_gdpr_data.sql.gz"
  53. ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_gdpr_data.sql.gz"
  54. fi
  55. fi
  56. ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}.my.cnf"
  57. else
  58. echo "Backup structure ..."
  59. mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --hex-blob --no-data "$database_name" | \
  60. LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | sed -e 's/,NO_AUTO_CREATE_USER//' | gzip -9 > \
  61. "$app_dir/backup/database/${currentDate}-${env}_structure.sql.gz"
  62. echo "Backup data ..."
  63. mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --no-create-info --skip-triggers --hex-blob \
  64. ${ignoredTablesArguments[*]} "$database_name" | \
  65. LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \
  66. "$app_dir/backup/database/${currentDate}-${env}_data.sql.gz"
  67. if [ "$with_gdpr" == "1" ]
  68. then
  69. includedTables=("$(buildIncludedTables "${gdprTables[@]}")")
  70. echo "Backup gdpr ..."
  71. mysqldump --defaults-extra-file="$app_dir/etc/local.my.cnf" --no-tablespaces --no-create-info --skip-triggers --hex-blob \
  72. "$database_name" ${includedTables[*]} | \
  73. LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | gzip -9 > \
  74. "$app_dir/backup/database/${currentDate}-${env}_gdpr_data.sql.gz"
  75. fi
  76. fi
  77. echoFinal "... database backed up"
  78. echo "Running postscript ..."
  79. postScript "$app_dir/bin/postscripts/functions/backup_db.sh"