forked from renard/mysql-backup
/
mysql-backup
executable file
·842 lines (698 loc) · 22.2 KB
/
mysql-backup
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
#!/bin/bash
# mysql-backup --- Backup mysql databases
# Copyright © 2010 Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
# Last changed: 2019-05-23 11:10:20
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
trap atexit EXIT HUP INT QUIT PIPE TERM
function atexit () {
stop_mysqld_for_backup
remove_snapshot
}
VERSION="2.4"
# Default values just in case of...
# Backup options
BACKUP_DIR=/var/backups/mysql
DATABASES="ALL"
IGNORE_DATABASES=("information_schema" "performance_schema")
COMPRESSION=gzip
COMPRESSION_OPTS=
DAILY_RETENTION=7
WEEKLY_DAY="1"
WEEKLY_RETENTION=35
MONTHLY_DAY="01"
MONTHLY_RETENTION=365
HOST=
SUFFIX=
LVM_SNAPSHOT_SIZE="10G"
LVM_EXT="_bkp"
LVCREATE_OPTS="--chunksize=256"
LVREMOVE_OPTS="-f"
TARGET_MOUNT="/tmp/mysql-snapshot"
ZFS_SNAPSHOT="BACKUP"
# time in seconds to try until mysql is up and running.
MYSQL_PING_TIMEOUT=600
# Dump hooks
pre_dump_backup_hook=
post_dump_backup_hook=
pre_dump_restore_hook=
post_dump_restore_hook=
# Snapshot hooks
pre_snapshot_backup_hook=
post_snapshot_backup_hook=
pre_snapshot_backup_lvm_snaphost_hook=
post_snapshot_backup_lvm_snaphost_hook=
pre_snapshot_backup_archive_hook=
post_snapshot_backup_archive_hook=
# mysql / mysqldump default configuration
batch=1
skip_column_names=1
quote_names=1
opt=1
add_drop_database=1
single_transaction=1
# Commands
mysql=$(which mysql)
mysqld=$(which mysqld)
mysqldump=$(which mysqldump)
mysqladmin=$(which mysqladmin)
my_print_defaults=$(which my_print_defaults)
my_print_defaults_opts=mysqld
awk=$(which awk)
sed=$(which sed)
sort=$(which sort)
uniq=$(which uniq)
tail=$(which tail)
run_parts=$(which run-parts)
lvcreate=$(which lvcreate)
lvremove=$(which lvremove)
lvdisplay=$(which lvdisplay)
mount=$(which mount)
umount=$(which umount)
tar=$(which tar)
tar_opts=""
# Useful variables
mysql_cli=
mysqldump_cli=
check_cli=
compression_cli=
extract_cli=
backup_extension=
lvm_snapshot=
datadir_path=
mysql_instance=mysqld
mysqladmin_extra_file=
backup_timestamp=$(date +%F)
this_week_day=$(date +%w)
this_month_day=$(date +%d)
# Nagios checks
NAGIOS_OK=0
NAGIOS_WARNING=1
NAGIOS_CRITICAL=2
NAGIOS_UNKNOWN=3
# Command line options
cli_config=
cli_verbose=
dry_run=
cli_dump=
cli_snapshot=
quiet="> /dev/null 2>&1"
my_pid=$$
RET=
# Load default values
for f in /etc/mysql-backup/default.cnf ~/.mysql-backup.cnf .mysql-backup.cnf; do
if test -r ${f}; then
source ${f}
fi
done
function die() {
echo "$1" >&2
exit "${2:-1}"
}
function die_nagios() {
echo "$1"
exit "${2:-1}"
}
function log() {
d=$(dirname "${backup_log}")
test -d "${d}" || mkdir "${d}"
test -z "${dry_run}" && echo "$(date +'%F %T') $my_pid $*" >> "${backup_log}"
}
function cmd() {
test -n "${dry_run}" && echo "$*"
test -n "${cli_verbose}" && log "$*"
test -z "${dry_run}" && eval "$*"
}
function run_hook() {
hook=$1
if test -n "${!hook}" -a -d "${!hook}"; then
log "Running hook ${hook}"
${run_parts} "${!hook}"
fi
}
function usage() {
exit_code=${1:-1}
cat <<EOF
mysql-backup Version ${VERSION}
Copyright © 2010-2014 Sébastien Gross <seb•ɑƬ•chezwam•ɖɵʈ•org>
$0 [ options | commands ]
Options:
-c|--config CONF The configuration file to use instead of default.
-v|--verbose Run in verbose mode.
--dry-run Only print what would be done.
-h|--help This help screen.
Mandatory commands:
-s|--snapshot Do a LVM snapshot.
-d|--dump Do a text dump backup.
-C|--check Do a nagios compatible check.
At least either snapshot or dump must be provided.
EOF
exit ${exit_code}
}
# Build mysql command line
function build_mysql_cli() {
RET="${mysql}"
# Missing: "databases", "execute", "pipe", "socket"
for v in defaults-extra-file; do
V=${v//-/_}
test -z "${!V}" || RET="${RET} --${v}=${!V}"
done
# Missing: "help", "table", "version"
for v in auto-rehash batch column-names column-type-info comments \
compress debug-check debug-info disable-named-commands force \
html ignore-spaces line-numbers named-commands no-auto-rehash \
no-beep no-named-commands no-pager no-tee one-database quick \
raw reconnect safe-updates i-am-a-dummy secure-auth show-warnings \
sigint-ignore silent skip-column-names skip-line-numbers unbuffered \
verbose vertical wait xml ; do
V=${v//-/_}
test -z "${!V}" || RET="${RET} --${v}"
done
# Missing: "databases", "execute", "pipe", "socket"
for v in character-sets-dir debug default-character-set delimiter host \
local-infile pager password port prompt protocol tee user ; do
V=${v//-/_}
test -z "${!V}" || RET="${RET} --${v}=${!V}"
done
mysql_cli=${RET}
}
# Build mysql dump command line
function build_mysqldump_cli() {
RET="${mysqldump}"
# Missing: "help", "pipe", "table", "version"
for v in add-drop-database add-drop-table add-locks allow-keywords \
comments compact complete-insert compress create-options \
debug-check debug-info delayed-insert delete-master-logs disable-keys \
dump-date events extended-insert flush-logs flush-privileges force \
hex-blob insert-ignore lock-all-tables lock-tables \
no-autocommit no-create-db no-create-info no-data no-set-names opt \
order-by-primary quick quote-names replace routines set-charset \
single-transaction skip-comments skip-opt triggers tz-utc verbose xml ; do
V=${v//-/_}
test -z "${!V}" || RET="${RET} --${v}"
done
# Missing: "database", "ignore-table", "socket", "ssl*"
for v in character-sets-dir compatible debug default-character-set \
fields-terminated-by fields-enclosed-by fields-optionally-enclosed-by \
fields-escaped-by host lines-terminated-by log-error master-data \
password port protocol result-file tab user where; do
V=${v//-/_}
test -z "${!V}" || RET="${RET} --${v}=${!V}"
done
mysqldump_cli=${RET}
}
# list databases to backup
function find_db () {
if test "${DATABASES}" == "ALL"; then
dbs=$(${mysql_cli} --socket=/tmp/${mysql_instance}.sock -e "SHOW DATABASES" | ${sed} 's/ /%/g')
fi
DATABASES=""
# Check is database is in the ignore list, then remove it from DATABASES.
for db in ${dbs}; do
skip_this_db=
for i in ${IGNORE_DATABASES[@]}; do
if test ${db} == ${i}; then
skip_this_db=1
fi
done
if test -z "${skip_this_db}"; then
DATABASES="${DATABASES} ${db}"
fi
done
}
function build_compression_cli () {
local extension_prefix=
case ${command} in
snapshot)
# Snapshots are always tar based
extension_prefix="tar"
;;
dump|backup|restore)
extension_prefix="sql"
;;
esac
case ${COMPRESSION} in
pigz)
# Maybe merge this with gzip
compression_cli="${COMPRESSION}"
extract_cli=$(which zcat)
backup_extension=".gz"
;;
gzip)
compression_cli="${COMPRESSION}"
extract_cli=$(which zcat)
backup_extension=".gz"
;;
bzip2)
compression_cli="${COMPRESSION}"
extract_cli=$(which bzcat)
backup_extension=".bz2"
;;
xz)
compression_cli="${COMPRESSION}"
extract_cli=$(which xzcat)
backup_extension=".xz"
;;
*)
compression_cli=$(which cat)
extract_cli=$(which cat)
backup_extension=""
;;
esac
backup_extension="${extension_prefix}${backup_extension}"
}
function get_backup_file () {
db=${1}
ignore_extention=${2}
if test -n "${ignore_extention}"; then
RET="${db}_${backup_timestamp}"
else
RET="${db}_${backup_timestamp}.${backup_extension}"
fi
}
function do_snapshot_lvm () {
local datadir="${1}"
local mountpoint="${2}"
local datadir_dev=$(df -P ${datadir} | awk '{if(NF==6){print $1}}' \
| xargs ${lvdisplay} -c | cut -d: -f1 | sed 's,^[[:space:]]*/,/,')
if test -z "${datadir}"; then
die "LVM not installed or LVM commands not found or ${datadir} is not in LVM."
fi
lvm_snapshot="${datadir_dev}${LVM_EXT}"
local fs_type=$(mount | sed -n "s,.* on ${mountpoint} type \([^[:space:]]\+\).*,\1,p")
local mount_opts="-o rw,noexec,nosuid,nodev"
if test "${fs_type}" == "xfs"; then
mount_opts="${mount_opts} -o nouuid"
# Do not freeze XFS since LVM change a mount: unknown filesystem
# type 'DM_snapshot_cow' error happens
fi
datadir_path=$(python -c "import os; print(os.path.realpath('${datadir}'))" | sed -e "s,^${mountpoint}/\?,,")
local mysql_query=
if test -n "${dry_run}"; then
cat<<EOF
datadir = ${datadir}
mountpoint = ${mountpoint}
datadir_dev = ${datadir_dev}
datadir_path = ${datadir_path}
lvm_snapshot = ${lvm_snapshot}
fs_type = ${fs_type}
mount_opts = ${mount_opts}
EOF
fi
run_hook pre_snapshot_backup_hook
${lvdisplay} -c | grep -F ${lvm_snapshot} > /dev/null 2>&1
# remove existing mounts
if test $? -eq 0; then
mounted_on=$(df -P ${TARGET_MOUNT} | awk '{if(NF==6){print $NF}}')
if test "${mounted_on}" = "${TARGET_MOUNT}"; then
cmd ${umount} ${TARGET_MOUNT} ${quiet}
if test $? -ne 0; then
msg="ERROR cannot umount ${TARGET_MOUNT} from previous backup"
log "${msg}"
die "${msg}"
fi
fi
cmd ${lvremove} ${LVREMOVE_OPTS} ${lvm_snapshot} ${quiet}
if test $? -ne 0; then
msg="ERROR cannot remove LVM snapshot ${lvm_snapshot} from previous backup"
log "${msg}"
die "${msg}"
fi
fi
run_hook pre_snapshot_backup_lvm_snaphost_hook
mysql_query=$(cat <<EOF
STOP SLAVE;
FLUSH TABLES WITH READ LOCK;
system ${mysql_cli} --socket="${socket}" --column-names -e "SHOW MASTER STATUS\G" > ${datadir}/show-master-status
system ${mysql_cli} --socket="${socket}" --column-names -e "SHOW SLAVE STATUS\G" > ${datadir}/show-slave-status
system ${lvcreate} -s --size ${LVM_SNAPSHOT_SIZE} -n ${lvm_snapshot} ${LVCREATE_OPTS} ${datadir_dev} > /dev/null 2>&1
UNLOCK TABLES;
START SLAVE;
EOF
)
cmd "echo '${mysql_query}' | ${mysql_cli} --socket=\"${socket}\""
run_hook post_snapshot_backup_lvm_snaphost_hook
cmd mkdir -p ${TARGET_MOUNT}
cmd ${mount} ${mount_opts} ${lvm_snapshot} ${TARGET_MOUNT}
}
function do_snapshot_zfs () {
local datadir="${1}"
local mountpoint="${2}"
local datadir_dev=$(df -P ${datadir} | awk '{if(NF==6){print $1}}')
datadir_path=$(python -c "import os; print(os.path.realpath('${datadir}'))" | sed -e "s,^${mountpoint}/\?,,")
cmd zfs get -H type $datadir_dev@${ZFS_SNAPSHOT} $quiet
if test $? -eq 0; then
msg="$datadir_dev@${ZFS_SNAPSHOT} exists. This is not good."
log "$msg"
die "$msg"
fi
run_hook pre_snapshot_backup_hook
run_hook pre_snapshot_backup_zfs_snaphost_hook
mysql_query=$(cat <<EOF
STOP SLAVE;
SET autocommit=0;
FLUSH TABLES WITH READ LOCK;
system ${mysql_cli} --socket="${socket}" --column-names -e "SHOW MASTER STATUS\G" > ${datadir}/show-master-status
system ${mysql_cli} --socket="${socket}" --column-names -e "SHOW SLAVE STATUS\G" > ${datadir}/show-slave-status
system zfs snapshot $datadir_dev@${ZFS_SNAPSHOT} > /dev/null 2>&1
UNLOCK TABLES;
START SLAVE;
EOF
)
cmd "echo '${mysql_query}' | ${mysql_cli} --socket=\"${socket}\""
cmd zfs clone -o mountpoint=$TARGET_MOUNT $datadir_dev@${ZFS_SNAPSHOT} ${datadir_dev}_${ZFS_SNAPSHOT}
if test $? -ne 0; then
msg "Couldn't clone ${datadir_dev}@${ZFS_SNAPSHOT} to ${datadir_dev}_${ZFS_SNAPSHOT}."
log "${msg}"
die "${msg}"
fi
run_hook post_snapshot_backup_zfs_snaphost_hook
run_hook post_snapshot_backup_hook
}
function do_snapshot () {
local _datadir=$(${mysql_cli} --socket="${socket}" -e "SHOW VARIABLES LIKE 'datadir'" \
| sed -e 's,\(^datadir[[:space:]]\+\|/$\),,g')
test -z "${_datadir}" && die "Cannot connect to mysql server"
local datadir=$(readlink ${_datadir})
if test -z "${datadir}"; then
datadir="${_datadir}"
fi
local mountpoint=$(df -P ${datadir} | awk '{if(NF==6){print $NF}}')
local datadir_type=$(df -T -P ${datadir} | awk '{if(NF==7){print $2}}')
case $datadir_type in
zfs) do_snapshot_zfs "$datadir" "$mountpoint";;
*) do_snapshot_lvm "$datadir" "$mountpoint";;
esac
}
function remove_snapshot_lvm () {
if test "$mounted_on" = "${TARGET_MOUNT}"; then
cmd ${umount} ${TARGET_MOUNT} ${quiet}
if test $? -ne 0; then
msg="ERROR cannot umount ${TARGET_MOUNT}"
log "${msg}"
die "${msg}"
fi
fi
if test -n "${lvm_snapshot}"; then
if test -e "${lvm_snapshot}"; then
cmd ${lvremove} ${LVREMOVE_OPTS} ${lvm_snapshot} ${quiet}
if test $? -ne 0; then
msg="ERROR could not remove LVM snapshot ${lvm_snapshot}"
log "${msg}"
die "${msg}"
fi
fi
fi
}
function remove_snapshot_zfs () {
if test "$mounted_on" != "${TARGET_MOUNT}"; then
msg="ERROR $mysql_instance mount is not ${TARGET_MOUNT}"
log "${msg}"
die "${msg}"
fi
mount_dev=$(df -P ${TARGET_MOUNT} | awk '{if(NF==6){print $1}}')
snapshot=$(zfs get -H -o value origin $mount_dev)
cmd zfs umount $mount_dev ${quiet}
cmd zfs destroy $mount_dev ${quiet}
cmd zfs destroy $snapshot ${quiet}
}
function remove_snapshot () {
if test -n "${cli_check}"; then
return
fi
mounted_on=$(df -P ${TARGET_MOUNT} | awk '{if(NF==6){print $NF}}')
mount_type=$(df -T -P ${TARGET_MOUNT} | awk '{if(NF==7){print $2}}')
case $mount_type in
zfs) remove_snapshot_zfs;;
*) remove_snapshot_lvm;;
esac
}
function start_mysqld_for_backup() {
local innodb_data_file=$(${my_print_defaults} ${my_print_defaults_opts} | \
grep innodb_data_file)
rm -f /tmp/${mysql_instance}.log /tmp/${mysql_instance}-stdout.log
local mysql_bkp="${mysqld} --defaults-file=${defaults_file} \
--read-only --skip-networking ${innodb_data_file} \
--datadir=${TARGET_MOUNT}/${datadir_path} \
--skip-slave-start --socket=/tmp/${mysql_instance}.sock \
--pid=/tmp/${mysql_instance}.pid \
--skip-log-bin \
--innodb-flush-method=fsync \
--log-error=/tmp/${mysql_instance}.log > /tmp/${mysql_instance}-stdout.log 2>&1 &"
cmd $mysql_bkp
if test -n "${dry_run}"; then
return
fi
# test if new mysql instance can be reached.
for i in `seq ${MYSQL_PING_TIMEOUT}`; do
sleep 1
log "Try $i/${MYSQL_PING_TIMEOUT}..."
cmd "${mysqladmin} \
--defaults-extra-file='${mysqladmin_extra_file}' \
--socket=/tmp/${mysql_instance}.sock ping > /dev/null 2>&1"
ec=$?
if test $ec -eq 0; then
return
# else
# echo " $i $ec"
fi
# Give mysql a try...
if test $i -gt ${MYSQL_PING_TIMEOUT}; then
if ! test -e /tmp/${mysql_instance}.pid; then
log "PID file not found, could not start MySQL properly."
break
fi
fi
done
log "Could not access to mysql after ${MYSQL_PING_TIMEOUT}s. Something went wrong"
stop_mysqld_for_backup
remove_snapshot
test -z "${dry_run}" && log "DAILY backup failled."
cmd rm -f "${lock_file}"
die "Backup failed"
}
function stop_mysqld_for_backup() {
if ! test -e /tmp/${mysql_instance}.sock; then
return
fi
local mysql_cmd="${mysqladmin} \
--defaults-extra-file="${mysqladmin_extra_file}" \
--socket=/tmp/${mysql_instance}.sock"
local pid=$(cat /tmp/${mysql_instance}.pid)
for i in `seq 60`; do
sleep 1
log "Shutting down mysql: $mysql_cmd shut"
cmd "$mysql_cmd shut"
log "Testing if mysql is still alive: $mysql_cmd ping"
cmd "$mysql_cmd ping >/dev/null 2>&1"
ec=$?
if test $ec -ne 0; then
log "mysql seems to be shut (ec=$ec), checking pid ($pid)..."
for j in `seq 120`; do
cmd "ps -p $pid 2>&1 >/dev/null"
ec=$?
if test $ec -ne 0; then
log "mysql pid $pid not found. This is good, backup can start."
return
fi
log "mysql instance ${mysql_instance} is found for (pid: $pid)."
sleep 1
done
log "mysql instance ${mysql_instance} is still running (pid: $pid) after 120s. Something might be wrong."
fi
done
msg="ERROR Could not stop mysql instance ${mysql_instance}"
log "${msg}"
die "${msg}"
}
# do archive backups
# $1 => root directory for backups
# $2 => backup file name
# $3 => db name (for loggin purposes
function archive_bkps () {
local bkp_dir="$1"
local backup_file="$2"
local db="$3"
# Keep weekly archives
if test "$this_week_day" == "${WEEKLY_DAY}"; then
log "${db} weekly"
cmd ln -f "${bkp_dir}/daily/${backup_file}" "${bkp_dir}/weekly/${backup_file}"
fi
# remove old weekly archives
cmd "find '${bkp_dir}/weekly' -type f -mtime +${WEEKLY_RETENTION} -print0 | \
xargs -0 --no-run-if-empty rm"
# Keep monthly archives
if test "$this_month_day" == "${MONTHLY_DAY}"; then
log "${db} monthly"
cmd ln -f "${bkp_dir}/daily/${backup_file}" "${bkp_dir}/monthly/${backup_file}"
fi
# remove old monthly archives
cmd "find '${bkp_dir}/monthly' -type f -mtime +${MONTHLY_RETENTION} -print0 | \
xargs -0 --no-run-if-empty rm"
# remove old daily archives
cmd "find '${bkp_dir}/daily' -type f -mtime +${DAILY_RETENTION} -print0 | \
xargs -0 --no-run-if-empty rm"
}
function do_binary_backup() {
get_backup_file "${HOST}"
local snapshot_file=${RET}
local bkp_dir="${backup_dir}/binary"
for t in daily weekly monthly; do
cmd mkdir -p "${bkp_dir}/${t}"
done
test -z "${dry_run}" && log "DAILY snapshot start"
cmd "cp /tmp/${mysql_instance}.log ${TARGET_MOUNT}/backup.log"
cmd "cp /tmp/${mysql_instance}-stdout.log ${TARGET_MOUNT}/backup-stdout.log"
cmd "${tar} ${tar_opts} -C ${TARGET_MOUNT}/${datadir_path}/${SUFFIX} -c -f - . | \
${compression_cli} ${compression_opts} > ${bkp_dir}/daily/${snapshot_file}"
if test $? -eq 0; then
test -z "${dry_run}" && log "DAILY binary backup done."
else
test -z "${dry_run}" && log "DAILY binary backup failed."
echo "DAILY binary backup failed."
fi
archive_bkps "${bkp_dir}" "${snapshot_file}" "Snapshot $HOST"
}
function get_logs_dir() {
RET=$(${my_print_defaults} ${my_print_defaults_opts} | ${awk} -F= \
'{if($1~/^--(log_bin|relay_log)/){o[$1]=$2}}END{for(k in o){print o[k]}}')
}
function do_dump_backup() {
get_backup_file "${HOST}"
local snapshot_file=${RET}
local bkp_dir="${backup_dir}/dump"
for t in daily weekly monthly; do
cmd mkdir -p "${bkp_dir}/${t}"
done
find_db
test -z "${dry_run}" && log "DAILY mysqldump backup start."
for db in ${DATABASES}; do
for t in daily weekly monthly; do
mkdir -p "${bkp_dir}/${t}"
done
run_hook pre_dump_backup_hook_${db}
get_backup_file ${db}
backup_file=${RET}
log "Dump table ${db} start"
cmd "${mysqldump_cli} --socket=/tmp/${mysql_instance}.sock \
--databases ${db} |
${compression_cli} > '${bkp_dir}/daily/${backup_file}'"
if test $? -eq 0; then
test -z "${dry_run}" && log "Dump table ${db} finished."
else
test -z "${dry_run}" && log "Dump table ${db} failed."
echo "Dump table ${db} failed."
fi
archive_bkps "${bkp_dir}" "${backup_file}" "Dump $HOST"
run_hook post_dump_backup_hook_${db}
done
# TODO: Backup logs
logs_files=""
get_logs_dir
for d in $RET '*.info' 'show-*-status'; do
logs_files="${logs_files} $d*";
done
get_backup_file "__logs"
backup_file=${RET}
cmd "cd ${TARGET_MOUNT}/${datadir_path} && ${tar} ${tar_opts} -c -f - ${logs_files} | \
${compression_cli} ${compression_opts} > ${bkp_dir}/daily/${backup_file}; cd - 2>&1 >/dev/null"
archive_bkps "${bkp_dir}" "${backup_file}" "Dump $HOST"
test -z "${dry_run}" && log "DAILY mysqldump backup finished."
}
function check_for_locked_queries() {
locked=$(${mysql_cli} --socket="${socket}" -e 'SELECT * FROM information_schema.PROCESSLIST WHERE DB IS NOT NULL AND TIME > 10' | wc -l)
if test ${locked} -gt 0; then
msg="ERROR ${locked} locked queries"
log "${msg}"
if test -z "${dry_run}"; then
die "${msg}"
fi
fi
}
function do_backup () {
check_for_locked_queries
if test -e "${lock_file}"; then
msg="ERROR Lock file ${lock_file} exists since `stat -c %y ${lock_file}`"
log "${msg}"
die "${msg}"
fi
touch "${lock_file}"
test -z "${dry_run}" && log "DAILY backup start."
do_snapshot
if test -n "${cli_snapshot}"; then
start_mysqld_for_backup
stop_mysqld_for_backup
command=snapshot
build_compression_cli
do_binary_backup
fi
if test -n "${cli_dump}"; then
start_mysqld_for_backup
command=dump
do_dump_backup
build_compression_cli
stop_mysqld_for_backup
fi
remove_snapshot
test -z "${dry_run}" && log "DAILY backup finish."
cmd rm -f "${lock_file}"
}
function do_check() {
# Check for lock
if test -e ${lock_file}; then
local _count=$(find ${lock_file} -type f -mtime +1| wc -l)
if test $_count -gt 0; then
local _time=$(stat --printf="%y" ${lock_file} | cut -d. -f 1)
die_nagios "Lock file ${lock_file} is more than 24h: ${_time}" $NAGIOS_CRITICAL
fi
fi
# Check for last backup
local _count=$(find ${backup_dir} -type f -mtime 0| wc -l)
if test $_count -eq 0; then
die_nagios "No backup for last 24h" $NAGIOS_CRITICAL
fi
die_nagios "No problem found" $NAGIOS_OK
}
# Parse the command line
while test $# != 0; do
case "$1" in
--dump|-d) cli_dump=1 ;;
--snapshot|-s) cli_snapshot=1 ;;
--check|-C) cli_check=1 ;;
-c|--config)
test -z "$2" && die "Need a parameter for $1."
cli_config=$2
shift
;;
--dry-run) dry_run=1;;
-v|--verbose) cli_verbose=1 ;;
--help|-h) usage 0 ;;
--) shift; break ;;
-*|*) usage ;;
esac
shift
done
# Finally load configuration file.
if test -r "${cli_config}"; then
source "${cli_config}"
fi
if test -z "${cli_dump}" -a -z "${cli_snapshot}" -a -z "${cli_check}"; then
die "Either --dump or --snapshot or --check must be provided."
fi
backup_dir="${BACKUP_DIR}/${HOST}"
backup_log="/var/log/mysql-backup/${HOST}.log"
lock_file="/tmp/${mysql_instance}_bkp.lock"
if test -n "${cli_check}"; then
do_check
fi
build_mysql_cli
build_mysqldump_cli
do_backup