-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql-backup
28 lines (23 loc) · 1.33 KB
/
mysql-backup
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#!/bin/bash
# Backup WordPress database to a compressed file.
set -e #stop on error
set -o pipefail #stop on pipe error
# start of SETUP
dh_user='techxing'
dh_web_directory='techxing.net'
# end of SETUP
dh_user_folder="/home/${dh_user}"
backup_destination="${dh_user_folder}/mysql-backup-dreamhost/backups"
db_conf="${dh_user_folder}/${dh_web_directory}/wp-config.php"
db_name="$(grep -o "define('DB_NAME', '[^']*[^');]'" "${db_conf}" | cut -d "," -f 2 | sed "s/'//g;s/ //g")"
db_user="$(grep -o "define('DB_USER', '[^']*[^');]'" "${db_conf}" | cut -d "," -f 2 | sed "s/'//g;s/ //g")"
db_pass="$(grep -o "define('DB_PASSWORD', '[^']*[^');]'" "${db_conf}" | cut -d "," -f 2 | sed "s/'//g;s/ //g")"
db_host="$(grep -o "define('DB_HOST', '[^']*[^');]'" "${db_conf}" | cut -d "," -f 2 | sed "s/'//g;s/ //g")"
# Uncomment to show the variables extracted. The whole script will stop before doing the mysqldump.
# printf "VARS: \n\${db_conf} = '${db_conf}' \n\${db_name} = '${db_name}' \n\${db_user} = '${db_user}' \n\${db_pass} = '${db_pass}' \n\${db_host} = '${db_host}' \nDESTINATION: '${backup_destination}/${db_name}_$(date +%Y%m%d-%H%M%S).sql.gz' \n"; exit;
# MySQL / MariaDB.
/usr/bin/mysqldump -h "${db_host}" \
-u "${db_user}" \
-p"${db_pass}" \
"${db_name}" \
| gzip > "${backup_destination}/${db_name}_$(date +%Y%m%d-%H%M%S).sql.gz"