From f4acf11ac3501111c341e1ba9387bfc241dd431e Mon Sep 17 00:00:00 2001 From: Sven Ullmann Date: Tue, 7 Mar 2023 08:00:40 +0100 Subject: [PATCH] db backup script --- bin/bak/functions/backup_db.sh | 3 -- bin/commands/create-project | 2 + bin/commands/install-project-manager | 2 + bin/commands/list-projects | 2 + bin/commands/remove-project | 2 + bin/includes/includes | 2 + bin/includes/project_header | 2 + bin/project-manager | 2 + plugins/db/commands/backup | 46 +++++++++++++++++++ plugins/db/commands/create-config | 34 ++++++++++++-- plugins/db/etc/.my.cnf_template | 5 ++- plugins/db/includes/includes | 59 ++++++++++++++++++++++++- plugins/git/commands/clone | 2 + plugins/git/includes/includes | 2 + plugins/plesk/commands/create-database | 2 + plugins/plesk/commands/create-subdomain | 2 + plugins/plesk/includes/includes | 2 + plugins/shopware/includes/includes | 2 + plugins/ssh/commands/add-key | 2 + plugins/ssh/includes/includes | 2 + 20 files changed, 166 insertions(+), 11 deletions(-) create mode 100644 plugins/db/commands/backup diff --git a/bin/bak/functions/backup_db.sh b/bin/bak/functions/backup_db.sh index 0733cf3..42becc9 100644 --- a/bin/bak/functions/backup_db.sh +++ b/bin/bak/functions/backup_db.sh @@ -21,9 +21,6 @@ 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[*]}")") diff --git a/bin/commands/create-project b/bin/commands/create-project index 712121e..7492ec2 100644 --- a/bin/commands/create-project +++ b/bin/commands/create-project @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" diff --git a/bin/commands/install-project-manager b/bin/commands/install-project-manager index 8da7b39..222c374 100644 --- a/bin/commands/install-project-manager +++ b/bin/commands/install-project-manager @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/includes" function usage { diff --git a/bin/commands/list-projects b/bin/commands/list-projects index d53a014..b7b1a53 100644 --- a/bin/commands/list-projects +++ b/bin/commands/list-projects @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" diff --git a/bin/commands/remove-project b/bin/commands/remove-project index ff48033..e6c6cfe 100644 --- a/bin/commands/remove-project +++ b/bin/commands/remove-project @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/config" source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" diff --git a/bin/includes/includes b/bin/includes/includes index 20c5768..f392cb6 100644 --- a/bin/includes/includes +++ b/bin/includes/includes @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + if [ true != ${includes_included:-false} ] then includes_included=true diff --git a/bin/includes/project_header b/bin/includes/project_header index a296464..cd65c7c 100644 --- a/bin/includes/project_header +++ b/bin/includes/project_header @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + source "$project_manager_dir/bin/includes/includes" shortname="$(getArgument "$1" "$(usage)" true)" project="$(getProjectFromShortname "$shortname")" diff --git a/bin/project-manager b/bin/project-manager index 6d8d3d0..612e14f 100755 --- a/bin/project-manager +++ b/bin/project-manager @@ -1,6 +1,8 @@ #!/bin/bash set -e +### DO NOT EDIT THIS FILE + project_manager_dir="$(readlink -f "$(dirname "$(readlink -f ${BASH_SOURCE[0]})")/..")" source "$project_manager_dir/bin/includes/includes" if [ -f "$project_manager_dir/etc/config" ] diff --git a/plugins/db/commands/backup b/plugins/db/commands/backup new file mode 100644 index 0000000..e9e5408 --- /dev/null +++ b/plugins/db/commands/backup @@ -0,0 +1,46 @@ +#!/bin/bash + +### DO NOT EDIT THIS FILE + +function usage { + echo + echoMainTitle "Backup a database" + echo + echoSubTitle "Usage:" + echo + echo "project-manager db:backup [project-shortname] [env]" + echo + echo " [env] can be live, stage, local_live or local_stage" + echo + echo "--help Prints this message" + echo +} + +help="$(getParameter "--help" false "$*")" +if [ "$help" == true ] || [ "$1" == "" ] +then + usage + exit +fi + +source "$project_manager_dir/bin/includes/project_header" +env="$(getArgument "$2" "$(usage)" "live stage local_live local_stage")" +dbValidate + +database=$(dbGetConfig "$env" "database") + +echo +echoMainTitle "Backup database $env" +echo + +echo "Backup structure ..." +db="$(dbDump "$env" "structure" "--no-data" "$database")" +echo "Database structure backed up to: $db" + +echo "Backup data ..." +db="$(dbDump "$env" "data" "--no-create-info --skip-triggers" "$database")" +echo "Database data backed up to: $db" + +echo +echoSuccess "Database has been backed up" +echo \ No newline at end of file diff --git a/plugins/db/commands/create-config b/plugins/db/commands/create-config index d45e35e..736fdd2 100644 --- a/plugins/db/commands/create-config +++ b/plugins/db/commands/create-config @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + function usage { echo echoMainTitle "Create configuration files" @@ -59,7 +61,13 @@ path="$project_manager_dir/data/$customer/$project" quoted_socket="$(sedEscape "$db_stage_socket")" quoted_password="$(sedEscape "$db_stage_password")" quoted_admin_password="$(sedEscape "$db_stage_admin_password")" -cat "$template_file" | \ +if [ "$quoted_socket" == "" ] +then + file_string="$(cat "$template_file" | sed "s/socket=//")" +else + file_string="$(cat "$template_file")" +fi +echo "$file_string" | \ sed "s/socket=/socket=$quoted_socket/" | \ sed "s/host=/host=$db_stage_host/" | \ sed "s/port=/port=$db_stage_port/" | \ @@ -71,7 +79,13 @@ cat "$template_file" | \ quoted_socket="$(sedEscape "$db_live_socket")" quoted_password="$(sedEscape "$db_live_password")" quoted_admin_password="$(sedEscape "$db_live_admin_password")" -cat "$template_file" | \ +if [ "$quoted_socket" == "" ] +then + file_string="$(cat "$template_file" | sed "s/socket=//")" +else + file_string="$(cat "$template_file")" +fi +echo "$file_string" | \ sed "s/socket=/socket=$quoted_socket/" | \ sed "s/host=/host=$db_live_host/" | \ sed "s/port=/port=$db_live_port/" | \ @@ -83,7 +97,13 @@ cat "$template_file" | \ quoted_socket="$(sedEscape "$db_local_stage_socket")" quoted_password="$(sedEscape "$db_local_stage_password")" quoted_admin_password="$(sedEscape "$db_local_stage_admin_password")" -cat "$template_file" | \ +if [ "$quoted_socket" == "" ] +then + file_string="$(cat "$template_file" | sed "s/socket=//")" +else + file_string="$(cat "$template_file")" +fi +echo "$file_string" | \ sed "s/socket=/socket=$quoted_socket/" | \ sed "s/host=/host=$db_local_stage_host/" | \ sed "s/port=/port=$db_local_stage_port/" | \ @@ -95,7 +115,13 @@ cat "$template_file" | \ quoted_socket="$(sedEscape "$db_local_live_socket")" quoted_password="$(sedEscape "$db_local_live_password")" quoted_admin_password="$(sedEscape "$db_local_live_admin_password")" -cat "$template_file" | \ +if [ "$quoted_socket" == "" ] +then + file_string="$(cat "$template_file" | sed "s/socket=//")" +else + file_string="$(cat "$template_file")" +fi +echo "$file_string" | \ sed "s/socket=/socket=$quoted_socket/" | \ sed "s/host=/host=$db_local_live_host/" | \ sed "s/port=/port=$db_local_live_port/" | \ diff --git a/plugins/db/etc/.my.cnf_template b/plugins/db/etc/.my.cnf_template index 3a2dacc..a55291c 100644 --- a/plugins/db/etc/.my.cnf_template +++ b/plugins/db/etc/.my.cnf_template @@ -1,16 +1,17 @@ + +### DO NOT EDIT THIS FILE + [mysqldump] max_allowed_packet=500M socket= host= port= -database= [mysql] max_allowed_packet=500M socket= host= port= -database= [client] diff --git a/plugins/db/includes/includes b/plugins/db/includes/includes index 2443487..bc2e7fd 100644 --- a/plugins/db/includes/includes +++ b/plugins/db/includes/includes @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + db_included=true function dbValidate { @@ -10,11 +12,64 @@ function dbValidate { echo >&2 exit fi + + if [ "$ssh_included" == "" ] + then + echo >&2 + echoError "Plugin \"ssh\" has to be included" >&2 + echo >&2 + exit + fi } function dbGetConfig { - local env=$(getArgument "$1" "Usage getEnvVar [live|stage|local_live|local_stage] var" "live stage local_live local_stage") - local suffix=$(getArgument "$2" "Usage getEnvVar [live|stage|local_live|local_stage] var" true) + local env=$(getArgument "$1" "Usage dbGetConfig [live|stage|local_live|local_stage] var" "live stage local_live local_stage") + local suffix=$(getArgument "$2" "Usage dbGetConfig [live|stage|local_live|local_stage] var" true) echo "$(eval "echo \"\$db_${env}_$suffix\"")" +} + +function dbDump { + local env=$(getArgument "$1" "Usage: dbDump [env] [filename] [mysqldump-parameters] [database] [tables]" true) + local name=$(getArgument "$2" "Usage: dbDump [env] [filename] [mysqldump-parameters] [database] [tables]" true) + local parameters=$(getArgument "$3" "Usage: dbDump [env] [filename] [mysqldump-parameters] [database] [tables]") + local database=$(getArgument "$4" "Usage: dbDump [env] [filename] [mysqldump-parameters] [database] [tables]" true) + local tables=$(getArgument "$5" "Usage: dbDump [env] [filename] [mysqldump-parameters] [database] [tables]") + + local currentDate=$(date '+%Y-%m-%d_%H:%M:%S') + local is_remote="$(if [ "$env" == "live" ] || [ "$env" == "stage" ]; then echo 1; else echo 0; fi)" + + if [ "$is_remote" == 1 ] + then + local ssh_user="$(sshGetConfig "$env" "user")" + local ssh_domain="$(sshGetConfig "$env" "domain")" + sshAddKey "$env" + + 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 ${parameters[*]} $database $tables | \ + LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | \ + sed -e 's/,NO_AUTO_CREATE_USER//' | gzip -9 > ~/${env}_$name.sql.gz" + scp "$ssh_user"@"$ssh_domain":"~/${env}_$name.sql.gz" "$app_dir/var/tmp/$currentDate-${env}_$name.sql.gz" + ssh "$ssh_user"@"$ssh_domain" "unlink ~/${env}_$name.sql.gz" + + gunzip "$app_dir/var/tmp/$currentDate-${env}_$name.sql.gz" + else + mysqldump --defaults-extra-file="$app_dir/etc/${env}.my.cnf" --no-tablespaces ${parameters[*]} $database $tables | \ + LANG=C LC_CTYPE=C LC_ALL=C sed -e 's/DEFINER[ ]*=[ ]*[^*]*\*/\*/' | sed -e 's/,NO_AUTO_CREATE_USER//' \ + > "$app_dir/var/tmp/${currentDate}-${env}_$name.sql" + fi + + if [[ "$(tail -n 2 "$app_dir/var/tmp/$currentDate-${env}_$name.sql" )" != *"-- Dump completed"* ]] + then + rm "$app_dir/var/tmp/$currentDate-${env}_$name.sql" + echo + echoError "File was corrupted, deleting" + echo + exit + fi + + gzip -c "$app_dir/var/tmp/${currentDate}-${env}_$name.sql.gz" > "$app_dir/backup/database/${currentDate}-${env}_$name.sql.gz" + rm "$app_dir/var/tmp/$currentDate-${env}_$name.sql" + + echo "$app_dir/backup/database/$currentDate-${env}_$name.sql.gz" } \ No newline at end of file diff --git a/plugins/git/commands/clone b/plugins/git/commands/clone index f97502d..5c3ef6c 100644 --- a/plugins/git/commands/clone +++ b/plugins/git/commands/clone @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + function usage { echo echoMainTitle "Clone repository" diff --git a/plugins/git/includes/includes b/plugins/git/includes/includes index 83a0254..415dc7f 100644 --- a/plugins/git/includes/includes +++ b/plugins/git/includes/includes @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + git_included=true function gitValidate { diff --git a/plugins/plesk/commands/create-database b/plugins/plesk/commands/create-database index 9db419c..2207909 100644 --- a/plugins/plesk/commands/create-database +++ b/plugins/plesk/commands/create-database @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + function usage { echo echoMainTitle "Creates a database on plesk server" diff --git a/plugins/plesk/commands/create-subdomain b/plugins/plesk/commands/create-subdomain index 11cb258..f647bef 100644 --- a/plugins/plesk/commands/create-subdomain +++ b/plugins/plesk/commands/create-subdomain @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + function usage { echo echoMainTitle "Creates a subdomain on plesk server" diff --git a/plugins/plesk/includes/includes b/plugins/plesk/includes/includes index 221cb2e..7536c9d 100644 --- a/plugins/plesk/includes/includes +++ b/plugins/plesk/includes/includes @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + plesk_included=true function pleskValidate { diff --git a/plugins/shopware/includes/includes b/plugins/shopware/includes/includes index f83d684..6236a68 100644 --- a/plugins/shopware/includes/includes +++ b/plugins/shopware/includes/includes @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + function shopware6BuildIgnoredTablesArguments { local env=$(getArgument "$1" "Usage: buildIgnoredTables [live|stage|local] [ignoredTablesArray]" "live stage local") local ignoredTables=$(getArgument "$2" "Usage: buildIgnoredTables [live|stage|local] [ignoredTablesArray]" true) diff --git a/plugins/ssh/commands/add-key b/plugins/ssh/commands/add-key index 17901d7..46d4de0 100644 --- a/plugins/ssh/commands/add-key +++ b/plugins/ssh/commands/add-key @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + function usage { echo echoMainTitle "Create, move the ssh keys and install them to server" diff --git a/plugins/ssh/includes/includes b/plugins/ssh/includes/includes index aadb08b..21e5449 100644 --- a/plugins/ssh/includes/includes +++ b/plugins/ssh/includes/includes @@ -1,5 +1,7 @@ #!/bin/bash +### DO NOT EDIT THIS FILE + ssh_included=true function sshValidate {