forked from selim13/docker-automysqlbackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
automysqlbackup
executable file
·2255 lines (2053 loc) · 87 KB
/
automysqlbackup
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
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
#
# 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. 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
shopt -s extglob
# BEGIN _flags
let "filename_flag_encrypted=0x01"
let "filename_flag_gz=0x02"
let "filename_flag_bz2=0x04"
let "filename_flag_diff=0x08"
# END _flags
# BEGIN _errors_notifications
let "E=0x00" # no errors
let "N=0x00" # no notifications
let "E_dbdump_failed=0x01"
let "E_backup_local_failed=0x02"
let "E_mkdir_basedir_failed=0x04"
let "E_mkdir_subdirs_failed=0x08"
let "E_perm_basedir=0x10"
let "E_enc_cleartext_delfailed=0x20"
let "E_enc_failed=0x40"
let "E_db_empty=0x80"
let "E_create_pipe_failed=0x100"
let "E_missing_deps=0x200"
let "E_no_basedir=0x400"
let "E_config_backupdir_not_writable=0x800"
let "E_dump_status_failed=0x1000"
let "E_dump_fullschema_failed=0x2000"
let "N_config_file_missing=0x01"
let "N_arg_conffile_parsed=0x02"
let "N_arg_conffile_unreadable=0x04"
let "N_too_many_args=0x08"
let "N_latest_cleanup_failed=0x10"
let "N_backup_local_nofiles=0x20"
# END _errors_notifications
# BEGIN _functions
# @info: Default configuration options.
# @deps: (none)
load_default_config() {
CONFIG_configfile="/etc/automysqlbackup/automysqlbackup.conf"
CONFIG_backup_dir='/var/backup/db'
CONFIG_multicore='yes'
CONFIG_multicore_threads=2
CONFIG_do_monthly="01"
CONFIG_do_weekly="5"
CONFIG_rotation_daily=6
CONFIG_rotation_weekly=35
CONFIG_rotation_monthly=150
CONFIG_mysql_dump_port=3306
CONFIG_mysql_dump_usessl='yes'
CONFIG_mysql_dump_username='root'
CONFIG_mysql_dump_password=''
CONFIG_mysql_dump_host='localhost'
CONFIG_mysql_dump_host_friendly=''
CONFIG_mysql_dump_socket=''
CONFIG_mysql_dump_create_database='no'
CONFIG_mysql_dump_use_separate_dirs='yes'
CONFIG_mysql_dump_compression='gzip'
CONFIG_mysql_dump_commcomp='no'
CONFIG_mysql_dump_latest='no'
CONFIG_mysql_dump_latest_clean_filenames='no'
CONFIG_mysql_dump_max_allowed_packet=''
CONFIG_mysql_dump_single_transaction='no'
CONFIG_mysql_dump_master_data=
CONFIG_mysql_dump_full_schema='yes'
CONFIG_mysql_dump_dbstatus='yes'
CONFIG_mysql_dump_differential='no'
CONFIG_backup_local_files=()
CONFIG_db_names=()
CONFIG_db_month_names=()
CONFIG_db_exclude=( 'information_schema' )
CONFIG_table_exclude=()
CONFIG_mailcontent='stdout'
CONFIG_mail_maxattsize=4000
CONFIG_mail_splitandtar='yes'
CONFIG_mail_use_uuencoded_attachments='no'
CONFIG_mail_address='root'
CONFIG_encrypt='no'
CONFIG_encrypt_password='password0123'
}
# @return: true, if variable is set; else false
isSet() {
if [[ ! ${!1} && ${!1-_} ]]; then return 1; else return 0; fi
}
# @return: true, if variable is empty; else false
isEmpty() {
if [[ ${!1} ]]; then return 1; else return 0; fi
}
# @info: Called when one of the signals EXIT, SIGHUP, SIGINT, SIGQUIT or SIGTERM is emitted.
# It removes the IO redirection, mails any log file information and cleans up any temporary files.
# @args: (none)
# @return: (none)
mail_cleanup () {
removeIO
# if the variables $log_file and $log_errfile aren't set or are empty and both associated files don't exist, skip output methods.
# this might happen if 'exit' occurs before they are set.
if [[ ! -e "$log_file" && ! -e "$log_errfile" ]];then
echo "Skipping normal output methods, since the program exited before any log files could be created."
else
case "${CONFIG_mailcontent}" in
'files')
# Include error log if larger than zero.
if [[ -s "$log_errfile" ]]; then
backupfiles=( "${backupfiles[@]}" "$log_errfile" )
errornote="WARNING: Error Reported - "
fi
temp="$(mktemp "$CONFIG_backup_dir"/tmp/mail_content.XXXXXX)"
# Get backup size
attsize=`du -c "${backupfiles[@]}" | awk 'END {print $1}'`
if (( ${CONFIG_mail_maxattsize} >= ${attsize} )); then
if [[ "x$CONFIG_mail_use_uuencoded_attachments" = "xyes" ]]; then
cat "$log_file" > "$temp"
for j in "${backupfiles[@]}"; do
uuencode "$j" "$j" >> "$temp"
done
mail -s "${errornote} MySQL Backup Log and SQL Files for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address} < "$temp"
else
mutt -s "${errornote} MySQL Backup Log and SQL Files for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" -a "${backupfiles[@]}" -- ${CONFIG_mail_address} < "$log_file"
fi
elif (( ${CONFIG_mail_maxattsize} <= ${attsize} )) && [[ "x$CONFIG_mail_splitandtar" = "xyes" ]]; then
if sPWD="$PWD"; cd "$CONFIG_backup_dir"/tmp && pax -wv "${backupfiles[@]}" | bzip2_compression | split -b $((CONFIG_mail_maxattsize*1000)) - mail_attachment_${datetimestamp}_ && cd "$sPWD"; then
files=("$CONFIG_backup_dir"/tmp/mail_attachment_${datetimestamp}_*)
echo -e "\n\nThe attachments have been split into multiple files.\nUse 'cat mail_attachment_2011-08-13_13h15m_* > mail_attachment_2011-08-13_13h15m.tar.bz2' to combine them and \
'bunzip2 <mail_attachment_2011-08-13_13h15m.tar.bz2 | pax -rv' to extract the content."
for ((j=0;j<"${#files[@]}";j++)); do
if [[ "x$CONFIG_mail_use_uuencoded_attachments" = "xyes" ]]; then
if (( $j = 0 )); then
cat "$log_file" > "$temp"
uuencode "$j" "$j" >> "$temp"
else
uuencode "$j" "$j" > "$temp"
fi
mail -s "${errornote} MySQL Backup Log and SQL Files for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address} < "$temp"
else
mutt -s "${errornote} MySQL Backup Log and SQL Files for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}; Part $((j+1))/${#files[@]}" -a "${files[j]}" -- ${CONFIG_mail_address} < "$log_file"
fi
done
else
cat "$log_file" | mail -s "WARNING! - MySQL Backup exceeds set maximum attachment size on ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address}
fi
else
cat "$log_file" | mail -s "WARNING! - MySQL Backup exceeds set maximum attachment size on ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address}
fi
rm "$temp"
;;
'log')
cat "$log_file" | mail -s "MySQL Backup Log for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address}
[[ -s "$log_errfile" ]] && cat "$log_errfile" | mail -s "ERRORS REPORTED: MySQL Backup error Log for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address}
;;
'quiet')
[[ -s "$log_errfile" ]] && cat "$log_errfile" | mail -s "ERRORS REPORTED: MySQL Backup error Log for ${CONFIG_mysql_dump_host_friendly:-$CONFIG_mysql_dump_host} - ${datetimestamp}" ${CONFIG_mail_address}
;;
*)
if [[ -s "$log_errfile" ]]; then
cat "$log_file"
echo
echo "###### WARNING ######"
echo "Errors reported during AutoMySQLBackup execution.. Backup failed"
echo "Error log below.."
cat "$log_errfile"
else
cat "$log_file"
fi
;;
esac
###################################################################################
# Clean up and finish
[[ -e "$log_file" ]] && rm -f "$log_file"
[[ -e "$log_errfile" ]] && rm -f "$log_errfile"
fi
}
# @params: #month #year
# @deps: (none)
days_of_month() {
m="$1"; y="$2"; a=$(( 30+(m+m/8)%2 ))
(( m==2 )) && a=$((a-2))
(( m==2 && y%4==0 && ( y<100 || y%100>0 || y%400==0) )) && a=$((a+1))
printf '%d' $a
}
# @info: Checks if a folder is writable by creating a temporary file in it and removing it afterwards.
# @args: folder to test
# @return: returns false if creation of temporary file failed or it can't be removed afterwards; else true
# @deps: (none)
chk_folder_writable () {
local temp; temp="$(mktemp "$1"/tmp.XXXXXX)"
if (( $? == 0 )); then
rm "${temp}" || return 1
return 0
else
return 1
fi
}
# @info: bzip2 compression
bzip2_compression() {
var=("$@")
re='^[0-9]*$'
if [[ "x$CONFIG_multicore" = 'xyes' ]]; then
if [[ "x$CONFIG_multicore_threads" != 'xauto' ]] && [[ "x$CONFIG_multicore_threads" =~ $re ]]; then
var=( "-p${CONFIG_multicore_threads}" "${var[@]}" )
fi
pbzip2 "${var[@]}"
else
bzip2 "${var[@]}"
fi
}
# @info: gzip compression
gzip_compression() {
var=("$@")
re='^[0-9]*$'
if [[ "x$CONFIG_multicore" = 'xyes' ]]; then
if [[ "x$CONFIG_multicore_threads" != 'xauto' ]] && [[ "x$CONFIG_multicore_threads" =~ $re ]]; then
var=( "-p${CONFIG_multicore_threads}" "${var[@]}" )
fi
pigz "${var[@]}"
else
gzip "${var[@]}"
fi
}
# @info: Remove date and time information from filename by renaming it.
# @args: filename
# @return: (none)
# @deps: (none)
remove_datetimeinfo () {
mv "$1" "$(echo "$1" | sed -re 's/_[0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}h[0-9]{2}m_(Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday|January|February|March|April|May|June|July|August|September|October|November|December|[0-9]{1,2})//g')"
}
export -f remove_datetimeinfo
# @info: Set time and date variables.
# @args: (none)
# @deps: days_of_month
set_datetime_vars() {
datetimestamp=`date +%Y-%m-%d_%Hh%Mm` # Datestamp e.g 2002-09-21_18h12m
date_stamp=`date +%Y-%m-%d` # Datestamp e.g 2002-09-21
date_day_of_week=`date +%A` # Day of the week e.g. Monday
date_dayno_of_week=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday
date_day_of_month=`date +%e | sed -e 's/^ //'` # Date of the Month e.g. 27
date_month=`date +%B` # Month e.g January
date_weekno=`date +%V | sed -e 's/^0//'` # Week Number e.g 37
year=`date +%Y`
month=`date +%m | sed -e 's/^0//'`
date_lastday_of_last_month=$(days_of_month $(( $month==1 ? 12 : $month-1 )) $(( $month==1 ? ($year-1):$year )) )
date_lastday_of_this_month=$(days_of_month $month $year)
}
# @info: This function is called after data has already been saved. It performs encryption and
# hardlink-copying of files to a latest folder.
# @return: flags
# @deps: load_default_config
files_postprocessing () {
local flags
let "flags=0x00"
let "flags_files_postprocessing_success_encrypt=0x01"
# -> CONFIG_encrypt
[[ "${CONFIG_encrypt}" = "yes" && "${CONFIG_encrypt_password}" ]] && {
if (( $CONFIG_dryrun )); then
printf 'dry-running: openssl enc -aes-256-cbc -e -in %s -out %s.enc -pass pass:%s\n' ${1} ${1} "${CONFIG_encrypt_password}"
else
openssl enc -aes-256-cbc -e -in ${1} -out ${1}.enc -pass pass:"${CONFIG_encrypt_password}"
if (( $? == 0 )); then
if rm ${1} 2>&1; then
echo "Successfully encrypted archive as ${1}.enc"
let "flags |= $flags_files_postprocessing_success_encrypt"
else
echo "Successfully encrypted archive as ${1}.enc, but could not remove cleartext file ${1}."
let "E |= $E_enc_cleartext_delfailed"
fi
else
let "E |= $E_enc_failed"
fi
fi
}
# <- CONFIG_encrypt
# -> CONFIG_mysql_dump_latest
[[ "${CONFIG_mysql_dump_latest}" = "yes" ]] && {
if (( $flags & $flags_files_postprocessing_success_encrypt )); then
if (( $CONFIG_dryrun )); then
printf 'dry-running: cp -al %s.enc %s/latest/\n' "${1}" "${CONFIG_backup_dir}"
else
cp -al "${1}${suffix}.enc" "${CONFIG_backup_dir}"/latest/
fi
else
if (( $CONFIG_dryrun )); then
printf 'dry-running: cp -al %s %s/latest/\n' "${1}" "${CONFIG_backup_dir}"
else
cp -al "${1}" "${CONFIG_backup_dir}"/latest/
fi
fi
}
# <- CONFIG_mysql_dump_latest
return $flags
}
# @info: When called, sets error and notify strings matching their flags. It then goes through all
# collected error and notify messages and displays them.
# @args: (none)
# @return: true if no errors were set, otherwise false
# @deps: log_base2, load_default_config
error_handler () {
errors=(
[0x01]='dbdump() failed.'
[0x02]='Backup of local files failed. This is not this scripts primary objective. Continuing anyway.'
[0x04]="Could not create the backup_dir ${CONFIG_backup_dir}. Please check permissions of the higher directory."
[0x08]='At least one of the subdirectories (daily, weekly, monthly, latest) failed to create.'
[0x10]="The backup_dir ${CONFIG_backup_dir} is not writable AND/OR executable."
[0x20]='Could not remove the cleartext file after encryption. This error did not cause an abort. Remove it manually and check permissions.'
[0x40]='Encryption failed. Continuing without encryption.'
[0x80]='The mysql server is empty, i.e. no databases found. Check if something is wrong. Exiting.'
[0x100]='Failed to create the named pipe (fifo) for reading in all databases. Exiting.'
[0x200]='Dependency programs are missing. Perhaps they are not in $PATH. Exiting.'
[0x400]='No basedir found, i.e. '
[0x800]="${CONFIG_backup_dir} is not writable. Exiting."
[0x1000]='Running of mysqlstatus failed.'
[0x2000]='Running of mysqldump full schema failed.'
)
notify=(
[0x01]="${CONFIG_configfile} was not found - no global config file."
[0x02]="Parsed config file ${opt_config_file}."
[0x04]="Unreadable config file \"${opt_config_file}\""
[0x08]='Supplied more than one argument, ignoring ALL arguments - using default and global config file only.'
[0x10]='Could not remove the files in the latest directory. Please check this.'
[0x20]='No local backup files were set.'
[0x40]=''
[0x80]=''
[0x100]=''
[0x200]=''
[0x400]=''
[0x800]=''
[0x1000]=''
[0x2000]=''
)
local n
local e
n=$((${#notify[@]}-1))
while (( N > 0 )); do
e=$((2**n))
if (( N&e )); then
echo "Note:" ${notify[e]}
let "N-=e"
fi
((n--))
done
unset n;
n=$((${#errors[@]}-1))
if (( E > 0 )); then
while (( E > 0 )); do
e=$((2**n))
if (( E&e )); then
echo "Error:" ${errors[e]}
let "E-=e"
fi
((n--))
done
exit 1
else
exit 0
fi
}
# @info: Packs files in array ${#CONFIG_backup_local_files[@]} into tar file with optional compression.
# @args: archive file without compression suffix, i.e. ending on .tar
# @return: true in case of dry-run, otherwise the return value of tar -cvf
# @deps: load_default_config
backup_local_files () {
if ((! ${#CONFIG_backup_local_files[@]})) ; then
if (( $CONFIG_dryrun )); then
case "${CONFIG_mysql_dump_compression}" in
'gzip')
echo "tar -czvf ${1}${suffix} ${CONFIG_backup_local_files[@]}";
;;
'bzip2')
echo "tar -cjvf ${1}${suffix} ${CONFIG_backup_local_files[@]}";
;;
*)
echo "tar -cvf ${1}${suffix} ${CONFIG_backup_local_files[@]}";
;;
esac
echo "dry-running: tar -cv ${1} ${CONFIG_backup_local_files[@]}"
return 0;
else
case "${CONFIG_mysql_dump_compression}" in
'gzip')
tar -czvf "${1}${suffix}" "${CONFIG_backup_local_files[@]}";
return $?
;;
'bzip2')
tar -cjvf "${1}${suffix}" "${CONFIG_backup_local_files[@]}";
return $?
;;
*)
tar -cvf "${1}${suffix}" "${CONFIG_backup_local_files[@]}";
return $?
;;
esac
fi
else
let "N |= $N_backup_local_nofiles"
echo "No local backup files specified."
fi
}
# @info: Parses the configuration options and sets the variables appropriately.
# @args: (none)
# @deps: load_default_config
parse_configuration () {
# OPT string for use with mysqldump ( see man mysqldump )
opt=( '--quote-names' '--opt' )
# OPT string for use with mysql (see man mysql )
mysql_opt=()
# OPT string for use with mysqldump fullschema
opt_fullschema=( '--all-databases' '--routines' '--no-data' )
# OPT string for use with mysqlstatus
opt_dbstatus=( '--status' )
[[ "${CONFIG_mysql_dump_usessl}" = "yes" ]] && {
opt=( "${opt[@]}" '--ssl' )
mysql_opt=( "${mysql_opt[@]}" '--ssl' )
opt_fullschema=( "${opt_fullschema[@]}" '--ssl' )
opt_dbstatus=( "${opt_dbstatus[@]}" '--ssl' )
}
[[ "${CONFIG_mysql_dump_master_data}" ]] && (( ${CONFIG_mysql_dump_master_data} == 1 || ${CONFIG_mysql_dump_master_data} == 2 )) && { opt=( "${opt[@]}" "--master-data=${CONFIG_mysql_dump_master_data}" );}
[[ "${CONFIG_mysql_dump_single_transaction}" = "yes" ]] && {
opt=( "${opt[@]}" '--single-transaction' )
opt_fullschema=( "${opt_fullschema[@]}" '--single-transaction' )
}
[[ "${CONFIG_mysql_dump_commcomp}" = "yes" ]] && {
opt=( "${opt[@]}" '--compress' )
opt_fullschema=( "${opt_fullschema[@]}" '--compress' )
opt_dbstatus=( "${opt_dbstatus[@]}" '--compress' )
}
[[ "${CONFIG_mysql_dump_max_allowed_packet}" ]] && {
opt=( "${opt[@]}" "--max_allowed_packet=${CONFIG_mysql_dump_max_allowed_packet}" )
opt_fullschema=( "${opt_fullschema[@]}" "--max_allowed_packet=${CONFIG_mysql_dump_max_allowed_packet}" )
}
[[ "${CONFIG_mysql_dump_socket}" ]] && {
opt=( "${opt[@]}" "--socket=${CONFIG_mysql_dump_socket}" )
mysql_opt=( "${mysql_opt[@]}" "--socket=${CONFIG_mysql_dump_socket}" )
opt_fullschema=( "${opt_fullschema[@]}" "--socket=${CONFIG_mysql_dump_socket}" )
opt_dbstatus=( "${opt_dbstatus[@]}" "--socket=${CONFIG_mysql_dump_socket}" )
}
[[ "${CONFIG_mysql_dump_port}" ]] && {
opt=( "${opt[@]}" "--port=${CONFIG_mysql_dump_port}" )
mysql_opt=( "${mysql_opt[@]}" "--port=${CONFIG_mysql_dump_port}" )
opt_fullschema=( "${opt_fullschema[@]}" "--port=${CONFIG_mysql_dump_port}" )
opt_dbstatus=( "${opt_dbstatus[@]}" "--port=${CONFIG_mysql_dump_port}" )
}
# Check if CREATE DATABASE should be included in Dump
if [[ "${CONFIG_mysql_dump_use_separate_dirs}" = "yes" ]]; then
if [[ "${CONFIG_mysql_dump_create_database}" = "no" ]]; then
opt=( "${opt[@]}" '--no-create-db' )
else
opt=( "${opt[@]}" '--databases' )
fi
else
opt=( "${opt[@]}" '--databases' )
fi
# if differential backup is active and the specified rotation is smaller than 21 days, set it to 21 days to ensure, that
# master backups aren't deleted.
if [[ "x$CONFIG_mysql_dump_differential" = "xyes" ]] && (( ${CONFIG_rotation_daily} < 21 )); then
CONFIG_rotation_daily=21
fi
# -> determine suffix
case "${CONFIG_mysql_dump_compression}" in
'gzip') suffix='.gz';;
'bzip2') suffix='.bz2';;
*) suffix='';;
esac
# <- determine suffix
# -> check exclude tables for wildcards
local tmp;tmp=()
local z;z=0
for i in "${CONFIG_table_exclude[@]}"; do
r='^[^*.]+\.[^.]+$'; [[ "$i" =~ $r ]] || { printf 'The entry %s in CONFIG_table_exclude has a wrong format. Ignoring the entry.' "$i"; continue; }
db=${i%.*}
table=${i#"$db".}
r='\*'; [[ "$i" =~ $r ]] || { tmp[z++]="$i"; continue; }
while read -r; do tmp[z++]="${db}.${REPLY}"; done < <(mysql --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${mysql_opt[@]}" --batch --skip-column-names -e "select table_name from information_schema.tables where table_schema='${db}' and table_name like '${table//\*/%}';")
done
for l in "${tmp[@]}"; do echo "exclude $l";done
CONFIG_table_exclude=("${tmp[@]}")
# <-
if ((${#CONFIG_table_exclude[@]})); then
for i in "${CONFIG_table_exclude[@]}"; do
opt=( "${opt[@]}" "--ignore-table=$i" )
done
fi
}
# @info: Backup database status
# @args: archive file without compression suffix, i.e. ending on .txt
# @return: true in case of dry-run, otherwise the return value of mysqlshow
# @deps: load_default_config, parse_configuration
dbstatus() {
if (( $CONFIG_dryrun )); then
case "${CONFIG_mysql_dump_compression}" in
'gzip')
echo "dry-running: mysqlshow --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_dbstatus[@]} | gzip_compression > ${1}${suffix}";
;;
'bzip2')
echo "dry-running: mysqlshow --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_dbstatus[@]} | bzip2_compression > ${1}${suffix}";
;;
*)
echo "dry-running: mysqlshow --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_dbstatus[@]} > ${1}${suffix}";
;;
esac
return 0;
else
case "${CONFIG_mysql_dump_compression}" in
'gzip')
mysqlshow --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_dbstatus[@]}" | gzip_compression > "${1}${suffix}";
return $?
;;
'bzip2')
mysqlshow --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_dbstatus[@]}" | bzip2_compression > "${1}${suffix}";
return $?
;;
*)
mysqlshow --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_dbstatus[@]}" > "${1}${suffix}";
return $?
;;
esac
fi
}
# @info: Backup of the database schema.
# @args: filename to save data to
# @return: true in case of dry-run, otherwise the return value of mysqldump
# @deps: load_default_config, parse_configuration
fullschema () {
if (( $CONFIG_dryrun )); then
case "${CONFIG_mysql_dump_compression}" in
'gzip')
echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_fullschema[@]} | gzip_compression > ${1}${suffix}";
;;
'bzip2')
echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_fullschema[@]} | bzip2_compression > ${1}${suffix}";
;;
*)
echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt_fullschema[@]} > ${1}${suffix}";
;;
esac
return 0;
else
case "${CONFIG_mysql_dump_compression}" in
'gzip')
mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_fullschema[@]}" | gzip_compression > "${1}${suffix}";
return $?
;;
'bzip2')
mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_fullschema[@]}" | bzip2_compression > "${1}${suffix}";
return $?
;;
*)
mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt_fullschema[@]}" > "${1}${suffix}";
return $?
;;
esac
fi
}
# @info: Process a single db.
# @args: subfolder, prefix, midfix, extension, rotation, rotation_divisor, rotation_string, 0/1 (db/dbs), db[, db ...]
process_dbs() {
local subfolder="$1"
local prefix="$2"
local midfix="$3"
local extension="$4"
local rotation="$5"
local rotation_divisor="$6"
local rotation_string="$7"
local multipledbs="$8"
shift 8
local name
local subsubfolder
# only activate differential backup for daily backups
[[ "x$subfolder" != "xdaily" ]] && activate_differential_backup=0 || activate_differential_backup=1
if (( $multipledbs )); then
# multiple dbs
subsubfolder=""
name="all-databases"
else
# single db
subsubfolder="/$1"
name="$@"
fi
[[ -d "${CONFIG_backup_dir}/${subfolder}${subsubfolder}" ]] || {
if (( $CONFIG_dryrun )); then
printf 'dry-running: mkdir -p %s/${subfolder}%s\n' "${CONFIG_backup_dir}" "${subsubfolder}"
else
mkdir -p "${CONFIG_backup_dir}/${subfolder}${subsubfolder}"
fi
}
manifest_file="${CONFIG_backup_dir}/${subfolder}${subsubfolder}/Manifest"
fname="${CONFIG_backup_dir}/${subfolder}${subsubfolder}/${prefix}${name}_${datetimestamp}${midfix}${extension}"
(( $CONFIG_debug )) && echo "DEBUG: process_dbs >> Setting manifest file to: ${manifest_file}"
if (( $multipledbs )); then
# multiple databases
db="all-databases"
else
# single db
db="$1"
fi
if [[ "x$CONFIG_mysql_dump_differential" = "xyes" ]] && [[ "x${CONFIG_encrypt}" != "xyes" ]] && (( $activate_differential_backup )); then
unset manifest_entry manifest_entry_to_check
echo "## Reading in Manifest file"
parse_manifest "$manifest_file"
echo
echo "Number of manifest entries: $(num_manifest_entries)"
echo
# -> generate diff file
let "filename_flags=0x00"
# ## -> get latest differential manifest entry for specified db
# if get_latest_manifest_entry_for_db "$db" 1; then
# pid="${manifest_entry[2]}"
# # filename format: prefix_db_YYYY-MM-DD_HHhMMm_[A-Za-z0-9]{8}(.sql|.diff)(.gz|.bz2)(.enc)
# FileStub=${manifest_entry[0]%.@(sql|diff)*}
# FileExt=${manifest_entry[0]#"$FileStub"}
# re=".*\.enc.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_encrypted"
# re=".*\.gz.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_gz"
# re=".*\.bz2.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_bz2"
# re=".*\.diff.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_diff"
# manifest_latest_diff_entry=("${manifest_entry[@]}")
# else # no entries in manifest
# pid=0
# fi
# ## <- get latest differential manifest entry for specified db
## -> get latest master manifest entry for specified db
# Create a differential backup if a master entry in the manifest exists, it isn't the day we do weekly master backups or the master file we fetched is already from today.
if get_latest_manifest_entry_for_db "$db" 0 && ( (( ${date_dayno_of_week} != ${CONFIG_do_weekly} )) || [[ "${manifest_entry[0]}" = *_$(date +%Y-%m-%d)_* ]] ); then
pid="${manifest_entry[2]}"
# filename format: prefix_db_YYYY-MM-DD_HHhMMm_[A-Za-z0-9]{8}(.sql|.diff)(.gz|.bz2)(.enc)
FileStub="${manifest_entry[0]%.@(sql|diff)*}"
FileExt="${manifest_entry[0]#"$FileStub"}"
re=".*\.enc.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_encrypted"
re=".*\.gz.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_gz"
re=".*\.bz2.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_bz2"
re=".*\.diff.*"; [[ "$FileExt" =~ $re ]] && let "filename_flags|=$filename_flag_diff"
manifest_latest_master_entry=("${manifest_entry[@]}")
else # no entries in manifest
pid=0
fi
## <- get latest master manifest entry for specified db
fi
if [[ "x$CONFIG_mysql_dump_differential" = "xyes" ]] && [[ "x${CONFIG_encrypt}" != "xyes" ]] && (( $activate_differential_backup )) && ((! ($filename_flags & $filename_flag_encrypted) )); then
# the master file is encrypted ... well this just shouldn't happen ^^ not going to decrypt or stuff like that ...at least not today :)
if [[ "x$pid" = "x0" ]]; then
# -> create master backup
cfname="$(mktemp "${fname%.sql}_"XXXXXXXX".sql${suffix}")"
uid="${cfname%.@(diff|sql)*}"
uid="${uid:-8:8}"
case "${CONFIG_mysql_dump_compression}" in
'gzip')
mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" | gzip_compression > "$cfname";
;;
'bzip2')
mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" | bzip2_compression > "$cfname";
;;
*)
mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" > "$cfname";
;;
esac
add_manifest_entry "$manifest_file" "$cfname" "$pid" "$db" && parse_manifest "$manifest_file" && cp -al "$cfname" "${CONFIG_backup_dir}"/latest/ && echo "Generated master backup $cfname" && return 0 || return 1
# <- create master backup
else
cfname="$(mktemp "${fname%.sql}_"XXXXXXXX".diff${suffix}")"
uid="${cfname%.@(diff|sql)*}"
uid=${uid:-8:8}
echo "Creating differential backup to ${manifest_entry[0]}:"
case "${CONFIG_mysql_dump_compression}" in
'gzip')
if (( $filename_flags & $filename_flag_gz )); then
diff <(gzip_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | gzip_compression > "$cfname";
elif (( $filename_flags & $filename_flag_bz2 )); then
diff <(bzip2_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | gzip_compression > "$cfname";
else
diff "${manifest_latest_master_entry[0]}" <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | gzip_compression > "$cfname";
fi
;;
'bzip2')
if (( $filename_flags & $filename_flag_gz )); then
diff <(gzip_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | bzip2_compression > "$cfname";
elif (( $filename_flags & $filename_flag_bz2 )); then
diff <(bzip2_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | bzip2_compression > "$cfname";
else
diff "${manifest_latest_master_entry[0]}" <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") | bzip2_compression > "$cfname";
fi
;;
*)
if (( $filename_flags & $filename_flag_gz )); then
diff <(gzip_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") > "$cfname";
elif (( $filename_flags & $filename_flag_bz2 )); then
diff <(bzip2_compression -dc "${manifest_latest_master_entry[0]}") <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") > "$cfname";
else
diff "${manifest_latest_master_entry[0]}" <(mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@") > "$cfname";
fi
;;
esac
add_manifest_entry "$manifest_file" "$cfname" "$pid" "$db" && parse_manifest "$manifest_file" && cp -al "$cfname" "${manifest_latest_master_entry[0]}" "${CONFIG_backup_dir}"/latest/ && echo "generated $cfname" && return 0 || return 1
fi
# <- generate diff filename
else
cfname="${fname}${suffix}"
if (( $CONFIG_dryrun )); then
case "${CONFIG_mysql_dump_compression}" in
'gzip')
echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt[@]} $@ | gzip_compression > ${cfname}"
;;
'bzip2')
echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt[@]} $@ | bzip2_compression > ${cfname}"
;;
*)
echo "dry-running: mysqldump --user=${CONFIG_mysql_dump_username} --password=${CONFIG_mysql_dump_password} --host=${CONFIG_mysql_dump_host} ${opt[@]} $@ > ${cfname}"
;;
esac
return 0;
else
case "${CONFIG_mysql_dump_compression}" in
'gzip')
mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" | gzip_compression > "${cfname}"
ret=$?
;;
'bzip2')
mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" | bzip2_compression > "${cfname}"
ret=$?
;;
*)
mysqldump --user="${CONFIG_mysql_dump_username}" --password="${CONFIG_mysql_dump_password}" --host="${CONFIG_mysql_dump_host}" "${opt[@]}" "$@" > "${cfname}"
ret=$?
;;
esac
fi
fi
if (( $ret == 0 )); then
echo "Rotating $(( ${rotation}/${rotation_divisor} )) ${rotation_string} backups for ${name}"
if (( $CONFIG_dryrun )); then
find "${CONFIG_backup_dir}/${subfolder}${subsubfolder}" -mtime +"${rotation}" -type f -exec echo "dry-running: rm" {} \;
else
find "${CONFIG_backup_dir}/${subfolder}${subsubfolder}" -mtime +"${rotation}" -type f -exec rm {} \;
fi
files_postprocessing "$cfname"
tmp_flags=$?; var=; (( $tmp_flags & $flags_files_postprocessing_success_encrypt )) && var=.enc
backupfiles=( "${backupfiles[@]}" "${cfname}${var}" )
else
let "E |= $E_dbdump_failed"
echo "dbdump with parameters \"${CONFIG_db_names[@]}\" \"${cfname}\" failed!"
fi
}
# @info: Save stdout and stderr
# @deps: (none)
activateIO() {
###################################################################################
# IO redirection for logging.
# $1 = $log_file, $2 = $log_errfile
#(( $CONFIG_debug )) || {
touch "$log_file"
exec 6>&1 # Link file descriptor #6 with stdout. Saves stdout.
exec > "$log_file" # stdout replaced with file $log_file.
touch "$log_errfile"
exec 7>&2 # Link file descriptor #7 with stderr. Saves stderr.
exec 2> "$log_errfile" # stderr replaced with file $log_errfile.
#}
}
# @info: Restore stdout and stderr redirections.
# @deps: (none)
removeIO() {
exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
exec 2>&7 7>&- # Restore stdout and close file descriptor #7.
}
# @info: Checks directories and subdirectories for existence and activates logging to either
# $CONFIG_backup_dir or /tmp depending on what exists.
# @args: (none)
# @deps: load_default_config, activateIO, chk_folder_writable, error_handler
directory_checks_enable_logging () {
###################################################################################
# Check directories and do cleanup work
checkdirs=( "${CONFIG_backup_dir}"/{daily,weekly,monthly,latest,tmp} )
[[ "${CONFIG_backup_local_files[@]}" ]] && { checkdirs=( "${checkdirs[@]}" "${CONFIG_backup_dir}/backup_local_files" ); }
[[ "${CONFIG_mysql_dump_full_schema}" = 'yes' ]] && { checkdirs=( "${checkdirs[@]}" "${CONFIG_backup_dir}/fullschema" ); }
[[ "${CONFIG_mysql_dump_dbstatus}" = 'yes' ]] && { checkdirs=( "${checkdirs[@]}" "${CONFIG_backup_dir}/status" ); }
tmp_permcheck=0
printf '# Checking for permissions to write to folders:\n'
# "dirname ${CONFIG_backup_dir}" exists?
# Y -> ${CONFIG_backup_dir} exists?
# Y -> Dry-run?
# Y -> log to /tmp, proceed to test subdirs
# N -> check writable ${CONFIG_backup_dir}?
# Y -> proceed to test subdirs
# N -> error: can't write to ${CONFIG_backup_dir}. Exit.
# N -> Dry-run?
# N -> proceed without testing subdirs
# Y -> create directory ${CONFIG_backup_dir}?
# Y -> check writable ${CONFIG_backup_dir}?
# Y -> proceed to test subdirs
# N -> error: can't write to ${CONFIG_backup_dir}. Exit.
# N -> error: ${CONFIG_backup_dir} is not writable. Exit.
# N -> Dry-run?
# Y -> log to /tmp, proceed without testing subdirs
# N -> error: no basedir. Exit.
# -> check base folder
printf 'base folder %s ... ' "$(dirname "${CONFIG_backup_dir}")"
if [[ -d "$(dirname "${CONFIG_backup_dir}")" ]]; then
printf 'exists ... ok.\n'
printf 'backup folder %s ... ' "${CONFIG_backup_dir}"
if [[ -d "${CONFIG_backup_dir}" ]]; then
printf 'exists ... writable? '
if (( $CONFIG_dryrun )); then
printf 'dry-running. Skipping. Logging to /tmp\n'
log_file="/tmp/${CONFIG_mysql_dump_host}-`date +%N`.log"
log_errfile="/tmp/ERRORS_${CONFIG_mysql_dump_host}-`date +%N`.log"
activateIO "$log_file" "$log_errfile"
tmp_permcheck=1
else
if chk_folder_writable "${CONFIG_backup_dir}"; then
printf 'yes. Proceeding.\n'
log_file="${CONFIG_backup_dir}/${CONFIG_mysql_dump_host}-`date +%N`.log"
log_errfile="${CONFIG_backup_dir}/ERRORS_${CONFIG_mysql_dump_host}-`date +%N`.log"
activateIO "$log_file" "$log_errfile"
tmp_permcheck=1
else
printf 'no. Exiting.\n'
let "E |= $E_config_backupdir_not_writable"
error_handler
fi
fi
else
printf 'creating ... '
if (( $CONFIG_dryrun )); then
printf 'dry-running. Skipping.\n'
else
if mkdir -p "${CONFIG_backup_dir}" >/dev/null 2>&1; then
printf 'success.\n'
log_file="${CONFIG_backup_dir}/${CONFIG_mysql_dump_host}-`date +%N`.log"
log_errfile="${CONFIG_backup_dir}/ERRORS_${CONFIG_mysql_dump_host}-`date +%N`.log"
activateIO "$log_file" "$log_errfile"
tmp_permcheck=1
else
printf 'failed. Exiting.\n'
let "E |= $E_mkdir_basedir_failed"
error_handler
fi
fi
fi
else
if (( $CONFIG_dryrun )); then
printf 'dry-running. Skipping. Logging to /tmp\n'
log_file="/tmp/${CONFIG_mysql_dump_host}-`date +%N`.log"
log_errfile="/tmp/ERRORS_${CONFIG_mysql_dump_host}-`date +%N`.log"
activateIO "$log_file" "$log_errfile"
else
printf 'does not exist. Exiting.\n'
let "E |= $E_no_basedir"
error_handler
fi
fi
# <- check base folder
# -> check subdirs
if (( $tmp_permcheck == 1 )); then
(( $CONFIG_dryrun )) || [[ -r "${CONFIG_backup_dir}" && -x "${CONFIG_backup_dir}" ]] || { let "E |= $E_perm_basedir"; error_handler; }
for i in "${checkdirs[@]}"; do
printf 'checking directory "%s" ... ' "$i"
if [[ -d "$i" ]]; then
printf 'exists.\n'
else
printf 'creating ... '
if (( $CONFIG_dryrun )); then
printf 'dry-running. Skipping.\n'
else
if mkdir -p "$i" >/dev/null 2>&1; then
printf 'success.\n'
else
printf 'failed. Exiting.\n'
let "E |= $E_mkdir_subdirs_failed"
error_handler
fi
fi
fi
done
fi
# <- check subdirs
}
# @info: If CONFIG_mysql_dump_latest is set to 'yes', the directory ${CONFIG_backup_dir}"/latest will
# be cleaned.
# @args: (none)
# @deps: load_default_config
cleanup_latest () {
# -> latest cleanup
if [[ "${CONFIG_mysql_dump_latest}" = "yes" ]]; then
printf 'Cleaning up latest directory ... '
if (( $CONFIG_dryrun )); then
printf 'dry-running. Skipping.\n'
else
if rm -f "${CONFIG_backup_dir}"/latest/* >/dev/null 2>&1; then
printf 'success.\n'
else
printf 'failed. Continuing anyway, activating Note-Flag.\n'
let "N |= $N_latest_cleanup_failed"
fi
fi
fi
# <- latest cleanup
}
# @info: Checks for dependencies in form of external programs, that need to be available when running
# this program.
# @args: (none)
# @deps: load_default_config
check_dependencies () {
echo
echo "# Testing for installed programs"