This repository was archived by the owner on Dec 13, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathEXAMPLE_MYSQL_BACKUP_SCRIPT.sh
More file actions
100 lines (82 loc) · 2.89 KB
/
EXAMPLE_MYSQL_BACKUP_SCRIPT.sh
File metadata and controls
100 lines (82 loc) · 2.89 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#!/usr/bin/env bash
# =============================================================================
# Copyright [2015] [Kevin Carter]
# License Information :
# This software has no warranty, it is provided 'as is'. It is your
# responsibility to validate the behavior of the routines and its accuracy
# using the code provided. Consult the GNU General Public license for further
# details (see GNU General Public License).
# http://www.gnu.org/licenses/gpl.html
# =============================================================================
# Purpose:
# This script will backup all of the databases found in MySQL as individual
# databases. Once the backup is complete a tarball will be created of all
# databases which were found and dumped as ".sql" files. Finally if
# Turbolift is installed and a settings file has been created containing
# your credentials the backed up databases will be saved in a cloudfiles
# container.
set -e
# Set .my.cnf variable
MYSQL_CRED_FILE="/root/.my.cnf"
# Set the database backup directory
BACKUP_LOCATION="/opt/backup/databases"
# Set the turbolift settings file
TURBOLIFT_CONFIG="/etc/turbolift.cfg"
export PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
# Make sure mysql CLI clients are found.
if [ ! "$(which mysql)" ];then
echo "mysql client not installed"
exit 1
fi
# Make sure mysqldump CLI clients are found.
if [ ! "$(which mysqldump)" ];then
echo "mysqldump client not installed"
exit 1
fi
# Check if the my.cnf file exists
if [ ! -f "${MYSQL_CRED_FILE}" ];then
echo "MySQL .my.cnf file was not found."
exit 1
fi
# Check for backup directory
if [ ! -d "${BACKUP_LOCATION}" ];then
mkdir -p ${BACKUP_LOCATION}
fi
# Get value from mysql file
function getcred(){
grep "${1}" ${MYSQL_CRED_FILE} | awk -F'=' '{print $2}' | awk '{$1=$1}{ print }'
}
# Get a list of all of the databases
function getdatabases(){
mysql --silent \
--skip-column-names \
-u"${MYSQL_USERNAME}" \
-p"${MYSQL_PASSWORD}" \
-e "show databases;" | grep -v "performance_schema"
}
MYSQL_USERNAME=$(getcred "user")
MYSQL_PASSWORD=$(getcred "password")
MYSQL_DATABASES=$(getdatabases)
# Backup the databases separately
for db in ${MYSQL_DATABASES};do
mysqldump --events \
--skip-lock-tables \
-u"${MYSQL_USERNAME}" \
-p"${MYSQL_PASSWORD}" \
${db} > ${BACKUP_LOCATION}/${db}.sql
done
# Compress all of the databases
tar -czf /tmp/database-tarball.tgz ${BACKUP_LOCATION} > /dev/null 2>&1
if [ -f "/tmp/database-tarball.tgz" ];then
mv /tmp/database-tarball.tgz ${BACKUP_LOCATION}/database-tarball.tgz
fi
# Backup the databases which were just backed up
if [ "$(which turbolift)" ];then
if [ -f "${TURBOLIFT_CONFIG}" ];then
turbolift --quiet \
--system-config ${TURBOLIFT_CONFIG} \
upload \
-c backup-databases \
-s "${BACKUP_LOCATION}"
fi
fi