-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_mysqlSlaveBackup.sh
221 lines (189 loc) · 5.63 KB
/
01_mysqlSlaveBackup.sh
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/bin/bash
# vim: sw=5
###########################################################################
#
# Program name: mysqlSlaveBackup.sh
# File name: mysqlSlaveBackup.sh
# Author: PBB
# First revision: Jan '2012
#
# Description:
# Script is intended to run from cron. It wll stop the mysql
# slave, dump the database and start the mysql slave again.
# The connection information should be in /root/.<defaultsFile>
# and it should be accessible only to root:
# cat /root/.<defaultsFile>
# [client]
# host=127.0.0.1
# port=3311 (or whatever the port is)
# user = backup
# password = secret
# [mysqladmin]
# host=127.0.0.1
# port=3311
# user = backup
# password = secret
#
# The mysql backup account requires SUPER privileges to be able to stop slave
# mysql> grant super
# -> on *.* to 'backup'@'localhost'
# -> IDENTIFIED BY 'secret';
#
# The cron entry should look like this:
# mysqlSlaveBackup \
# -c /root/.mysqlCustom.cnf \
# -d /dbslave/backup/daily \
# --keep 5
#
# This command will use .mysqlCustom.cnf to connect to the
# database, create backup file:
# /dbslave/backup/daily/mysql-<hostName>Slave-date.sql.gz
# and keep the 5 most recent files in the backup
# directory. It will remove the older files.
#
###########################################################################
PATH="/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin"
self="`basename $0`"
HOSTNAME=`hostname | sed 's/\..*//'`
USAGE="YES"
# Collect some parametters
#
while [ $# -ne 0 ]; do
case $1 in
"-c"|"--config")
USAGE="NO"
defaultsFile=$2
shift 1
;;
"-d"|"--directory")
USAGE="NO"
backupDir=$2
shift 1
;;
"--keep")
USAGE="NO"
toKeep=$2
shift 1
;;
*)
USAGE="YES"
;;
esac
shift 1
done
[ -z $defaultsFile ] && USAGE="YES"
[ -z $backupDir ] && USAGE="YES"
if [ ! -r $defaultsFile ]; then
echo "File $defaultsFile not readable"
exit 1
fi
if [ ! -d $backupDir ]; then
echo ""
echo "ERROR: Directory $backupDir not accessible !!!"
USAGE="YES"
fi
if [ "$USAGE" = "YES" ]; then
echo "
${self}:
Usage: $self -c defaultsFile -d backupDir --keep N
-c, --config defaultsFile
Use only this file to get connection information.
-d, --directory backupDir
Dump the databases in this directory.
--keep N
Where N is number of most recent backup files to keep.
Delete the rest.
-?, --help
Show this help message.
"
exit 1
fi
nameAddOn=`mysql --defaults-file=$defaultsFile -e 'show slave status\G' | \
grep Master_Host | awk '{print $2}'`
backupFile="$backupDir/mysql-${nameAddOn}Slave-`date +%Y%m%d`-$$.sql.gz"
# ===========================================================================
# Good to go. Let's do some real work.
# ---------------------------------------------------------------------------
stopSlave ()
{
CMD="mysql --defaults-file=$defaultsFile -e 'stop slave;'"
echo $CMD
echo $CMD | bash
}
# ---------------------------------------------------------------------------
startSlave ()
{
sleep 5
CMD="mysql --defaults-file=$defaultsFile -e 'start slave;'"
echo $CMD
echo $CMD | bash
}
# ---------------------------------------------------------------------------
backupSlave ()
{
sleep 5
/bin/touch $backupFile
if [ $? -ne 0 ]; then
echo ""
echo " ERROR: Can't create file $backupFile"
exit 1
fi
/bin/chmod 600 $backupFile
CMD="mysqldump --defaults-file=$defaultsFile \
--opt --quote-names --all-databases | gzip > $backupFile"
echo $CMD
echo $CMD | bash
}
# ---------------------------------------------------------------------------
rotateBackups ()
{
directoryFiles=`ls -t ${backupDir}/mysql-${nameAddOn}Slave*`
countFiles=`echo $directoryFiles | tr ' ' '\n' | wc -l`
if [ $countFiles -gt $toKeep ]; then
toDelete=`expr $countFiles - $toKeep`
deleteFiles=`echo $directoryFiles | tr ' ' '\n' | tail -$toDelete`
echo " Deleting:"
echo $deleteFiles | tr ' ' '\n'
echo $deleteFiles | xargs /bin/rm -f {}
if [ $? -ne 0 ]; then
echo " ERROR: Failed to successfully delete old backups !!!"
exit 1
fi
else
echo " Nothing to delete. Found only $countFiles backups."
fi
}
# ---------------------------------------------------------------------------
echo ""; echo "=== [`date +%D\ -\ %T`] Running $self on $HOSTNAME ==="
echo " DB backup file $backupFile"
echo ""; echo "=== [`date +%D\ -\ %T`] Stop MySQL Slave Instance ==="
echo -n " Command: "
stopSlave
if [ $? -ne 0 ]; then
echo " ERROR: MySQL slave instance failed to stop !!!"
exit 1
fi
echo ""; echo "=== [`date +%D\ -\ %T`] Backup MySQL Slave Instance ==="
echo -n " Command: "
backupSlave
if [ $? -ne 0 ]; then
echo " ERROR: MySQL slave instance backup FAILED !!!"
exit 1
fi
echo ""; echo "=== [`date +%D\ -\ %T`] Rotate the backups ==="
echo " Keep $toKeep backups in $backupDir"
rotateBackups
if [ $? -ne 0 ]; then
echo " ERROR: Rotating backup files FAILED !!!"
exit 1
fi
echo ""; echo "=== [`date +%D\ -\ %T`] Start MySQL Slave Instance ==="
echo -n " Command: "
startSlave
if [ $? -ne 0 ]; then
echo " ERROR: MySQL slave instance failed to start !!!"
exit 1
fi
echo ""; echo "=== [`date +%D\ -\ %T`] End of $self ==="
exit 0
# ----