Browse Source

db backup script

master
Sven Ullmann 2 years ago
parent
commit
f4acf11ac3
  1. 3
      bin/bak/functions/backup_db.sh
  2. 2
      bin/commands/create-project
  3. 2
      bin/commands/install-project-manager
  4. 2
      bin/commands/list-projects
  5. 2
      bin/commands/remove-project
  6. 2
      bin/includes/includes
  7. 2
      bin/includes/project_header
  8. 2
      bin/project-manager
  9. 46
      plugins/db/commands/backup
  10. 34
      plugins/db/commands/create-config
  11. 5
      plugins/db/etc/.my.cnf_template
  12. 59
      plugins/db/includes/includes
  13. 2
      plugins/git/commands/clone
  14. 2
      plugins/git/includes/includes
  15. 2
      plugins/plesk/commands/create-database
  16. 2
      plugins/plesk/commands/create-subdomain
  17. 2
      plugins/plesk/includes/includes
  18. 2
      plugins/shopware/includes/includes
  19. 2
      plugins/ssh/commands/add-key
  20. 2
      plugins/ssh/includes/includes

3
bin/bak/functions/backup_db.sh

@ -21,9 +21,6 @@ database_name="$(getConfig "$env" "database_name")"
if [ "$is_remote" == "1" ] if [ "$is_remote" == "1" ]
then then
ssh_user="$(getConfig "$env" "ssh_user")"
ssh_domain="$(getConfig "$env" "ssh_domain")"
addSSHKey "$env"
ignoredTablesArguments=("$(buildIgnoredTablesArguments "$env" "${gdpr_tables[*]}")") ignoredTablesArguments=("$(buildIgnoredTablesArguments "$env" "${gdpr_tables[*]}")")

2
bin/commands/create-project

@ -1,5 +1,7 @@
#!/bin/bash #!/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/config"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects"

2
bin/commands/install-project-manager

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/includes" source "$(cd "$(dirname "${BASH_SOURCE[0]}")/.." &> /dev/null && pwd)/includes/includes"
function usage { function usage {

2
bin/commands/list-projects

@ -1,5 +1,7 @@
#!/bin/bash #!/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/config"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects"

2
bin/commands/remove-project

@ -1,5 +1,7 @@
#!/bin/bash #!/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/config"
source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects" source "$(cd "$(dirname "${BASH_SOURCE[0]}")/../.." &> /dev/null && pwd)/etc/projects"

2
bin/includes/includes

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
if [ true != ${includes_included:-false} ] if [ true != ${includes_included:-false} ]
then then
includes_included=true includes_included=true

2
bin/includes/project_header

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
source "$project_manager_dir/bin/includes/includes" source "$project_manager_dir/bin/includes/includes"
shortname="$(getArgument "$1" "$(usage)" true)" shortname="$(getArgument "$1" "$(usage)" true)"
project="$(getProjectFromShortname "$shortname")" project="$(getProjectFromShortname "$shortname")"

2
bin/project-manager

@ -1,6 +1,8 @@
#!/bin/bash #!/bin/bash
set -e set -e
### DO NOT EDIT THIS FILE
project_manager_dir="$(readlink -f "$(dirname "$(readlink -f ${BASH_SOURCE[0]})")/..")" project_manager_dir="$(readlink -f "$(dirname "$(readlink -f ${BASH_SOURCE[0]})")/..")"
source "$project_manager_dir/bin/includes/includes" source "$project_manager_dir/bin/includes/includes"
if [ -f "$project_manager_dir/etc/config" ] if [ -f "$project_manager_dir/etc/config" ]

46
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

34
plugins/db/commands/create-config

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
function usage { function usage {
echo echo
echoMainTitle "Create configuration files" echoMainTitle "Create configuration files"
@ -59,7 +61,13 @@ path="$project_manager_dir/data/$customer/$project"
quoted_socket="$(sedEscape "$db_stage_socket")" quoted_socket="$(sedEscape "$db_stage_socket")"
quoted_password="$(sedEscape "$db_stage_password")" quoted_password="$(sedEscape "$db_stage_password")"
quoted_admin_password="$(sedEscape "$db_stage_admin_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/socket=/socket=$quoted_socket/" | \
sed "s/host=/host=$db_stage_host/" | \ sed "s/host=/host=$db_stage_host/" | \
sed "s/port=/port=$db_stage_port/" | \ sed "s/port=/port=$db_stage_port/" | \
@ -71,7 +79,13 @@ cat "$template_file" | \
quoted_socket="$(sedEscape "$db_live_socket")" quoted_socket="$(sedEscape "$db_live_socket")"
quoted_password="$(sedEscape "$db_live_password")" quoted_password="$(sedEscape "$db_live_password")"
quoted_admin_password="$(sedEscape "$db_live_admin_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/socket=/socket=$quoted_socket/" | \
sed "s/host=/host=$db_live_host/" | \ sed "s/host=/host=$db_live_host/" | \
sed "s/port=/port=$db_live_port/" | \ sed "s/port=/port=$db_live_port/" | \
@ -83,7 +97,13 @@ cat "$template_file" | \
quoted_socket="$(sedEscape "$db_local_stage_socket")" quoted_socket="$(sedEscape "$db_local_stage_socket")"
quoted_password="$(sedEscape "$db_local_stage_password")" quoted_password="$(sedEscape "$db_local_stage_password")"
quoted_admin_password="$(sedEscape "$db_local_stage_admin_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/socket=/socket=$quoted_socket/" | \
sed "s/host=/host=$db_local_stage_host/" | \ sed "s/host=/host=$db_local_stage_host/" | \
sed "s/port=/port=$db_local_stage_port/" | \ sed "s/port=/port=$db_local_stage_port/" | \
@ -95,7 +115,13 @@ cat "$template_file" | \
quoted_socket="$(sedEscape "$db_local_live_socket")" quoted_socket="$(sedEscape "$db_local_live_socket")"
quoted_password="$(sedEscape "$db_local_live_password")" quoted_password="$(sedEscape "$db_local_live_password")"
quoted_admin_password="$(sedEscape "$db_local_live_admin_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/socket=/socket=$quoted_socket/" | \
sed "s/host=/host=$db_local_live_host/" | \ sed "s/host=/host=$db_local_live_host/" | \
sed "s/port=/port=$db_local_live_port/" | \ sed "s/port=/port=$db_local_live_port/" | \

5
plugins/db/etc/.my.cnf_template

@ -1,16 +1,17 @@
### DO NOT EDIT THIS FILE
[mysqldump] [mysqldump]
max_allowed_packet=500M max_allowed_packet=500M
socket= socket=
host= host=
port= port=
database=
[mysql] [mysql]
max_allowed_packet=500M max_allowed_packet=500M
socket= socket=
host= host=
port= port=
database=
[client] [client]

59
plugins/db/includes/includes

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
db_included=true db_included=true
function dbValidate { function dbValidate {
@ -10,11 +12,64 @@ function dbValidate {
echo >&2 echo >&2
exit exit
fi fi
if [ "$ssh_included" == "" ]
then
echo >&2
echoError "Plugin \"ssh\" has to be included" >&2
echo >&2
exit
fi
} }
function dbGetConfig { 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\"")" 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"
} }

2
plugins/git/commands/clone

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
function usage { function usage {
echo echo
echoMainTitle "Clone repository" echoMainTitle "Clone repository"

2
plugins/git/includes/includes

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
git_included=true git_included=true
function gitValidate { function gitValidate {

2
plugins/plesk/commands/create-database

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
function usage { function usage {
echo echo
echoMainTitle "Creates a database on plesk server" echoMainTitle "Creates a database on plesk server"

2
plugins/plesk/commands/create-subdomain

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
function usage { function usage {
echo echo
echoMainTitle "Creates a subdomain on plesk server" echoMainTitle "Creates a subdomain on plesk server"

2
plugins/plesk/includes/includes

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
plesk_included=true plesk_included=true
function pleskValidate { function pleskValidate {

2
plugins/shopware/includes/includes

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
function shopware6BuildIgnoredTablesArguments { function shopware6BuildIgnoredTablesArguments {
local env=$(getArgument "$1" "Usage: buildIgnoredTables [live|stage|local] [ignoredTablesArray]" "live stage local") local env=$(getArgument "$1" "Usage: buildIgnoredTables [live|stage|local] [ignoredTablesArray]" "live stage local")
local ignoredTables=$(getArgument "$2" "Usage: buildIgnoredTables [live|stage|local] [ignoredTablesArray]" true) local ignoredTables=$(getArgument "$2" "Usage: buildIgnoredTables [live|stage|local] [ignoredTablesArray]" true)

2
plugins/ssh/commands/add-key

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
function usage { function usage {
echo echo
echoMainTitle "Create, move the ssh keys and install them to server" echoMainTitle "Create, move the ssh keys and install them to server"

2
plugins/ssh/includes/includes

@ -1,5 +1,7 @@
#!/bin/bash #!/bin/bash
### DO NOT EDIT THIS FILE
ssh_included=true ssh_included=true
function sshValidate { function sshValidate {

Loading…
Cancel
Save