forked from y-trudeau/Yves-zfs-tools
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmysql-zfs-snap.sh
executable file
·233 lines (215 loc) · 6.16 KB
/
mysql-zfs-snap.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
222
223
224
225
226
227
228
229
230
231
232
#!/bin/bash
#
# mysql_zfs_snap.sh version v 0.1 2013-06-24
# Yves Trudeau, Percona
# Inspired by zfs_snap of Nils Bausch
#
# take ZFS snapshots with a time stamp
# -h help page
# -d choose default options: hourly, daily, weekly, monthly, yearly
# -f filesystem to snapshot
# -v verbose output
# -p pretend - don't take snapshots
# -S mysql socket
# -H mysql host
# -u user mysql user
# -P mysql password
# -w warmup script
DEBUGFILE="/tmp/mysql-zfs-snap.log"
if [ "${DEBUGFILE}" -a -w "${DEBUGFILE}" -a ! -L "${DEBUGFILE}" ]; then
exec 9>>"$DEBUGFILE"
exec 2>&9
date >&9
echo "$*" >&9
set -x
else
echo 9>/dev/null
fi
export PATH=$PATH:/sbin:/usr/sbin
# Path to binaries used
ZPOOL=`which zpool`
ZFS=`which zfs`
EGREP=`which egrep`
GAWK=`which gawk`
GREP=`which grep`
TAIL=`which tail`
SORT=`which sort`
XARGS=`which xargs`
DATE=`which date`
CUT=`which cut`
TR=`which tr`
MYSQL=`which mysql`
ECHO=`which echo`
# set default values
DEFAULTOPT=
LABELPREFIX="Automatic"
LABEL=`${DATE} +"%FT%H:%M"`
vflag=
pflag=
socket=/tmp/mysql.sock
mysql_hostname=127.0.0.1
mysql_user=root
password=
filesystem=
warmup=
optS="FALSE"
optH="FALSE"
# go through passed options and assign to variables
while getopts 'hd:l:vpu:S:H:P:f:w:' OPTION
do
case $OPTION in
d) DEFAULTOPT="$OPTARG"
;;
l) LABELPREFIX="$OPTARG"
;;
v) vflag=1
;;
p) pflag=1
;;
u) mysql_user="$OPTARG"
;;
f) filesystem="$OPTARG"
;;
S) socket="$OPTARG"
optS="TRUE"
;;
H) mysql_hostname="$OPTARG"
optH="TRUE"
;;
P) password="$OPTARG"
;;
w) warmup="$OPTARG"
;;
h|?) printf "Usage: %s: [-h] [-d <default-preset>] [-v] [-p] [-u <mysql user>] [-P <mysql password>] [-S <mysql socket>] [-H <mysql_hostname>] [-f <zfs filesystem>] [-w <warmup sql script>]\n" $(basename $0) >&2
exit 2
;;
esac
done
#Check Mysql Options
if [ $optS == "TRUE" -a $optH == "TRUE" ]; then
${ECHO} "You cannot use -S and -H at the same time, specify only one"
exit 1
elif [ $optS == "FALSE" -a $optH == "FALSE" ]; then
${ECHO} "No mysql connection specified"
exit 1
fi
#Sanity check
if [ ! -z $filesystem ]; then
checkfs=`${ZFS} list -o name | ${EGREP} -c "^${filesystem}$"`
if [ "$checkfs" -eq "0" ]; then
${ECHO} "Invalid filesystem"
exit 1
else
mountpoint=`${ZFS} list -t filesystem | ${EGREP} -w "^${filesystem}$"| ${GAWK} '{print $5}'`
fi
else
${ECHO} "Missing filesyem (-f)"
${ECHO} "File sytem ${filesystem} not found"
exit 1
fi
# go through possible presets if available
if [ -n "$DEFAULTOPT" ]; then
case $DEFAULTOPT in
hourly) LABELPREFIX="AutoH"
LABEL=`${DATE} +"%FT%H:%M"`
retention=24
;;
daily) LABELPREFIX="AutoD"
LABEL=`${DATE} +"%F"`
retention=7
;;
weekly) LABELPREFIX="AutoW"
LABEL=`${DATE} +"%Y-%U"`
retention=4
;;
monthly)LABELPREFIX="AutoM"
LABEL=`${DATE} +"%Y-%m"`
retention=12
;;
yearly) LABELPREFIX="AutoY"
LABEL=`${DATE} +"%Y"`
retention=10
;;
*) printf 'Default option not specified\n'
exit 2
;;
esac
fi
# do the snapshot dance
if [ "$vflag" ]; then
echo "Calling flush table and doing ${ZFS} snapshot $filesystem@$LABELPREFIX-$LABEL"
fi
#
#Check if we can connect to mysql, this should be done always
#Copied from http://linuxtitbits.blogspot.nl/2011/01/checking-mysql-connection-status.html
#
dbaccess="denied"
until [[ $dbaccess = "success" ]]; do
if [ $optS == "TRUE" ]; then
if [ "$pflag" ]; then
echo "Checking MySQL connection to socket ${socket} "
fi
$MYSQL --user="${mysql_user}" --password="${password}" -S $socket -e exit 2>/dev/null
elif [ $optH == "TRUE" ]; then
if [ "$pflag" ]; then
echo "Checking MySQL connection to host ${mysql_hostname}"
fi
$MYSQL --user="${mysql_user}" --password="${password}" -h $mysql_hostname -e exit 2>/dev/null
fi
dbstatus=`echo $?`
if [ $dbstatus -ne 0 ]; then
if [ $optS == "TRUE" ]; then
echo "Can't connect to MySQL server on ${socket} with user ${mysql_user}"
elif [ $optH == "TRUE" ]; then
echo "Can't connect to MySQL server on ${mysql_hostname} with user ${mysql_user}"
fi
exit 1
else
dbaccess="success"
if [ "$pflag" ]; then
echo "Mysql connection Success!"
fi
fi
done
if [ "$pflag" ]; then
echo "Flushing mysql tables"
else
if [ $optS == "TRUE" ]; then
connection_arg="-S $socket "
elif [ $optH == "TRUE" ]; then
connection_arg="-h $mysql_hostname "
fi
connect_string="$MYSQL -N -n -u $mysql_user -p$password $connection_arg"
$connect_string > /${mountpoint}/snap_master_pos.out <<EOF
flush tables with read lock;
flush logs;
show master status;
\! sync
\! ${ZFS} snapshot $filesystem@$LABELPREFIX-$LABEL
EOF
if [ "$vflag" ]; then
echo "Snapshot taken"
fi
fi
if [ "$warmup" ]; then
cat $warmup | $MYSQL -N -u $mysql_user -p$password $connection_arg &
fi
#DELETE SNAPSHOTS
# adjust retention to work with tail i.e. increase by one
let retention+=1
if [ "$vflag" ]; then
echo "${ZFS} list -t snapshot -o name | ${GREP} $pool@${LABELPREFIX} | ${SORT} -r | ${TAIL} -n +${retention}"
fi
list=`${ZFS} list -t snapshot -o name | ${GREP} $filesystem@${LABELPREFIX} | ${SORT} -r | ${TAIL} -n +${retention}`
if [ ! -z "$pflag" ]; then
if [ "${#list}" -gt 0 ]; then
echo "Delete recursively:"
echo "$list"
else
echo "No snapshots to delete for pool ${pool}"
fi
else
if [ "${#list}" -gt 0 ]; then
$(${ZFS} list -t snapshot -o name | ${GREP} $filesystem@${LABELPREFIX} | ${SORT} -r | ${TAIL} -n +${retention} | ${XARGS} -n 1 ${ZFS} destroy -r)
fi
fi