-
Notifications
You must be signed in to change notification settings - Fork 3
/
backup.sh
executable file
·3526 lines (2901 loc) · 107 KB
/
backup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
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
# (The MIT License)
#
# Copyright (c) 2015 - 2019 Mamadou Babaei
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# OmniBackup
# One Script to back them all up
# Version 0.2.0
# ToDo
# * Restore script
# * GnuPG integration
# * SFTP and FTP support
# * Refactoring and code clean-up
# * Any potential bug fixes
readonly START_SECOND=${SECONDS}
readonly APP_NAME="OmniBackup"
readonly APP_VERSION_MAJOR="0"
readonly APP_VERSION_MINOR="2"
readonly APP_VERSION_PATCH="0"
readonly CONFIG_DIRS=( "~/.omnibackup" "/usr/local/etc/omnibackup" "/etc/omnibackup" "" )
CONFIG_FILE="config.json"
readonly LOCK_FILE="/var/run/omnibackup.lock"
readonly SYSLOG_TAG="BACKUP"
readonly DEBUG_TAG="DEBUG"
readonly ERROR_TAG="ERROR"
readonly FATAL_TAG="FATAL"
readonly INFO_TAG="INFO"
readonly SUCCESS_TAG="SUCCESS"
readonly TRACE_TAG="TRACE"
readonly WARNING_TAG="WARNING"
readonly E_1="1"
readonly E_2="2"
readonly E_3="3"
readonly E_4="4"
readonly E_5="5"
readonly E_6="6"
readonly E_7="7"
readonly E_8="8"
readonly E_9="9"
readonly E_1E="1e"
readonly E_2E="2e"
readonly E_3E="3e"
readonly E_4E="4e"
readonly E_5E="5e"
readonly E_6E="6e"
readonly E_7E="7e"
readonly E_8E="8e"
readonly E_9E="9e"
readonly E_128="128"
readonly E_192="192"
readonly E_256="256"
readonly E_ALNUM="alnum"
readonly E_ALPHA="alpha"
readonly E_ASTERISK="*"
readonly E_BLANK="blank"
readonly E_BZIP2="bzip2"
readonly E_CNTRL="cntrl"
readonly E_DIGIT="digit"
readonly E_ERROR="error"
readonly E_FALSE="false"
readonly E_GRAPH="graph"
readonly E_GZIP="gzip"
readonly E_LOWER="lower"
readonly E_LZMA2="lzma2"
readonly E_MD4="md4"
readonly E_MD5="md5"
readonly E_MDC2="mdc2"
readonly E_MYSQL="mysql"
readonly E_NEVER="never"
readonly E_NO="no"
readonly E_NULL="null"
readonly E_PARTIAL="partial"
readonly E_POSTGRES="postgres"
readonly E_PRINT="print"
readonly E_PUNCT="punct"
readonly E_RIPEMD160="ripemd160"
readonly E_SHA="sha"
readonly E_SHA1="sha1"
readonly E_SHA224="sha224"
readonly E_SHA256="sha256"
readonly E_SHA384="sha384"
readonly E_SHA512="sha512"
readonly E_SPACE="space"
readonly E_TRUE="true"
readonly E_WHIRLPOOL="whirlpool"
readonly E_UPPER="upper"
readonly E_XDIGIT="xdigit"
readonly E_YES="yes"
readonly E_SUCCESS="success"
readonly COMPRESSION_ALGORITHM_OPTIONS=( "${E_LZMA2}" "${E_GZIP}" "${E_BZIP2}" "" )
readonly COMPRESSION_LEVEL_LZMA2_OPTIONS=( "${E_1}" "${E_2}" "${E_3}" "${E_4}" "${E_5}" "${E_6}" "${E_7}" "${E_8}" "${E_9}" "${E_1E}" "${E_2E}" "${E_3E}" "${E_4E}" "${E_5E}" "${E_6E}" "${E_7E}" "${E_8E}" "${E_9E}" )
readonly COMPRESSION_LEVEL_GZIP_OPTIONS=( "${E_1}" "${E_2}" "${E_3}" "${E_4}" "${E_5}" "${E_6}" "${E_7}" "${E_8}" "${E_9}" )
readonly COMPRESSION_LEVEL_BZIP2_OPTIONS=( "${E_1}" "${E_2}" "${E_3}" "${E_4}" "${E_5}" "${E_6}" "${E_7}" "${E_8}" "${E_9}" )
readonly COMPRESSION_PRESERVE_PERMISSIONS_OPTIONS=( "${E_YES}" "${E_NO}" )
readonly SECURITY_CHECKSUM_ALGORITHM_OPTIONS=( "${E_MD4}" "${E_MD5}" "${E_MDC2}" "${E_RIPEMD160}" "${E_SHA}" "${E_SHA1}" "${E_SHA224}" "${E_SHA256}" "${E_SHA384}" "${E_SHA512}" "${E_WHIRLPOOL}" )
readonly SECURITY_ENCRYPTION_ENABLE_OPTIONS=( "${E_YES}" "${E_NO}" )
readonly SECURITY_ENCRYPTION_KEY_SIZE_OPTIONS=( "${E_128}" "${E_192}" "${E_256}" )
readonly SECURITY_ENCRYPTION_BASE64_ENCODE_OPTIONS=( "${E_YES}" "${E_NO}" )
readonly SECURITY_ENCRYPTION_RANDOM_PASSPHRASE_PATTERN_OPTIONS=( "${E_ALNUM}" "${E_ALPHA}" "${E_BLANK}" "${E_CNTRL}" "${E_DIGIT}" "${E_GRAPH}" "${E_LOWER}" "${E_PRINT}" "${E_PUNCT}" "${E_SPACE}" "${E_UPPER}" "${E_XDIGIT}" )
readonly REPORTS_ATTACH_PASSPHRASES_OPTIONS=( "${E_YES}" "${E_NO}" )
readonly REMOTE_KEEP_BACKUP_LOCALLY_OPTIONS=( "${E_NEVER}" "${E_ERROR}" "${E_PARTIAL}" "${E_SUCCESS}" )
readonly FILESYSTEM_FOLLOW_SYMLINKS_OPTIONS=( "${E_YES}" "${E_NO}" )
readonly MISC_FOLLOW_SYMLINKS_OPTIONS=( "${E_YES}" "${E_NO}" )
readonly MISC_REMOVE_PATH_WHEN_DONE_OPTIONS=( "${E_YES}" "${E_NO}" )
readonly NUMBER_REGEX="^-?[0-9]+$"
readonly NATURAL_NUMBER_REGEX="^[1-9][0-9]*$"
readonly EMAIL_ADDRESS_REGEX="^[a-z0-9!#\$%&'*+/=?^_\`{|}~-]+(\.[a-z0-9!#$%&'*+/=?^_\`{|}~-]+)*@([a-z0-9]([a-z0-9-]*[a-z0-9])?\.)+[a-z0-9]([a-z0-9-]*[a-z0-9])?\$"
BASENAME="basename"
BZIP2="bzip2"
CALLER="caller"
CAT="cat"
CD="cd"
CHOWN="chown"
CUT="cut"
DATE="date"
DIRNAME="dirname"
DU="du"
ECHO="echo"
EXPR="expr"
FLOCK="flock"
GREP="grep"
GZIP="gzip"
HEAD="head"
HOSTNAME="hostname"
JQ="jq"
LOGGER="logger"
MAIL="mail"
MKDIR="mkdir"
MYSQL_DUMP="mysqldump"
OPENSSL="openssl"
PGSQL_DUMP="pg_dump"
PGSQL_DUMP_ALL="pg_dumpall"
PRINTF="printf"
READLINK="readlink"
RM="rm"
SCP="scp"
SLAPCAT="slapcat"
SSH="ssh"
STRINGS="strings"
SUDO="sudo"
TAR="tar"
TR="tr"
XZ="xz"
function findCommand()
{
local cmd
local ifs="${IFS}"
IFS=':'
for p in ${PATH} ;
do
cmd="${p}/${2}"
eval "${cmd}" --test > /dev/null 2>&1
if [[ $? != 127 && $? != 126 ]] ;
then
IFS="${ifs}"
eval "${1}='${cmd}'"
return 0
fi
done
IFS="${ifs}"
eval "${1}='${2}'"
return 1
}
function blindInitialize()
{
local rc
local ifs="${IFS}"
IFS=' '
local commands
commands=( $( < "${CONFIG_FILE}" jq --raw-output '.command | keys | join(" ")' ) )
rc=$?
IFS="${ifs}"
if [[ ${rc} -eq 0 ]] ;
then
for cmd in "${commands[@]}" ;
do
local cmd_var="${cmd^^}"
case "${cmd}" in
"caller" | "cd")
eval "${cmd_var}='${cmd}'"
continue
;;
esac
findCommand ${cmd_var} "${cmd}"
done
fi
}
function initializeCommands()
{
for cmd in "$@" ;
do
local cmd_var="${cmd^^}"
findCommand ${cmd_var} "${cmd}"
if [[ $? -eq 0 ]] ;
then
if [[ -n "${ECHO}" && -n "${CUT}" && -n "${LOGGER}" ]] ;
then
eval logInfo "Found command \'\${${cmd_var}}\'."
fi
else
logFatal "'${cmd}' command not found!"
fi
done
}
function log()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ -n "$@" ]] ;
then
echo "[$( "${DATE}" )] ${line} <== $@" >> "${LOG_FILE}"
${LOGGER} -s "[${SYSLOG_TAG}] ${line} <== $@"
fi
}
function logDebug()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
log "${DEBUG_TAG} ${line} <== $@"
}
function logError()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
REPORTS_STATUS="${ERROR_TAG}"
log "${ERROR_TAG} ${line} <== $@"
}
function logFatal()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
REPORTS_STATUS="${FATAL_TAG}"
log "${FATAL_TAG} ${line} <== $@"
cleanUp
sendReports
exit 1
}
function logInfo()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
log "${INFO_TAG} ${line} <== $@"
}
function logSuccess()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
log "${SUCCESS_TAG} ${line} <== $@"
}
function logTrace()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
log "${TRACE_TAG} ${line} <== $@"
}
function logWarning()
{
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
log "${WARNING_TAG} ${line} <== $@"
}
function logCommandReturnCode()
{
local cmd_name="${1}"
local cmd_rc="${2}"
local parent_line="${3}"
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ -n "${parent_line}" ]] ;
then
line="${line} <== ${parent_line}"
fi
local cmd="${cmd_name^^}"
local rc_hash="CONF_COMMAND_${cmd}_RETURN_CODES"
local message
eval "message=\${${rc_hash}['rc_${cmd_rc}']}"
if [[ -z ${message} ]] ;
then
eval "message=\${${rc_hash}['rc_any']}"
fi
eval "log \"${line} <== \${${cmd}}: ${message}\""
}
function acquireLock()
{
local temp_file="/var/tmp/${BACKUP_TAG}.tmp"
exec {FD}>"${temp_file}"
exec {FD}>&-
removeFileOrDirectory "${temp_file}"
eval "exec ${FD}>${LOCK_FILE}"
local flag
${FLOCK} -n ${FD} && flag=0 || flag=1
local rc=$?
if [[ ${rc} -eq 0 ]] ;
then
if [[ ${flag} -eq 0 ]] ;
then
readonly LOCK_IS_ACQUIRED="${E_YES}"
trap "releaseLock" EXIT
fi
else
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ -n "${parent_line}" ]] ;
then
line="${line} <== ${parent_line}"
fi
logCommandReturnCode "flock" ${rc} "${line}"
REPORTS_STATUS="${ERROR_TAG}"
fi
return ${flag}
}
function releaseLock()
{
if [[ "${LOCK_IS_ACQUIRED}" != "${E_YES}" ]] ;
then
return
fi
log "Releasing the lock file '${LOCK_FILE}'..."
${FLOCK} -u ${FD}
local rc=$?
if [[ ${rc} -eq 0 ]] ;
then
logInfo "Successfully released the lock."
removeFileOrDirectory ${LOCK_FILE}
rc=$?
if [[ ${rc} -eq 0 ]] ;
then
logInfo "Successfully removed the lock file."
else
logError "Failed to clean-up the lock file!"
fi
else
logCommandReturnCode "flock" ${rc} "${line}"
logError "Failed to drop the lock!"
fi
}
function configParseError()
{
local error="${1}"
local parent_line="${2}"
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ -n "${parent_line}" ]] ;
then
line="${line} <== ${parent_line}"
fi
if [[ -n "${error}" ]] ;
then
logError "${line} <== ${error}"
fi
logFatal "${line} <== JSON parse error in '${CONFIG_FILE}'!"
}
function jsonHas()
{
local key="${2}"
local object="${3}"
local parent_line="${4}"
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ -n "${parent_line}" ]] ;
then
line="${line} <== ${parent_line}"
fi
if [[ ! -f "${CONFIG_FILE}" ]] ;
then
logFatal "${line} <== '${CONFIG_FILE}' does not exists!"
fi
local rcs
local json_has_raw_output
if [[ -z "${object}" ]] ;
then
json_has_raw_output=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "has(\"${key}\")" )
else
local null_object=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "${object}" )
if [[ "${null_object}" == "null" ||
-z "${null_object}" ]] ;
then
eval "${1}='${E_FALSE}'"
return
fi
json_has_raw_output=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "${object} | has(\"${key}\")" )
fi
rcs=( ${PIPESTATUS[*]} )
if [[ ${rcs[0]} -ne 0 || ${rcs[1]} -ne 0 ]] ;
then
if [[ ${rcs[0]} -ne 0 ]] ;
then
logCommandReturnCode "cat" ${rcs[0]} "${line}"
fi
if [[ ${rcs[1]} -ne 0 ]] ;
then
logCommandReturnCode "jq" ${rcs[1]} "${line}"
fi
configParseError "" "${line}"
fi
eval "${1}='${json_has_raw_output}'"
}
function jsonSelect()
{
local object="${2}"
local boolean_expression="${3}"
local parent_line="${4}"
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ -n "${parent_line}" ]] ;
then
line="${line} <== ${parent_line}"
fi
if [[ ! -f "${CONFIG_FILE}" ]] ;
then
logFatal "${line} <== '${CONFIG_FILE}' does not exists!"
fi
local rcs
local json_select_raw_output
if [[ -z "${object}" ]] ;
then
json_select_raw_output=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "select(${boolean_expression})" )
else
local null_object=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "${object}" )
if [[ "${null_object}" == "null"
|| -z "${null_object}" ]] ;
then
eval "${1}=''"
return
fi
json_select_raw_output=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "${object} | select(${boolean_expression})" )
fi
rcs=( ${PIPESTATUS[*]} )
if [[ ${rcs[0]} -ne 0 || ${rcs[1]} -ne 0 ]] ;
then
if [[ ${rcs[0]} -ne 0 ]] ;
then
logCommandReturnCode "cat" ${rcs[0]} "${line}"
fi
if [[ ${rcs[1]} -ne 0 ]] ;
then
logCommandReturnCode "jq" ${rcs[1]} "${line}"
fi
configParseError "" "${line}"
fi
eval "${1}='${json_select_raw_output}'"
}
function jsonLength()
{
local object="${2}"
local parent_line="${3}"
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ -n "${parent_line}" ]] ;
then
line="${line} <== ${parent_line}"
fi
if [[ ! -f "${CONFIG_FILE}" ]] ;
then
logFatal "${line} <== '${CONFIG_FILE}' does not exists!"
fi
local rcs
local json_length_raw_output
if [[ -z "${object}" ]] ;
then
json_length_raw_output=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "length" )
else
local null_object=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "${object}" )
if [[ "${null_object}" == "null" ||
-z "${null_object}" ]] ;
then
eval "${1}=0"
return
fi
json_length_raw_output=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "${object} | length" )
fi
rcs=( ${PIPESTATUS[*]} )
if [[ ${rcs[0]} -ne 0 || ${rcs[1]} -ne 0 ]] ;
then
if [[ ${rcs[0]} -ne 0 ]] ;
then
logCommandReturnCode "cat" ${rcs[0]} "${line}"
fi
if [[ ${rcs[1]} -ne 0 ]] ;
then
logCommandReturnCode "jq" ${rcs[1]} "${line}"
fi
configParseError "" "${line}"
fi
eval "${1}='${json_length_raw_output}'"
}
function jsonValue()
{
local key="${2}"
local treat_null_as_error="${3}"
local parent_line="${4}"
if [[ -z "${treat_null_as_error}" ]] ;
then
treat_null_as_error="${E_YES}"
fi
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ -n "${parent_line}" ]] ;
then
line="${line} <== ${parent_line}"
fi
if [[ ! -f "${CONFIG_FILE}" ]] ;
then
logFatal "${line} <== '${CONFIG_FILE}' does not exists!"
fi
local rcs
local json_value_raw_output
json_value_raw_output=$( "${CAT}" "${CONFIG_FILE}" | ${JQ} --raw-output "${key}" )
rcs=( ${PIPESTATUS[*]} )
if [[ ${rcs[0]} -eq 0 || ${rcs[1]} -eq 0 ]] ;
then
if [[ "${treat_null_as_error}" == "${E_YES}" && "${json_value_raw_output}" == "${E_NULL}" ]] ;
then
configParseError "'${key}' is null!" "${line}"
fi
else
if [[ ${rcs[0]} -ne 0 ]] ;
then
logCommandReturnCode "cat" ${rcs[0]} "${line}"
fi
if [[ ${rcs[1]} -ne 0 ]] ;
then
logCommandReturnCode "jq" ${rcs[1]} "${line}"
fi
configParseError "" "${line}"
fi
eval "${1}='${json_value_raw_output}'"
}
function verifyConfigOption()
{
local option="${1}"
local options=("${!2}")
local rc=1
for item in "${options[@]}" ;
do
if [[ "${option}" == "${item}" ]] ;
then
rc=0
break
fi
done
if [[ ${rc} -ne 0 ]] ;
then
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
configParseError "Invalid option '${option}'!" "${line}"
fi
return ${rc}
}
function changeOwnership()
{
local path="${1}"
local recursive="${2}"
local user="${3}"
local group="${4}"
local flags
if [[ ${recursive} == "${E_YES}" ]] ;
then
flags="-R"
fi
if [[ ( -n "${user}" && -n "${group}" ) ]] ;
then
"${CHOWN}" ${flags} ${user}:${group} "${path}"
rc=$?
elif [[ ( -n "${user}" ) ]] ;
then
"${CHOWN}" ${flags} ${user} "${path}"
rc=$?
elif [[ ( -n "${group}" ) ]] ;
then
"${CHOWN}" ${flags} :${group} "${path}"
rc=$?
fi
if [[ ${rc} -ne 0 ]] ;
then
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
logCommandReturnCode "chown" ${rc} "${line}"
logError "${line} <== Cannot change the ownership of '${path}'!"
return 1
fi
return 0
}
function makeDirectory()
{
local directory="${1}"
local user="${2}"
local group="${3}"
local rc
if [[ ( -n "${user}" && -n "${group}" ) ]] ;
then
"${SUDO}" -u ${user} -g ${group} -H ${MKDIR} -p "${directory}"
rc=$?
elif [[ ( -n "${user}" ) ]] ;
then
"${SUDO}" -u ${user} -g ${group} -H ${MKDIR} -p "${directory}"
else
${MKDIR} -p "${directory}"
rc=$?
fi
if [[ ${rc} -ne 0 ]] ;
then
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
logCommandReturnCode "mkdir" ${rc} "${line}"
logError "${line} <== Cannot create '${directory}' directory!"
return 1
fi
return 0
}
function changeDirectory()
{
local directory="${1}"
${CD} "${directory}"
local rc=$?
if [[ ${rc} -ne 0 ]] ;
then
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
logCommandReturnCode "cd" ${rc} "${line}"
logError "${line} <== Cannot change current path to '${directory}' directory!"
return 1
fi
return 0
}
function removeFileOrDirectory()
{
local path="${1}"
${RM} -rf ${path}
local rc=$?
if [[ ${rc} -ne 0 ]] ;
then
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
logCommandReturnCode "cd" ${rc} "${line}"
logError "${line} <== Cannot remove '${path}'!"
return 1;
fi
return 0
}
function getFileSize()
{
local file_path="${2}"
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ ! -f "${file_path}" ]] ;
then
logError "${line} <== '${file_path}' does not exists!"
eval "${1}='0'"
return 1;
fi
local size
size="~ $( ${DU} -h "$file_path" | ${CUT} -f1 )"
rcs=( ${PIPESTATUS[*]} )
if [[ ${rcs[0]} -ne 0 || ${rcs[1]} -ne 0 ]] ;
then
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ ${rcs[0]} -ne 0 ]] ;
then
logCommandReturnCode "du" ${rcs[0]} "${line}"
REPORTS_STATUS="${ERROR_TAG}"
fi
if [[ ${rcs[1]} -ne 0 ]] ;
then
logCommandReturnCode "cut" ${rcs[1]} "${line}"
REPORTS_STATUS="${ERROR_TAG}"
fi
eval "${1}='0'"
return 1
fi
eval "${1}='${size}'"
return 0
}
function verifyPrivateKeyFormat()
{
local private_key="${1}"
"${OPENSSL}" rsa -in "${private_key}" -check -noout > /dev/null 2>&1
if [[ $? -ne 0 ]] ;
then
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
logCommandReturnCode "openssl" ${rc} "${line}"
REPORTS_STATUS="${ERROR_TAG}"
return 1
fi
return 0
}
function verifyPublicKeyFormat()
{
local public_key="${1}"
"${OPENSSL}" rsa -inform PEM -pubin -in "${public_key}" -noout > /dev/null 2>&1
if [[ $? -ne 0 ]] ;
then
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
logCommandReturnCode "openssl" ${rc} "${line}"
REPORTS_STATUS="${ERROR_TAG}"
return 1
fi
return 0
}
function dumpDatabase()
{
local type="${1}"
local name="${2}"
local dump_file="${3}"
local cmd
local rc
local line=$( "${ECHO}" $( "${CALLER}" 0 ) | "${CUT}" -d " " -f1 )
if [[ "${name}" == "'${E_ASTERISK}'" ]] ;
then
name="${name%\'}"
name="${name#\'}"
fi
case "${type}" in
"${E_POSTGRES}")
if [[ "${name}" != "${E_ASTERISK}" ]] ;
then
if [[ ( -n "${CONF_BACKUP_DATABASE_POSTGRES_USER}" && -n "${CONF_BACKUP_DATABASE_POSTGRES_GROUP}" ) ]] ;
then
"${SUDO}" -u ${CONF_BACKUP_DATABASE_POSTGRES_USER} -g ${CONF_BACKUP_DATABASE_POSTGRES_GROUP} -H ${PGSQL_DUMP} ${name} > ${dump_file}
rc=$?
elif [[ ( -n "${CONF_BACKUP_DATABASE_POSTGRES_USER}" ) ]] ;
then
"${SUDO}" -u ${CONF_BACKUP_DATABASE_POSTGRES_USER} -H ${PGSQL_DUMP} ${name} > ${dump_file}
rc=$?
fi
cmd="pg_dump"
else
if [[ ( -n "${CONF_BACKUP_DATABASE_POSTGRES_USER}" && -n "${CONF_BACKUP_DATABASE_POSTGRES_GROUP}" ) ]] ;
then
"${SUDO}" -u ${CONF_BACKUP_DATABASE_POSTGRES_USER} -g ${CONF_BACKUP_DATABASE_POSTGRES_GROUP} -H ${PGSQL_DUMP_ALL} > ${dump_file}
rc=$?
elif [[ ( -n "${CONF_BACKUP_DATABASE_POSTGRES_USER}" ) ]] ;
then
"${SUDO}" -u ${CONF_BACKUP_DATABASE_POSTGRES_USER} -H ${PGSQL_DUMP_ALL} > ${dump_file}
rc=$?
fi
cmd="pg_dumpall"
fi
;;
"${E_MYSQL}")
if [[ "${name}" != "${E_ASTERISK}" ]] ;
then
if [[ ( -n "${CONF_BACKUP_DATABASE_MYSQL_USER}" && -n "${CONF_BACKUP_OPENDATABASE_MYSQLLDAP_GROUP}" ) ]] ;
then
"${SUDO}" -u ${CONF_BACKUP_DATABASE_MYSQL_USER} -g ${CONF_BACKUP_DATABASE_MYSQL_GROUP} -H ${MYSQL_DUMP} --single-transaction -u ${CONF_BACKUP_DATABASE_MYSQL_INTERNAL_USER} ${name} > ${dump_file}
rc=$?
elif [[ ( -n "${CONF_BACKUP_DATABASE_MYSQL_USER}" ) ]] ;
then
"${SUDO}" -u ${CONF_BACKUP_DATABASE_MYSQL_USER} -H ${MYSQL_DUMP} --single-transaction -u ${CONF_BACKUP_DATABASE_MYSQL_INTERNAL_USER} ${name} > ${dump_file}
rc=$?
else
${MYSQL_DUMP} --single-transaction -u ${CONF_BACKUP_DATABASE_MYSQL_INTERNAL_USER} ${name} > ${dump_file}
rc=$?
fi
cmd="mysqldump"
else
if [[ ( -n "${CONF_BACKUP_DATABASE_MYSQL_USER}" && -n "${CONF_BACKUP_DATABASE_MYSQL_GROUP}" ) ]] ;
then
"${SUDO}" -u ${CONF_BACKUP_DATABASE_MYSQL_USER} -g ${CONF_BACKUP_DATABASE_MYSQL_GROUP} -H ${MYSQL_DUMP} --single-transaction -u ${CONF_BACKUP_DATABASE_MYSQL_INTERNAL_USER} --events --all-databases > ${dump_file}
rc=$?
elif [[ ( -n "${CONF_BACKUP_DATABASE_MYSQL_USER}" ) ]] ;
then
"${SUDO}" -u ${CONF_BACKUP_DATABASE_MYSQL_USER} -H ${MYSQL_DUMP} --single-transaction -u ${CONF_BACKUP_DATABASE_MYSQL_INTERNAL_USER} --events --all-databases > ${dump_file}
rc=$?
else
${MYSQL_DUMP} --single-transaction -u ${CONF_BACKUP_DATABASE_MYSQL_INTERNAL_USER} --events --all-databases > ${dump_file}
rc=$?
fi
cmd="mysqldump"
fi
;;
*)
logError "${line} <== Unknown database type '${type}' for '${name}'!"
return 1
;;
esac
if [[ ${rc} -ne 0 ]] ;
then
logCommandReturnCode "${cmd}" ${rc} "${line}"
logError "${line} <== Dump failed for '${type}::${name}'!"
return 1
fi
return 0
}
function createArchive()
{
local working_dir="${1}"
local path="${2}"
local archive_file="${3}"
local follow_symlinks="${4}"
changeDirectory "${working_dir}"
if [[ $? -ne 0 ]] ;
then
return 1
fi
local rc
local rcs
case "${follow_symlinks}" in
"${E_YES}")
if [[ "${CONF_COMPRESSION_ALGORITHM}" != "" ]] ;
then
${TAR} ${TAR_OPTION_FOLLOW_SYMLINKS} ${TAR_OPTIONS} ${path} | ${ARCHIVE_COMPRESSOR} ${ARCHIVE_COMPRESSOR_OPTIONS} > ${archive_file}
rcs=( ${PIPESTATUS[*]} )
else
${TAR} ${TAR_OPTION_FOLLOW_SYMLINKS} ${TAR_OPTIONS} ${archive_file} ${path}
rc=$?
fi
;;
"${E_NO}")
if [[ "${CONF_COMPRESSION_ALGORITHM}" != "" ]] ;
then
${TAR} ${TAR_OPTIONS} ${path} | ${ARCHIVE_COMPRESSOR} ${ARCHIVE_COMPRESSOR_OPTIONS} > ${archive_file}
rcs=( ${PIPESTATUS[*]} )
else
${TAR} ${TAR_OPTIONS} ${archive_file} ${path}
rc=$?
fi
;;
*)
logError "Invalid option '${follow_symlinks}'!"
return 1
esac
if [[ -n "${rc}" ]] ;