-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmysqlbackup
executable file
·904 lines (823 loc) · 33.9 KB
/
mysqlbackup
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
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
#!/bin/bash
#------------------------------------------------------------------------------#
# vi: set sw=4 ts=4 ai: ("set modeline" in ~/.exrc) #
#------------------------------------------------------------------------------#
# Program : mysqlbackup #
# #
# Author : Ton Kersten The Netherlands #
# #
# Date : 04-07-2010 Time : 14:11 #
# #
# Description : Create a backup tree of MySQL #
# #
# Based on : http://sourceforge.net/projects/automysqlbackup/ #
# Copyright (c) 2002-2003 wipe_out@lycos.co.uk #
# #
# Parameters : The name of the config file to use (Optional) #
# #
# Pre reqs : MySQL and MySQL dump #
# #
# Exit codes : 0 -> OK #
# <> 0 -> !OK #
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# (c) Copyright 2010 by Ton Kersten, The Netherlands #
#------------------------------------------------------------------------------#
# 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 2 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. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program; if not, write to the #
# Free Software Foundation, Inc., #
# 59 Temple Place - Suite 330, #
# Boston, MA 02111-1307, #
# USA #
# #
# See the GNU General Public License for more details. #
# URL: http://www.gnu.org/copyleft/gpl.html. #
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# V e r s i o n i n f o r m a t i o n #
#------------------------------------------------------------------------------#
# $Id:: mysqlbackup 51 2013-06-03 09:00:14 tonk $: #
# $Revision:: 51 $: #
# $Author:: Ton Kersten <github@tonkersten.com> $: #
# $Date:: 2013-06-03 09:03:12 +0200 (Mon, 03 Jun 2013) $: #
# $Hash:: $: #
#------------------------------------------------------------------------------#
# E n d o f v e r s i o n i n f o r m a t i o n #
#------------------------------------------------------------------------------#
#------------------------------------------------------------------------------#
# Determine the program name and the 'running directory' #
#------------------------------------------------------------------------------#
IAM="${0##*/}"
CRD="$( [[ "${0:0:2}" = "./" ]] &&
{ printf "${PWD}/${0#./}"
} || {
printf "${0}"
})"
CRD="${CRD%/*}"
CUR="${PWD}"
#------------------------------------------------------------------------------#
# Save the shell settings #
#------------------------------------------------------------------------------#
SETA=0; [[ ${-} = *a* ]] && SETA=1
SETE=0; [[ ${-} = *e* ]] && SETE=1
SETU=0; [[ ${-} = *u* ]] && SETU=1
SETX=0; [[ ${-} = *x* ]] && SETX=1
#------------------------------------------------------------------------------#
# Set and unset the needed shell settings #
#------------------------------------------------------------------------------#
set +o noclobber # Overwrite existing files, if needed #
set -o nounset # Do not allow uninitialized variables #
set +o errexit # No returncode checking #
#------------------------------------------------------------------------------#
# Define constants #
#------------------------------------------------------------------------------#
VER="1.$(awk '/^# \$Revision::/ { print $3 }' ${0})"
CONFIGFILE="${CRD}/${IAM}.conf"
PATH="${PATH}:/bin:/sbin"
PATH="${PATH}:/usr/bin:/usr/sbin"
PATH="${PATH}:/usr/local/bin:/usr/local/sbin"
PATH="${PATH}:/usr/local/mysql/bin"
DATE=$( date '+%Y-%m-%d_%H%M%S') # Datestamp e.g 2008-06-05_142354 #
DOW=$( date '+%A') # Day of the week e.g. Monday #
DNOW=$( date '+%u') # Day number of the week e.g. 1 to 7 #
DOM=$( date '+%d') # Date of the Month e.g. 27 #
M=$( date '+%B') # Name of the Month e.g January #
W=$( date '+%V') # Week Number e.g 37 #
WN="$(echo "${W}" | sed 's/^0//')" # Week Nymber without leading 0 #
WD="74" # Screen width #
SIN="$(printf "%${WD}s" "")"; SIN="${SIN// /-}" # Single dash line #
DBL="$(printf "%${WD}s" "")"; DBL="${DBL// /=}" # Double dash line #
#------------------------------------------------------------------------------#
# Function to process the defaults #
#------------------------------------------------------------------------------#
default()
{
ACT="${1}" ; shift
DESC="${1}" ; shift
VAR="${1}" ; shift
VAL="\"${*}\""
case ${ACT}
in
set)
eval "${VAR}"="${VAL}"
;;
show)
echo "${VAR} = ${VAL}"
;;
desc)
eval PR="\${${VAR:-${VAL}}}"
PR="\"${PR}\""
printf "%-30s -> %-20s = %s\n" "${DESC}" "${VAR}" "${PR}"
;;
conf)
printf "#%s-#\n# %-${WD}s#\n#%s-#\n%s=%s\n\n" "${SIN}" "${DESC}" "${SIN}" "${VAR}" "${VAL}"
;;
*)
echo "Unknown defaults action: ${ACT}"
exit 1
;;
esac
}
#------------------------------------------------------------------------------#
# all defaults #
#------------------------------------------------------------------------------#
defaults()
{
WHAT="${1}"
default ${WHAT} "Database username " USERNAME "root"
default ${WHAT} "Database password " PASSWORD "password"
default ${WHAT} "Database host " DBHOST "localhost"
default ${WHAT} "Backup owner " OWNER "root"
default ${WHAT} "Backup group " GROUP "root"
default ${WHAT} "Backup databases " DBNAMES "all"
default ${WHAT} "Backup location " BACKUPDIR "/backup/mysql"
default ${WHAT} "Mail logging " MAILCONTENT "stdout"
default ${WHAT} "Maximum attachment size " MAXATTSIZE "4000"
default ${WHAT} "Email address for logging " MAILADDR "root@localhost"
default ${WHAT} "Make monthly backups " DOMONTHLY "yes"
default ${WHAT} "Monthly backup databases " MONTHLY_DBNAMES "all"
default ${WHAT} "Exclude databases " DBEXCLUDE ""
default ${WHAT} "Exclude database tables " TBEXCLUDE ""
default ${WHAT} "Include 'CREATE DATABASE' " CREATE_DATABASE "yes"
default ${WHAT} "Include 'DROP DATABASE' " DROP_DATABASE "yes"
default ${WHAT} "Include 'DROP TABLE' " DROP_TABLE "yes"
default ${WHAT} "Use seperate directories " SEPDIR "yes"
default ${WHAT} "Weekly backups on day (1=Monday) " DOWEEKLY "6"
default ${WHAT} "Use compression (gzip/bzip2) " COMP "gzip"
default ${WHAT} "Compress MySQL communication " COMMCOMP "no"
default ${WHAT} "Keep 'latest' backup copy " LATEST "yes"
default ${WHAT} "Lock tables during backup " LOCK_TABLES "yes"
default ${WHAT} "Include 'Event' database " INC_EVENTS "yes"
default ${WHAT} "Run database checks " RUN_CHECK "yes"
default ${WHAT} "Run database optimize checks " CHECK_OPTIMIZE "yes"
default ${WHAT} "Run database upgrade checks " CHECK_UPGRADE "yes"
default ${WHAT} "Run database repair " CHECK_REPAIR "no"
default ${WHAT} "Maximum packet size " MAX_ALLOWED_PACKET "16MB"
default ${WHAT} "UNIX socket path " SOCKET ""
default ${WHAT} "Pre backup script " PREBACKUP ""
default ${WHAT} "Post backup script " POSTBACKUP ""
return
}
defaults set
#------------------------------------------------------------------------------#
# Check the parameters #
#------------------------------------------------------------------------------#
[[ "${1:-}" = "-h" ]] &&
{ cat <<- @EOF >&2
${IAM} version ${VER}
Syntax: ${IAM} [ -h|-v|-s|-c|-d|<configfile>]
-h Display this help and exit
-v Display the version number and exit
-s Show the defaults and their values
-d Show a description of the defaults and their values
-c Show a default configuration file
-a Show settings after the config file is processed
configfile Use this file as the configuration file
instead of the default.
Default: ${CONFIGFILE}
@EOF
exit 0
}
[[ "${1:-}" = "-v" ]] && { echo "This is '${IAM}' version ${VER}" >&2 ; exit 0 ; }
[[ "${1:-}" = "-s" ]] && { defaults show ; exit 0 ; }
[[ "${1:-}" = "-c" ]] && { defaults conf ; exit 0 ; }
[[ "${1:-}" = "-d" ]] && { defaults desc ; exit 0 ; }
#------------------------------------------------------------------------------#
# Read the configuration #
#------------------------------------------------------------------------------#
showvars="n"
[[ "${1:-}" = "-a" ]] && { showvars="y" ; shift ; }
CONFIGFILE="${1:-${CRD}/${IAM}.conf}"
if [[ -r "${CONFIGFILE}" ]]
then
. "${CONFIGFILE}" || {
echo "Error processing config '${CONFIGFILE}'!" >&2
exit 1
}
else
echo "Could not find config '${CONFIGFILE}'!" >&2
exit 1
fi
[[ "${showvars}" = "y" ]] &&
{ echo "Processed config file ${CONFIGFILE}"
echo "${DBL}"
defaults desc
exit 0
}
#------------------------------------------------------------------------------#
# We need the BACKUPDIR setting #
#------------------------------------------------------------------------------#
if [[ x"${BACKUPDIR:-}" = x"" ]]
then
echo "Config setting 'BACKUPDIR' not found!'" >&2
exit 1
fi
#------------------------------------------------------------------------------#
# Set the constants from the configfile #
#------------------------------------------------------------------------------#
LOGFILE="${BACKUPDIR}/${DBHOST}-${DATE}.log" # Logfile Name #
LOGERR="${BACKUPDIR}/ERRORS_${DBHOST}-${DATE}.log" # Error logfile Name #
BACKUPFILES=""
OPT="--opt" # MySQL dump options #
OPT="${OPT} --quote-names"
# Extra options: --no-create-info #
#------------------------------------------------------------------------------#
# Add compression if this is requested #
#------------------------------------------------------------------------------#
if [[ "${COMMCOMP}" = "yes" ]]
then
OPT="${OPT} --compress"
fi
#------------------------------------------------------------------------------#
# Add a maximum speed if this is requested #
#------------------------------------------------------------------------------#
if [[ x"${MAX_ALLOWED_PACKET}" != x"" ]]
then
OPT="${OPT} --max_allowed_packet=${MAX_ALLOWED_PACKET}"
fi
#------------------------------------------------------------------------------#
# Add drop database option if this is requested #
#------------------------------------------------------------------------------#
if [[ "${DROP_DATABASE}" = "yes" ]]
then
OPT="${OPT} --add-drop-database"
fi
#------------------------------------------------------------------------------#
# Add drop table option if this is requested #
#------------------------------------------------------------------------------#
if [[ "${DROP_TABLE}" = "yes" ]]
then
OPT="${OPT} --add-drop-table"
fi
#------------------------------------------------------------------------------#
# Create required directories #
#------------------------------------------------------------------------------#
[[ ! -e ${BACKUPDIR} ]] && mkdir -p ${BACKUPDIR}
[[ ! -e ${BACKUPDIR}/daily ]] && mkdir -p ${BACKUPDIR}/daily
[[ ! -e ${BACKUPDIR}/weekly ]] && mkdir -p ${BACKUPDIR}/weekly
[[ ! -e ${BACKUPDIR}/monthly ]] && mkdir -p ${BACKUPDIR}/monthly
#------------------------------------------------------------------------------#
# Latest backups requested (Default = yes) #
#------------------------------------------------------------------------------#
if [[ "${LATEST}" = "yes" ]]
then
[[ ! -e ${BACKUPDIR}/latest ]] && mkdir -p ${BACKUPDIR}/latest
#--------------------------------------------------------------------------#
# Remove all files in latest, older than 24 hours #
#--------------------------------------------------------------------------#
find ${BACKUPDIR}/latest -type f -mtime +0 -name \*.sql.gz -exec rm {} \;
else
LATEST="no"
fi
#------------------------------------------------------------------------------#
# IO redirection for logging. #
#------------------------------------------------------------------------------#
touch ${LOGFILE}
exec 6>&1 # Link file descriptor #6 with stdout. #
# Saves stdout. #
exec > ${LOGFILE} # stdout replaced with file ${LOGFILE}. #
touch ${LOGERR}
exec 7>&2 # Link file descriptor #7 with stderr. #
# Saves stderr. #
exec 2> ${LOGERR} # stderr replaced with file ${LOGERR}. #
SUFFIX=""
Center()
{
typeset LINE
#--------------------------------------------------------------------------#
# Cut the line to the maximum line length #
#--------------------------------------------------------------------------#
LINE=$(printf -- "${*:-}" | cut -c 1-$(( ${WD} - 4 )) )
LEN=$(( ( ${WD:-80} - ${#LINE} ) / 2 ))
CHAR="$(printf "%${LEN}s" " ")"
printf -- "${CHAR}${LINE}"
return
}
#------------------------------------------------------------------------------#
# Function for giving a nice datestring #
#------------------------------------------------------------------------------#
now()
{
date '+%Y-%m-%d %H:%M:%S'
}
#------------------------------------------------------------------------------#
# Function for checking the database #
#------------------------------------------------------------------------------#
dbcheck ()
{
CHKOPT=""
[[ "${CHECK_REPAIR}" = "yes" ]] && CHKOPT="${CHKOPT} --auto-repair"
[[ "${CHECK_OPTIMIZE}" = "yes" ]] && CHKOPT="${CHKOPT} --optimize"
[[ "${CHECK_UPGRADE}" = "yes" ]] && CHKOPT="${CHKOPT} --check-upgrade"
mysqlcheck \
--user=${USERNAME} \
--password=${PASSWORD} \
--host=${DBHOST} \
--check \
--all-databases \
${CHKOPT}
return
}
#------------------------------------------------------------------------------#
# Function for dumping the database #
#------------------------------------------------------------------------------#
dbdump ()
{
{
printf '[client]\npassword=%s\n' "${PASSWORD}" |
3<&0 <&4 4<&- mysqldump \
--defaults-file=/dev/fd/3 \
--user=${USERNAME} \
--host=${DBHOST} \
${OPT} \
${1} > ${2}
} 4<&0
RC=${?}
if [[ ${RC} != 0 ]]
then
echo "dbdump gave errorcode ${RC} for database '${1}'" >&2
fi
return ${RC}
}
#------------------------------------------------------------------------------#
# Compression function and copy of the latest #
#------------------------------------------------------------------------------#
compression ()
{
ZIPDIRE="$(dirname ${1})"
ZIPFILE="$(basename ${1})"
CURDIR="${PWD}"
cd ${ZIPDIRE}
RC=0
case ${COMP}
in
gzip)
echo "Compression information for ${ZIPFILE}.zip"
gzip -f "${1}"
RC=${?}
gzip -l "${ZIPFILE}.gz" | sed 's/^ //'
SUFFIX=".gz"
;;
bzip2)
echo "Compression information for ${ZIPFILE}.bz2"
echo "In directory ${ZIPDIRE}"
bzip2 -f -v ${ZIPFILE} 2>&1
RC=${?}
SUFFIX=".bz2"
;;
*)
echo "No compression option set, check advanced settings" >&2
;;
esac
RC=${?}
if [[ ${RC} != 0 ]]
then
echo "compression gave errorcode ${RC} for file '${1}'" >&2
fi
if [[ "${LATEST}" = "yes" ]]
then
cp -p ${ZIPFILE}${SUFFIX} ${BACKUPDIR}/latest/ || \
echo "copy compressed file gave errorcode ${RC} for file '${ZIPFILE}'" >&2
fi
cd ${CURDIR}
return ${RC}
}
#------------------------------------------------------------------------------#
# Run a command before we begin #
#------------------------------------------------------------------------------#
if [[ x"${PREBACKUP}" != x"" ]]
then
echo "${DBL}"
echo "Prebackup command output."
echo
eval ${PREBACKUP}
echo
echo "${DBL}"
echo
fi
#------------------------------------------------------------------------------#
# Create a OPT string for the given options #
#------------------------------------------------------------------------------#
SEPDIR="${SEPDIR}"
CREATE_DATABASE="${CREATE_DATABASE}"
if [[ "${SEPDIR}" = "yes" ]]
then
#--------------------------------------------------------------------------#
# Check if CREATE DATABASE should be included in the dump #
#--------------------------------------------------------------------------#
if [[ "${CREATE_DATABASE}" = "no" ]]
then
OPT="${OPT} --no-create-db"
else
OPT="${OPT} --databases"
fi
else
OPT="${OPT} --databases"
fi
[[ "${LOCK_TABLES}" = "no" ]] && OPT="${OPT} --skip-lock-tables"
[[ "${INC_EVENTS}" = "yes" ]] && OPT="${OPT} --events"
#------------------------------------------------------------------------------#
# Hostname for LOG information #
#------------------------------------------------------------------------------#
if [[ "${DBHOST}" = "localhost" ]]
then
HOST=$(hostname)
if [[ x"${SOCKET}" != x"" ]]
then
OPT="${OPT} --socket=${SOCKET}"
fi
else
HOST=${DBHOST}
fi
HD="${HOST} - $(now)"
#------------------------------------------------------------------------------#
# Backup all databases? #
#------------------------------------------------------------------------------#
if [[ "${DBNAMES}" = "all" ]]
then
#--------------------------------------------------------------------------#
# Get all databases #
#--------------------------------------------------------------------------#
DBNAMES="$( { \
printf '[client]\npassword=%s\n' "${PASSWORD}" |
3<&0 <&4 4<&- mysql \
--defaults-file=/dev/fd/3 \
--user=${USERNAME} \
--host=${DBHOST} \
--batch \
--skip-column-names \
-e "show databases" | \
grep -v 'lost+found' | \
sed 's/ /%/g' | \
sort
} 4<&0 )"
#--------------------------------------------------------------------------#
# Strip the DBs that are excluded #
#--------------------------------------------------------------------------#
for exclude in ${DBEXCLUDE}
do
DBNAMES=$(echo ${DBNAMES} | sed "s/\b${exclude}\b//g")
done
MONTHLY_DBNAMES=${DBNAMES}
fi
MONTHLY_DBNAMES=${MONTHLY_DBNAMES}
#------------------------------------------------------------------------------#
# Backup all database tables? #
#------------------------------------------------------------------------------#
if [[ x"${TBEXCLUDE}" != x"" ]]
then
for table in ${TBEXCLUDE}
do
OPT="${OPT} --ignore-table=${table}"
done
fi
#------------------------------------------------------------------------------#
# Here we go! #
#------------------------------------------------------------------------------#
case ${DOWEEKLY}
in
1) DOWEEKLYDAY="Monday" ;;
2) DOWEEKLYDAY="Tuesday" ;;
3) DOWEEKLYDAY="Wednesday" ;;
4) DOWEEKLYDAY="Thursday" ;;
5) DOWEEKLYDAY="Friday" ;;
6) DOWEEKLYDAY="Saturday" ;;
7) DOWEEKLYDAY="Sunday" ;;
esac
echo "${DBL}"
echo
Center "MySQLBackup version ${VER}"
echo
echo
echo "${DBL}"
echo "Backup of database server : ${HOST}"
echo "Backup settings"
echo " Start time and date : $(now)"
echo " Backup directory : ${BACKUPDIR}"
echo " Use seperate directories : ${SEPDIR}"
echo " Exclude databases : ${DBEXCLUDE}"
echo " Exclude database tables : ${TBEXCLUDE}"
echo " Weekly backup on day : ${DOWEEKLYDAY}"
echo " Backup owner and group : ${OWNER} - ${GROUP}"
echo " Compression : ${COMP}"
echo " Communication compression : ${COMMCOMP}"
echo " Create latest backup : ${LATEST}"
echo " Lock database during backup : ${LOCK_TABLES}"
echo " Include 'Event' database : ${INC_EVENTS}"
echo " Add 'create database' command : ${CREATE_DATABASE}"
echo " Add 'drop database' command : ${DROP_DATABASE}"
echo " Add 'drop table' command : ${DROP_TABLE}"
echo " Run MySQL check command : ${RUN_CHECK}"
[[ "${RUN_CHECK}" = "yes" ]] &&
{
echo " Optimize : ${CHECK_OPTIMIZE}"
echo " Upgrade : ${CHECK_OPTIMIZE}"
echo " Repair : ${CHECK_REPAIR}"
}
set ${DBNAMES}
echo " Backup databases : ${1}"
shift
for db in ${*:-}
do
echo " ${db}"
done
echo
echo "${DBL}"
echo "Backup start time -> $(now)"
echo "${DBL}"
#------------------------------------------------------------------------------#
# Run MySQL check when requested #
#------------------------------------------------------------------------------#
[[ "${RUN_CHECK}" = "yes" ]] &&
{
echo
echo "${SIN}"
echo "Running database check"
echo "${SIN}"
dbcheck
echo "${SIN}"
}
#------------------------------------------------------------------------------#
# Test if seperate DB backups are required #
#------------------------------------------------------------------------------#
if [[ "${SEPDIR}" = "yes" ]]
then
#--------------------------------------------------------------------------#
# Monthly full backup of all databases #
#--------------------------------------------------------------------------#
if [[ x"${DOMONTHLY}" = x"yes" ]]
then
if [[ ${DOM} = "01" ]]
then
for MDB in ${MONTHLY_DBNAMES}
do
#--------------------------------------------------------------#
# Prepare ${DB} for backup #
#--------------------------------------------------------------#
MDB="$(echo ${MDB} | sed 's/%/ /g')"
DIR="${BACKUPDIR}/monthly/${MDB}"
[[ ! -e "${DIR}" ]] && mkdir -p ${DIR}
echo "Monthly backup of ${MDB}..."
NAME="${DIR}/${MDB}_${DATE}.${M}.${MDB}.sql"
dbdump "${MDB}" "${NAME}"
compression "${NAME}"
BACKUPFILES="${BACKUPFILES} ${NAME}${SUFFIX}"
echo "${SIN}"
done
fi
fi
for DB in ${DBNAMES}
do
#----------------------------------------------------------------------#
# Prepare ${DB} #
#----------------------------------------------------------------------#
DB="$(echo ${DB} | sed 's/%/ /g')"
DAY="${BACKUPDIR}/daily/${DB}"
WKL="${BACKUPDIR}/weekly/${DB}"
#----------------------------------------------------------------------#
# Create Seperate directory for each DB #
#----------------------------------------------------------------------#
[[ ! -e "${DAY}" ]] && mkdir -p ${DAY}
[[ ! -e "${WKL}" ]] && mkdir -p ${WKL}
#----------------------------------------------------------------------#
# Weekly Backup #
#----------------------------------------------------------------------#
if [[ ${DNOW} = ${DOWEEKLY} ]]
then
echo "Weekly backup of database '${DB}'"
echo "Rotating 5 weeks backups..."
if [[ "${WN}" -le 05 ]]
then
REMW=$(( 48 + ${WN} ))
elif [[ "${WN}" -lt 15 ]]
then
REMW=0$(( ${WN} - 5 ))
else
REMW=$(( ${WN} - 5 ))
fi
for rem in ${WKL}/${DB}_week.${REMW}.*
do
echo " Removing -> $(basename ${rem})"
done
rm -f ${WKL}/${DB}_week.${REMW}.*
echo
NAME="${WKL}/${DB}_week.${W}.${DATE}.sql"
dbdump "${DB}" "${NAME}"
compression "${NAME}"
BACKUPFILES="${BACKUPFILES} ${NAME}${SUFFIX}"
echo "${SIN}"
else
echo "Daily backup of database '${DB}'"
echo "Rotating last weeks backup..."
find ${DAY} -type f -mtime +0 -name \*.${DOW}.sql.gz | \
sort | \
while read rem
do
echo " Removing -> $(basename ${rem})"
rm -f ${rem}
done
echo
NAME="${BACKUPDIR}/daily/${DB}/${DB}_${DATE}.${DOW}.sql"
dbdump "${DB}" "${NAME}"
compression "${NAME}"
BACKUPFILES="${BACKUPFILES} ${NAME}${SUFFIX}"
echo "${SIN}"
fi
done
else
#--------------------------------------------------------------------------#
# Monthly full backup of all databases #
#--------------------------------------------------------------------------#
if [[ x"${DOMONTHLY}" = x"yes" ]]
then
if [[ ${DOM} = "01" ]]
then
echo "Monthly full backup of (${MONTHLY_DBNAMES})..."
NAME="${BACKUPDIR}/monthly/${DATE}.${M}.all-databases.sql"
dbdump "${MONTHLY_DBNAMES}" "${NAME}"
compression "${NAME}"
BACKUPFILES="${BACKUPFILES} ${NAME}${SUFFIX}"
echo "${SIN}"
fi
fi
#--------------------------------------------------------------------------#
# Weekly backup #
#--------------------------------------------------------------------------#
if [[ ${DNOW} = ${DOWEEKLY} ]]
then
echo "Weekly backup of databases '${DBNAMES}'"
echo
echo "Rotating 5 weeks backups..."
if [[ "${WN}" -le 05 ]]
then
REMW=$(( 48 + ${WN} ))
elif [[ "${WN}" -lt 15 ]]
then
REMW=0$(( ${WN} - 5 ))
else
REMW=$(( ${WN} - 5 ))
fi
find ${BACKUPDIR}/weekly -type f -mtime +0 -name week.${REMW}\*.sql.gz | \
sort | \
while read rem
do
echo " Removing -> $(basename ${rem})"
rm -f ${rem}
done
echo
NAME="${BACKUPDIR}/weekly/week.${W}.${DATE}.sql"
dbdump "${DBNAMES}" "${NAME}"
compression "${NAME}"
BACKUPFILES="${BACKUPFILES} ${NAME}${SUFFIX}"
echo "${SIN}"
else
echo "Daily backup of databases '${DBNAMES}'"
echo
echo "Rotating last weeks backup..."
find ${BACKUPDIR}/daily -type f -mtime +0 -name \*.${DOW}\*.sql.gz | \
sort | \
while read rem
do
echo " Removing -> $(basename ${rem})"
rm -f ${rem}
done
echo
echo
NAME="${BACKUPDIR}/daily/${DATE}.${DOW}.sql"
dbdump "${DBNAMES}" "${NAME}"
compression "${NAME}"
BACKUPFILES="${BACKUPFILES} ${NAME}${SUFFIX}"
echo "${SIN}"
fi
fi
echo
echo "${DBL}"
echo "Backup end at $(now)"
echo "${DBL}"
#------------------------------------------------------------------------------#
# Show statistics #
#------------------------------------------------------------------------------#
echo "Total disk space used for backup storage...."
echo "Size - Location $(du -hs "${BACKUPDIR}")"
#------------------------------------------------------------------------------#
# Run command when we're done #
#------------------------------------------------------------------------------#
if [[ x"${POSTBACKUP}" != x"" ]]
then
echo "${DBL}"
echo "Postbackup command output."
echo
eval ${POSTBACKUP}
echo
echo "${DBL}"
fi
#------------------------------------------------------------------------------#
# Backup is done! #
#------------------------------------------------------------------------------#
if [[ -s "${LOGERR}" ]]
then
MESG="MySQLBackup ready with errors at $(now)"
else
MESG="MySQLBackup version ${VER} done at $(now)"
fi
echo "${DBL}"
echo
Center "${MESG}"
echo
echo
echo "${DBL}"
#------------------------------------------------------------------------------#
# Clean up IO redirection #
#------------------------------------------------------------------------------#
exec 1>&6 6>&-
exec 1>&7 7>&-
#------------------------------------------------------------------------------#
# Set the directory and file rights if allowed #
#------------------------------------------------------------------------------#
[[ $(id -u) = 0 ]] &&
{ chown -R ${OWNER}:${GROUP} ${BACKUPDIR}
chmod 700 ${BACKUPDIR}/*
find ${BACKUPDIR} -type d -print0 | xargs -0 chmod 700
find ${BACKUPDIR} -type f -print0 | xargs -0 chmod 600
}
#------------------------------------------------------------------------------#
# Mail the results if requested #
#------------------------------------------------------------------------------#
case "${MAILCONTENT}"
in
files)
if [[ -s "${LOGERR}" ]]
then
#------------------------------------------------------------------#
# Include error log if is larger than zero. #
#------------------------------------------------------------------#
BACKUPFILES="${BACKUPFILES} ${LOGERR}"
ERRORNOTE="WARNING: Error reported - "
fi
#----------------------------------------------------------------------#
# Get backup size #
#----------------------------------------------------------------------#
ATTSIZE=$(du -c ${BACKUPFILES} | grep "[[:digit:][:space:]]total$" | sed 's/\s*total//')
if [[ ${MAXATTSIZE} -ge ${ATTSIZE} ]]
then
BACKUPFILES=$(echo "${BACKUPFILES}" | sed -e "s# # -a #g")
mutt -s "${ERRORNOTE} MySQL backup log and SQL files for ${HD}" ${BACKUPFILES} ${MAILADDR} < ${LOGFILE}
else
cat "${LOGFILE}" | mail -s "WARNING! - MySQL backup exceeds set maximum attachment size on ${HD}" ${MAILADDR}
fi
;;
log)
cat "${LOGFILE}" | mail -s "MySQL backup log for ${HD}" ${MAILADDR}
if [[ -s "${LOGERR}" ]]
then
cat "${LOGERR}" | mail -s "ERRORS REPORTED: MySQL backup error log for ${HD}" ${MAILADDR}
fi
;;
quiet)
if [[ -s "${LOGERR}" ]]
then
cat "${LOGERR}" | mail -s "ERRORS REPORTED: MySQL backup error log for ${HD}" ${MAILADDR}
cat "${LOGFILE}" | mail -s "MySQL Backup Log for ${HD}" ${MAILADDR}
fi
;;
*)
if [[ -s "${LOGERR}" ]]
then
cat "${LOGFILE}"
echo
echo "###### WARNING ######"
echo "Errors reported during MySQL backup execution. Backup failed"
echo "Error log below"
cat "${LOGERR}"
else
cat "${LOGFILE}"
fi
;;
esac
#------------------------------------------------------------------------------#
# Check the error log #
#------------------------------------------------------------------------------#
if [[ -s "${LOGERR}" ]]
then
STATUS=1
else
STATUS=0
#--------------------------------------------------------------------------#
# Clean up the logfiles if no error occurred #
#--------------------------------------------------------------------------#
rm -f ${LOGFILE}
rm -f ${LOGERR}
fi
#------------------------------------------------------------------------------#
# That's all, folks! #
#------------------------------------------------------------------------------#
exit ${STATUS}