forked from y-trudeau/Yves-zfs-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysql-zfs-snap.sh
171 lines (157 loc) · 4.19 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
#!/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
# -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`
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_user=root
password=
filesystem=
warmup=
# go through passed options and assign to variables
while getopts 'hd:l:vpu:S: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"
;;
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>] [-f <zfs filesystem>] [-w <warmup sql script>]\n" $(basename $0) >&2
exit 2
;;
esac
done
#Sanity check
if [ ! -z $filesystem ]; then
checkfs=`${ZFS} list -o name | ${EGREP} -c "^${filesystem}$"`
if [ "$checkfs" -eq "0" ]; then
${ECHO} "Invalid filesystem"
exit 1
fi
else
${ECHO} "Missing filesyem (-f)"
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
if [ "$pflag" ]; then
echo "Flushing mysql tables"
else
$MYSQL -N -n -u $mysql_user -p$password -S $socket > /${filesystem}/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 -S $socket &
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