forked from automydumper/automydumper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automydumper
executable file
·250 lines (207 loc) · 5.74 KB
/
automydumper
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#!/bin/bash
export PATH="$PATH:/bin:/usr/bin"
# Defaults
backup_root_dir='/var/backups/automydumper'
backup_dir_format='+%F'
pre_dir='/usr/share/automydumper/pre.d'
post_dir='/usr/share/automydumper/post.d'
compress=1
keep=3
mysql_user='root'
mysql_password=''
mysql_host='localhost'
mysql_socket=
threads=4
mydumper_opts='--less-locking --use-savepoints'
enabled=1
mail_rcpts=''
mail_from="automydumper@${HOSTNAME}"
# Override defaults if config file is present
if [ -f '/etc/automydumper.cfg' ]; then
source /etc/automydumper.cfg
fi
backup_dir_format="${backup_dir_format/ /}"
formatted_date="$(date ${backup_dir_format})"
backup_dir="${backup_root_dir}/${formatted_date}"
version="1.1.0"
# What versions of Mydumper do we support?
mydumper_versions='0.9'
mydumper_opts="${mydumper_opts} --triggers --events --routines -o ${backup_dir} -v 3 --threads ${threads}"
# Compression support
[ $compress -eq 1 ] && mydumper_opts="$mydumper_opts --compress"
options=$(getopt -o h --long help -- "$@")
[ $? -eq 0 ] || {
echo "Incorrect options provided"
exit 1
}
eval set -- "$options"
while true; do
case "$1" in
--help|-h)
echo ""
echo "Automydumper ${version}"
echo ""
exit
;;
--)
shift
break
;;
esac
shift
done
function mysql_credentials()
{
echo -n "--user=${mysql_user} "
if [ ! -z "${mysql_password}" ]; then
echo -n "--password=${mysql_password} "
fi
echo "${mysql_host}" | grep -E "(localhost|127.0.0.1)" > /dev/null
if [ $? -eq 0 ]; then
[ ${mysql_socket} ] && echo -n "--socket=${mysql_socket}" || echo -n ''
else
echo -n "--host=${mysql_host}"
fi
}
function check_mysql()
{
which mysql > /dev/null
if [ $? -ne 0 ]; then
write_state "critical" "MySQL client not installed."
exit 1
fi
}
function check_mydumper()
{
which mydumper > /dev/null
if [ $? -ne 0 ]; then
write_state "critical" "Mydumper not installed."
exit 1
fi
}
function check_mysql_connection()
{
mysql $(mysql_credentials) -e 'select VERSION()' > /dev/null
if [ $? -ne 0 ]; then
write_state "critical" 'Wrong MySQL credentials or hostname/socket.'
exit 1
fi
}
function check_mydumper_version()
{
for mydumper_version in $mydumper_versions; do
mydumper --version | grep -i "mydumper $mydumper_version" > /dev/null && return 0
done
write_state "critical" "Unsupported version of Mydumper found."
echo "Supported versions: $mydumper_versions"
exit 1
}
function check_backup_root_dir()
{
if [ "$(basename ${backup_root_dir})" != "automydumper" ]; then
write_state "critical" "Backup_root_dir top directory should be 'automydumper'"
exit 1
fi
}
function create_directories()
{
mkdir -p $(dirname "${backup_dir}") "${pre_dir}" "${post_dir}"
}
function cleanup_backups()
{
if [ ${keep} -eq 0 ]; then
echo -e "Cleanup of old backups disabled because of keep=0\n"
return
fi
while [ "$(find "${backup_root_dir}" -type f -name "metadata" | wc -l)" -gt "${keep}" ]; do
oldest=$(find "${backup_root_dir}" -type f -name "metadata" -printf '%Cs %h\n' | sort | head -n 1 | awk '{ print $2}')
echo "Deleting ${oldest}"
rm -rf "${oldest}"
done
echo ""
}
function write_state()
{
mkdir -p "/var/lib/automydumper"
cat > /var/lib/automydumper/state << EOF
status="$1"
description="$2"
started="${curdate} ${curtime}"
EOF
chmod +r /var/lib/automydumper/state
echo $2
echo ""
}
function run_pre_commands()
{
for cmd in $(find ${pre_dir} -type f -executable); do
echo "Executing pre command: '${cmd}'"
${cmd} || ( write_state "critical" "Pre-backup command failed."; exit 1)
echo ""
done
}
function run_post_commands()
{
for cmd in $(find ${post_dir} -type f -executable); do
echo "Executing post command: '${cmd}'"
${cmd} || ( write_state "critical" "Post-backup command failed."; exit 1)
echo ""
done
}
function mail_report()
{
if [ ! -z "${mail_rcpts}" ]; then
echo "Sending mail report to '${mail_rcpts}' from '${mail_from}'"
grep "^${formatted_date} " "${log_file}" | mailx -s "Backup for $(hostname) ${1}" -r "${mail_from}" "${mail_rcpts}"
else
echo -e "Not configured to send a mail report.\n"
fi
echo ""
}
function verify_backups()
{
if grep "^${formatted_date} " "${log_file}" | grep WARNING > /dev/null; then
write_state "warning" "Backup completed with warnings."
mail_report 'Warning'
else
write_state "ok" "Backup completed successfully."
echo -e "Saved to ${backup_dir}.\n"
mail_report 'OK'
fi
}
function symlink_latest()
{
rm -f "${backup_root_dir}/latest"
ln -sf "${backup_dir}" "${backup_root_dir}/latest"
}
# Stop the script when it's disabled by the user.
if [ $enabled -eq 0 ]; then
echo "Script is disabled. Enable by removing enabled=0 from /etc/default/automydumper. Quitting."
exit 0
fi
trap "{ write_state critical 'Backup failed.' ; mail_report 'failed'; exit 255; }" ERR
exec > >(sed "/^\s*$/d;s#^#automydumper #" | tee -i >(exec logger -i))
exec 2>&1
check_backup_root_dir
check_mysql
check_mydumper
check_mydumper_version
check_mysql_connection
create_directories
echo -e "\nAutomydumper ${version} using $(mydumper --version | grep -i "mydumper")\n"
mydumper_opts="${mydumper_opts} $(mysql_credentials)"
# Run pre_command if it is defined
run_pre_commands
# Remove old backup first
rm -rf "${backup_dir}"
mydumper ${mydumper_opts}
if [ $? -ne 0 ]; then
echo "Backup failed."
rm -rf "${backup_dir}"
exit 1
fi
symlink_latest
# Run post_command if it is defined
run_post_commands
cleanup_backups
verify_backups