-
Notifications
You must be signed in to change notification settings - Fork 1
/
mysql-restore
47 lines (36 loc) · 1.75 KB
/
mysql-restore
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/bin/bash
# Restore your WordPress database.
set -e
set -o pipefail
# 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"
file_name="${1}"
restore_path="${backup_destination}/${file_name}"
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")"
if [ -z "${file_name}" ]; then
echo "You must supply a file name, example: ${0} ${db_name}_$(date +%Y%m%d-%H%M%S).sql.gz"
exit 1
fi
if [ ! -f "${restore_path}" ]; then
echo "'${restore_path}' not found"
exit 1
fi
read -p "Restoring is going to wipe your current database, are you sure (y/n)? " -n 1 -r
echo
if [[ ! "${REPLY}" =~ ^[yY]$ ]]; then
echo "The '${db_name}' database was not restored because you didn't type y or Y."
exit 1
fi
# 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}' \nRESTORE PATH: '${restore_path}' \n"; exit;
# MySQL / MariaDB.
zcat "${restore_path}" | mysql -h "${db_host}" -u "${db_user}" -p"${db_pass}" "${db_name}"
echo "The '${db_name}' database was successfully restored from '${file_name}'"