-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsyncbackup3.sh
executable file
·1520 lines (1253 loc) · 41 KB
/
rsyncbackup3.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
#!/bin/sh
#
# rsyncbackup3.sh - RSYNC BACKUP SCRIPT
# Copyright (C) 2011-2017 Jonas Kvinge
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# MAKE SURE YOU UNDERSTAND THIS SCRIPT BEFORE YOU USE IT!
# I AM NOT RESPONSIBLE FOR LOSS OF DATA!
#
# To backup daily and name the backups after the current days
# you can create a crontab entry to run the days you want and
# set:
# backuparchives="Day$(date '+%w')"
#
# Settings "backuparchives" to "" will rsync the files directly
# into the path specified in backuplocation.
#
# rsync options:
#
# Verbose: -v
# Archive: -a (Same as -rlptgoD)
# Recursive: -r
# Checksum: -c
# Time: -t
# Dry run: -n
# Delete: --delete
# Relative path names: -R
#
backupversion="3.2.1"
backupconfig="/etc/sysconfig/rsyncbackup3"
#
# All variables below will be overwritten by the configuration file.
# To configure this script, copy what you want to change below and put it in a separate file, ie: /etc/sysconfig/rsyncbackup3
#
backuphost="$(hostname -s)" # <--- Hostname of this machine --->
backuplockfiledir="$HOME" # <--- Lockfile to prevent multiple occurrences of the script running at the same time --->
backuplockfilettl=172800
backuplogfile="/var/log/rsyncbackup3.log" # <--- Logfile, this will be the same output as sent in e-mail reports --->
backuplogdir="/tmp" # <--- Directory to temporary store rsync log files --->
backupdate="$(date '+%Y%m%d-%H%M%S')" # <--- Datestamp --->
backupdebug=0 # <--- Debug output, starting script with -d will set this to 1 --->
backuparchives=3 # <--- Number of archives --->
#backuparchivedir="Day$(date '+%w')"
backuparchivedir="$(date '+%Y%m%d')" # <--- Random archive dir -->
# If backuparchivedirs is set, it will use only these directories instead of renamed directories
backuparchivedirs=
#backuparchivedirs="backup1 backup2 backup3" # <--- Subdirs of backupdir, the script will loop through these and update the oldest archive --->
# Arguments to rsync command
# Dry run
backuprsyncargs="-vaRn --itemize-changes --delete --delete-excluded"
# Archive
#backuprsyncargs="-vaR --itemize-changes --delete --delete-excluded"
# Checksum
#backuprsyncargs="-vacR --itemize-changes --delete --delete-excluded"
backupsshport=
#backupsshport="22" # <--- Use a different SSH port for ssh/sshfs/rsync commands --->
backupidfile=".RSYNCBACKUPID" # <--- File touched to identify that the directory is a valid archive --->
backuptsfile="RSYNCBACKUPTS" # <--- Timestamp file placed in the archive directory to track which directory was used last time, HIDDEN FILE WONT WORK! --->
backupid=$(date '+%Y%m%d%H%M%S')
backupts=$(date '+%Y%m%d%H%M%S')
backupemailfrom="nobody" # <--- From address to send backup reports from --->
backupemailsuccess="root" # <--- Where to send backup report, set to "0" for no report --->
backupemailfailure="root" # <--- Where to send backup report, set to "0" for no report --->
backupmntdir="$HOME/tmp-mnt-${RANDOM}" # <--- Where to mount --->
backupgziplog=1 # <--- Compress logfile --->
backupsourceretries=3 # <--- Times to retry if there is an error --->
backupsourceretrydelay=3 # <--- Time to sleep between each retry attempt if there is an error --->
backupsourceretryttl=60 # <--- Maximum time to attempt backup source before giving up --->
backupsourceconnectdelay=10 # <--- Time to sleep between each connect attempt if there is an error --->
backupsourceconnectttl=172800 # <--- How long time in seconds to wait for a host to come online before giving up --->
# One or more sources that you want to backup.
# Syntax are, Local: /home or SSH(SFTP): host:/home
# Use comma to separate each file/directory to backup.
# Files/directories specified with '-' in front will be exceptions.
backupsources="\
server: /etc,/var,/srv,/home,/usr/local,/tmp,-/mnt/backup
pc: /etc,/var,/srv,/home,/usr/local,/mnt/datadisk,/tmp,-/mnt/backup
"
# One or more destinations where you want to backup files to.
# This can be locally, via ssh/sftp, or via smb.
# Syntax are, Local: /mnt/backup, SSH(SFTP): host:/mnt/backup, SMB: smb://user:pass@host/dir
# If the directory starts with /mnt or /media and is not mounted it will be automatically mounted and umounted.
backupdestinations="\
$backuphost:/mnt/backup/rbackup
"
# Enter files to exclude here, like temp files and sockets
backupexclude="\
/lost+found
/tmp
/dev
/proc
/run
/sys
/media
/var/cache
/var/tmp
/var/run
/var/spool/postfix/private
/var/spool/postfix/public
/var/lib/named/lib
/var/lib/named/proc
/var/lib/named/var
/var/lib/named/dev
/var/lib/mysql/mysql.sock
/var/lib/ntp/proc
/var/lib/ntp/dev
/var/lib/dhcp/dev
/var/lib/dhcp/proc
/var/lib/dhcp6/dev
/var/lib/dhcp6/proc
/var/lib/docker
/home/*/.xsession-errors*
/home/*/.local/share/Trash
/home/*/.local/share/akonadi
/home/*/.cache
/home/*/.ccache
/home/*/.pulse-cookie
/home/*/.pulse
/home/*/.config/google-chrome/Default/Local Storage/*
/home/*/.config/google-chrome/Cookies*
/home/*/.config/google-chrome/History*
/home/*/.config/google-chrome/Default/Application Cache
/home/*/.mozilla/firefox/*/Cache*
"
# Variables - Don't change this.
cmds_required="which tput cat cut tr sed grep wc bc cp mv rm mkdir rmdir touch md5sum date hostname mutt mount umount mountpoint fusermount ssh sshfs rsync"
cmds_prefergnu="ls cat cut grep sed tr touch"
cmds_requiregnu="ls"
TPUT=dummy
MUTT=dummy
LS="ls"
SED="sed"
GREP="grep"
TOUCH="touch"
# Functions below here
dummy() { echo "">/dev/null; }
timestamp() { TS=$(date '+%d/%m-%Y %H:%M:%S'); }
print() {
timestamp
if [ -t 1 ] && ! [ "$color" = "" ]; then
$TPUT bold
$TPUT setaf "$color"
fi
echo "[$TS] $*"
if [ -t 1 ]; then
$TPUT sgr0
fi
color=0
}
errorprint() {
timestamp
if [ -t 1 ]; then
$TPUT bold
$TPUT setaf 1
fi
echo "[$TS] ERROR: $*" >&2
if [ -t 1 ]; then
$TPUT sgr0
fi
color=0
}
nnprint() {
timestamp
if [ -t 1 ]; then
$TPUT bold
$TPUT setaf 0
fi
echo "$n" "[$TS] $* $c"
if [ -t 1 ]; then
$TPUT bold
$TPUT setaf 1
fi
}
failprint() {
if [ -t 1 ]; then
$TPUT bold
$TPUT setaf 1
fi
}
successprint() {
if [ -t 1 ]; then
$TPUT bold
$TPUT setaf 2
fi
echo "Done."
if [ -t 1 ]; then
$TPUT sgr0
fi
}
startprint() { failprint; }
stopprint() {
if [ -t 1 ]; then
$TPUT sgr0
fi
}
log() {
timestamp
log="$log
[$TS] $*"
logall="$logall
[$TS] $*"
if [ "$haveconfig" -eq 1 ]; then
echo "[$TS] $*" >>$backuplogfile
fi
}
debuglog() {
timestamp
debuglog="$debuglog
[$TS] [DEBUG] $*"
}
status() { statusprint "$@"; statuslog "$@"; }
statusnn() { nnprint "$@"; statuslog "$@"; }
result() { resultprint "$@"; resultlog "$@"; }
info() { infoprint "$@"; infolog "$@"; }
warn() { warnprint "$@"; warnlog "$@"; }
error() { errorprint "$@"; errorlog "$@"; }
debug() {
if [ "$backupdebug" = "1" ]; then
debugprint "$@"
debuglog "$@"
fi
}
statusprint() { color=0; print "$@"; }
resultprint() { color=2; print "$@"; }
infoprint() { color=7; print "$@"; }
warnprint() { color=3; print "WARN: $*"; }
debugprint() { color=4; print "[DEBUG] $*"; }
statuslog() { log "$@"; }
resultlog() { log "$@"; }
infolog() { log "$@"; }
warnlog() { log "WARN: $*"; }
errorlog() { log "ERROR: $*"; }
prnlognts() {
echo "$@"
log="$log
$*"
logall="$logall
$*"
}
run() {
info "- $*"
"$@"
ret=$?
if ! [ $? = 0 ]; then
error "$* failed!"
fi
return $ret
}
main() {
backup_init "$@"
}
backup_logo() {
$TPUT bold
$TPUT setaf 7
#echo " _ __ _ _ _ _ "
#echo " |_) (_ \_/ |\ | / |_) /\ / |/ | | |_) "
#echo " | \ __) | | \| \_ |_) /--\ \_ |\ |_| | "
echo " ___ _____ ___ _ ___ ___ _ ___ _ ___ _ ___"
echo " | _ \/ __\ \ / / \| |/ __| _ ) /_\ / __| |/ / | | | _ \\"
echo " | /\__ \\\\ V /| .\` | (__| _ \/ _ \ (__| ' <| |_| | _/"
echo " |_|_\|___/ |_| |_|\_|\___|___/_/ \_\___|_|\_\\\\___/|_|"
$TPUT sgr0
$TPUT setaf 7
echo " v$backupversion"
echo ""
$TPUT sgr0
}
backup_init() {
lockfile=0
mntdir=0
haveconfig=0
backuprun=0
backup_logo
info "Command: $0 $*"
backup_chkcmds
# Find out how to echo without newline
c=''
n=''
if [ "$(eval echo -n 'a')" = "-n a" ] ; then
c='\c'
else
n='-n'
fi
# Parse parameters
script=$(echo "$0" | $SED 's/.*\///g')
while getopts c:hvdR o
do case "$o" in
c) backupconfig="$OPTARG";;
h) prnlognts "Usage: $script -hvdR -c <config>"; exit 0;;
v) prnlognts "$script v$backupversion"; exit 0;;
d) backupdebug=1;;
R) backuprun=1;;
\?) prnlognts "ERROR: Unknown option: $script -h for help."; backup_failure_report_all; exit 1;;
esac
done
# Reset $@
shift "$(echo "$OPTIND"-1 | bc)"
# Load configuration
backup_loadconf
# Initialize logfile
$TOUCH $backuplogfile || { backup_failure_report_all; exit_failure; }
echo "$log" >>$backuplogfile || { backup_failure_report_all; exit_failure; }
# Check if script is already running
backuplockfile="${backuplockfiledir}/RSYNCBACKUP-LOCKFILE-$(echo "$backupconfig" | $SED 's/\//-/g' | $SED 's/ /-/g' | $SED 's/\./-/g').lock"
backuplockfile=$(echo "$backuplockfile" | $SED 's/--/-/g')
which lockfile >/dev/null 2>&1
if [ $? -eq 0 ] ; then
lockfile -r 0 -l $backuplockfilettl "$backuplockfile"
if ! [ $? -eq 0 ] ; then
error "Script is already running. If this is incorrect, remove: $backuplockfile."
backup_failure_report_all
exit_failure
fi
else
if [ -f "$backuplockfile" ]; then
error "Script is already running. If this is incorrect, remove: $backuplockfile."
backup_failure_report_all
exit_failure
fi
$TOUCH "$backuplockfile" || { backup_failure_report_all; exit_failure; }
fi
lockfile=1
mkdir -p "$backupmntdir" || { backup_failure_report_all; exit_failure; }
mntdir=1
# Loop sources
backup_source_loop
if [ "$sources_failure" -gt 0 ] ; then
exit_failure
else
exit_success
fi
}
backup_chkcmds() {
# Make sure $cmds_required contain all commands
if ! [ "$cmds_requiregnu" = "" ]; then
for cmd1 in $cmds_requiregnu
do
found=0
for cmd2 in $cmds_required
do
if [ "$cmd1" = "$cmd2" ]; then
found=1
break
fi
done
if ! [ "$found" -eq 1 ]; then
cmds_required="$cmds_required $cmd1"
fi
done
fi
if ! [ "$cmds_prefergnu" = "" ]; then
for cmd1 in $cmds_prefergnu
do
found=0
for cmd2 in $cmds_required
do
if [ "$cmd1" = "$cmd2" ]; then
found=1
break
fi
done
if ! [ "$found" -eq 1 ]; then
cmds_required="$cmds_required $cmd1"
fi
done
fi
# Check that we got all the needed commands
for cmd in $cmds_required
do
which "$cmd" >/dev/null 2>&1
if ! [ $? -eq 0 ] ; then
error "Missing \"${cmd}\" command!"
backup_failure_report_all
exit_failure
fi
prefergnu=0
requiregnu=0
if ! [ "$cmds_prefergnu" = "" ]; then
for cmd2 in $cmds_prefergnu
do
if [ "$cmd" = "$cmd2" ]; then
prefergnu=1
break
fi
done
fi
if ! [ "$cmds_requiregnu" = "" ]; then
for cmd2 in $cmds_requiregnu
do
if [ "$cmd" = "$cmd2" ]; then
requiregnu=1
break
fi
done
fi
found=0
gnu=0
CMD=$(echo "$cmd" | tr /a-z/ /A-Z/)
paths=$(echo "$PATH" | $SED 's/:/ /g')
for path in $paths
do
cmdpath=$path/$cmd
if ! [ -x "$cmdpath" ]; then
continue
fi
if ! [ "$found" -eq 1 ]; then
eval "${CMD}"="$cmdpath"
fi
found=1
if ! [ "$prefergnu" -eq 1 ]; then
break
fi
# Check that we got the GNU version of the command
result=$($cmdpath --version 2>&1 | head -n1)
if ! [ $? -eq 0 ] ; then
continue
fi
echo "$result" | $GREP "^.* (GNU coreutils) .*$" >/dev/null 2>&1
if ! [ $? -eq 0 ] ; then
echo "$result" | $GREP "^.* (GNU ${cmd}) .*$" >/dev/null 2>&1
if ! [ $? -eq 0 ] ; then
continue
fi
fi
gnu=1
eval "${CMD}"="$cmdpath"
break
done
if ! [ "$found" -eq 1 ]; then
error "Missing \"${cmd}\" command!"
backup_failure_report_all
exit_failure
fi
if [ "$requiregnu" -eq 1 ] && ! [ "$gnu" -eq 1 ]; then
error "Missing GNU version of \"${cmd}\" command!"
backup_failure_report_all
exit_failure
fi
done
}
backup_loadconf() {
# Locate configuration file
if [ "$backupconfig" = "" ] ; then
backupconfig="/etc/sysconfig/rsyncbackup3"
fi
# Check for existence of needed config file and read it
if ! [ -f "$backupconfig" ]; then
error "Backup configuration file \"$backupconfig\" does not exist!"
backup_failure_report_all
exit_failure
fi
if ! [ -r "$backupconfig" ]; then
error "Unable to read configuration file \"$backupconfig\"."
backup_failure_report_all
exit_failure
fi
info "Configuration: $backupconfig"
. $backupconfig || {
error "Failed to load configuration file \"$backupconfig\"."
backup_failure_report_all
exit_failure
}
# Check that we got the configuration needed
for i in \
"backupconfig" \
"backuphost" \
"backuplockfiledir" \
"backuplockfilettl" \
"backuplogfile" \
"backupdate" \
"backuprsyncargs" \
"backupidfile" \
"backuptsfile" \
"backupid" \
"backupts" \
"backupemailfrom" \
"backupemailsuccess" \
"backupemailfailure" \
"backupsources" \
"backupdestinations" \
"backupexclude" \
"backuparchives" \
"backuparchivedir" \
"backupmntdir" \
"backuplogdir" \
"backupgziplog" \
"backupsourceretries" \
"backupsourceretrydelay" \
"backupsourceretryttl" \
"backupsourceconnectdelay" \
"backupsourceconnectttl"
do
if [ "${!i}" = "" ]; then
error "Missing configuration variable \"$i\" in $backupconfig!"
backup_failure_report_all
exit_failure
fi
done
# Set variables
haveconfig=1
backupexcludeoneline=$(echo "$backupexclude" | tr '\n' ' ' | $SED -e 's/^ //g' | $SED -e 's/ / /g')
#backupsources=$(echo "$backupsources" | $SED -e 's/ //g' | $SED -e 's/\t//g')
#backupsources=$(echo "$backupsources" | tr '\n' ' ' | $SED -e 's/^ //g' | $SED -e 's/ / /g')
backupsources=$(echo "$backupsources" | tr -d ' ' | tr -d '\t')
backupsources=$(echo "$backupsources" | tr '\n' ' ')
#backupdestinations=$(echo "$backupdestinations" | $SED -e 's/ //g' | $SED -e 's/\t//g')
#backupdestinations=$(echo "$backupdestinations" | tr '\n' ' ' | $SED -e 's/^ //g' | $SED -e 's/ / /g')
backupdestinations=$(echo "$backupdestinations" | tr -d ' ' | tr -d '\t')
backupdestinations=$(echo "$backupdestinations" | tr '\n' ' ')
sources_success=0
sources_failure=0
destinations_success=0
destinations_failure=0
debug "backupsources: $backupsources"
debug "backupdestinations: $backupdestinations"
debug "backupexcludeoneline: $backupexcludeoneline"
if ! [ "$backupsshport" == "" ]; then
backup_ssh_args="-p $backupsshport"
fi
}
backup_source_loop() {
sources_success=0
sources_failure=0
sources_total=$(echo "$backupsources" | wc -w)
sources_finished=0
while [ "$sources_finished" -lt "$sources_total" ]
do
source_index=0
for backupsource in $backupsources
do
source_index=$(echo $source_index + 1 | bc)
sourcehost=
#sourceuser=
sourcefiles=
sourcemountpoint=
sourceexcludes=
log=${source_log[$source_index]}
if ! [ "${source_finished[$source_index]}" = "" ] && [ "${source_finished[$source_index]}" -eq 1 ]; then
continue
fi
if [ "${source_count[$source_index]}" = "" ]; then
source_count[$source_index]=1
sourcecount=${source_count[$source_index]}
source_timestart[$source_index]=$(date +%s)
source_destloop[$source_index]=0
else
source_count[$source_index]=$(echo ${source_count[$source_index]} + 1 | bc)
sourcecount=${source_count[$source_index]}
fi
ret=
echo "$backupsource" | $GREP '^\/' >/dev/null 2>&1
if [ $? -eq 0 ]; then # Local
backup_source_local
ret=$?
else
echo "$backupsource" | $GREP '.*:\/' >/dev/null 2>&1
if [ $? -eq 0 ]; then # SSH
backup_source_ssh
ret=$?
fi
fi
if [ "$ret" = "" ]; then
source_finished[$source_index]=1
sources_finished=$(echo "$sources_finished" + 1 | bc)
sources_failure=$(echo "$sources_failure" + 1 | bc)
error "Unknown backup source: \"$backupsource\""
sourcehost=$backupsource
if ! [ "$backupemailfailure" = "0" ]; then
backup_source_failure_report
fi
source_log[$source_index]=
continue
elif [ "$ret" -eq 0 ]; then
backup_dest_loop
source_destloop[$source_index]=1
if [ "$destinations_success" -gt 0 ]; then
source_finished[$source_index]=1
sources_finished=$(echo "$sources_finished" + 1 | bc)
sources_success=$(echo "$sources_success" + 1 | bc)
source_log[$source_index]=
continue
fi
fi
timenow=$(date +%s)
time=$(echo "$timenow" - "${source_timestart[$source_index]}" | bc)
if [ "${source_count[$source_index]}" -ge "$backupsourceretries" ] || [ "$time" -ge "$backupsourceretryttl" ]; then
source_finished[$source_index]=1
sources_finished=$(echo "$sources_finished" + 1 | bc)
sources_failure=$(echo "$sources_failure" + 1 | bc)
if ! [ "$backupemailfailure" = "0" ] && [ "${source_destloop[$source_index]}" -eq 0 ]; then
backup_source_failure_report
fi
fi
if [ "${source_destloop[$source_index]}" -eq 0 ]; then
source_log[$source_index]=$log
else
source_log[$source_index]=
fi
continue
done
done
if [ "$sources_failure" -eq 0 ] ; then
return 0
else
return 1
fi
}
backup_source_local() {
if [ "$sourcehost" = "" ]; then
sourcehost="$(hostname -s)"
fi
if [ "$sourcefiles" = "" ]; then
sourcefiles=$backupsource
fi
sourcemountpoint=
if [ "$sourcecount" -gt 1 ]; then
status "Waiting $backupsourceretrydelay seconds to retry local source \"$sourcehost\"."
sleep $backupsourceretrydelay
fi
status "Doing backup of local source \"$sourcehost\" [$sourcecount]."
backup_source_checkdirs
return $?
}
backup_source_ssh() {
sourcehost=$(echo "$backupsource" | cut -d':' -f1)
sourceuh=$sourcehost
sourcefiles=$(echo "$backupsource" | cut -d':' -f2-)
echo "$sourcehost" | $GREP '.@.' >/dev/null 2>&1
if [ $? -eq 0 ]; then
#sourceuser=$(echo "$sourcehost" | cut -d'@' -f1)
sourcehost=$(echo "$sourcehost" | cut -d'@' -f2-)
fi
if [ "$sourcehost" = "$(hostname -s)" ] ; then
backup_source_local
return $?
fi
if [ "$sourcecount" -gt 1 ]; then
status "Waiting $backupsourceretrydelay seconds to retry remote source \"$sourcehost\"."
sleep $backupsourceretrydelay
fi
status "Doing backup of remote source \"$sourcehost\" [$sourcecount]."
# Check if host is up
checkstarttime=$(date +%s)
waitprint=0
while :
do
ssh -o ConnectTimeout=3 -o BatchMode=yes "$backup_ssh_args" "$sourceuh" echo "" >/dev/null 2>&1
if [ $? -eq 0 ]; then
status "Backup source \"$sourcehost\" is up."
break
fi
if [ "$waitprint" -eq 0 ]; then
waitprint=1
error "Backup source \"$sourcehost\" is down - Waiting for host to respond."
fi
sleep $backupsourceconnectdelay
timenow=$(date +%s)
time=$(echo "$timenow" - "$checkstarttime" | bc)
if [ "$time" -ge "$backupsourceconnectttl" ]; then
error "Backup source \"$sourcehost\" is down - Giving up."
return 1
fi
done
sourcemountpoint="$backupmntdir/ssh-$(hostname -s)-$sourcehost-$backupdate-${RANDOM}"
mkdir -p "$sourcemountpoint" || return 1
statusnn "Connecting SSH source \"$sourcehost\" on \"$sourcemountpoint\"."
sshfs "$backup_ssh_args" "${sourceuh}":/ "$sourcemountpoint" || {
error "Unable to connect to SSH source \"$sourcehost\"."
rmdir "$sourcemountpoint" >/dev/null 2>&1;
return 1;
}
successprint
backup_source_checkdirs
return $?
}
backup_source_checkdirs() {
status "Validating backup directories of source \"$sourcehost\"."
sourcefilesX=$(echo "$sourcefiles" | tr '\n' ' ' | $SED -e 's/^ //g' | $SED -e 's/ / /g')
sourcefiles=
sourcefilesmissing=0
sourceexcludes=
for ((i=1; ; i++))
do
s=$(echo "$sourcefilesX" | cut -d',' -f"$i" | tr -d '[:space:]')
if [ "$s" = "" ]; then
break
fi
echo "$s" | $GREP -i '^-.*$' >/dev/null 2>&1
if [ $? -eq 0 ]; then
s=$(echo "$s" | $SED -e 's/^-//g')
debug "Excluding file \"$s\" for \"$sourcehost\"."
if [ "$sourceexcludes" = "" ]; then
sourceexcludes="$s"
else
sourceexcludes="$sourceexcludes
$s"
fi
continue
fi
if [ "$sourcemountpoint" = "" ]; then
lsfile=$s
else
lsfile=$sourcemountpoint/$s
fi
$LS "$lsfile" >/dev/null 2>&1
if [ $? -eq 0 ] ; then
debug "Including file \"$s\" for \"$sourcehost\"."
if [ "$sourcehost" = "$(hostname -s)" ]; then
sourcefiles="$sourcefiles $s"
else
sourcefiles="$sourcefiles ${sourceuh}:${s}"
fi
else
#error "Excluding \"$s\" from backup for \"$sourcehost\", can't access!"
error "Cant't access file \"$s\" on \"$sourcehost\"."
sourcefilesmissing=$(echo $sourcefilesmissing + 1 | bc)
fi
done
sourcefiles=$(echo "$sourcefiles" | $SED -e 's/^ //g')
sourcefilesX=
if ! [ "$sourcemountpoint" = "" ]; then
statusnn "Unmounting source \"$sourcehost\" from \"$sourcemountpoint\"."
fusermount -u "$sourcemountpoint" && { rmdir "$sourcemountpoint" && successprint; }
stopprint
fi
if [ "$sourcefiles" = "" ] ; then
error "No valid source files for \"$sourcehost\", aborting backup!"
return 1
fi
if ! [ "$sourcefilesmissing" -eq 0 ]; then
error "One or more source files missing for \"$sourcehost\", aborting backup!"
return 1
fi
if [ "$sourcehost" = "$(hostname -s)" ]; then
sourcefiles="$sourcefiles /dev/null"
else
sourcefiles="$sourcefiles $sourceuh:/dev/null"
fi
return 0
}
backup_dest_loop() {
destinations_failure=0
destinations_success=0
for backupdest in $backupdestinations
do
status "Doing backup from source \"$sourcehost\" to destination \"$backupdest\"."
desttype=
desttarget=
desthost=
destuser=
destdir=
destmountpoint=
destmountdir=
destmounted=0
rsyncstart=
ret=
echo "$backupdest" | $GREP -i '.*:\/\/.*\/' >/dev/null 2>&1
if [ $? -eq 0 ] ; then
echo "$backupdest" | $GREP -i '^smb:\/\/.*\/' >/dev/null 2>&1
if [ $? -eq 0 ] ; then # SMB
backup_dest_smb
ret=$?
fi
else
echo "$backupdest" | $GREP '^\/' >/dev/null 2>&1
if [ $? -eq 0 ] ; then # Local
backup_dest_local
ret=$?
fi
echo "$backupdest" | $GREP '.*:\/' >/dev/null 2>&1
if [ $? -eq 0 ] ; then # SSH
backup_dest_ssh
ret=$?
fi
fi
if [ "$ret" = "" ] ; then
destinations_failure=$(echo "$destinations_failure" + 1 | bc)
error "Unknown backup destination: \"$backupdest\""
if ! [ "$backupemailfailure" = "0" ]; then
backup_failure_report
fi
continue
fi
if [ "$ret" -eq 0 ] ; then
backup_rsync
ret=$?
fi
if [ "$ret" -eq 0 ] ; then
destinations_success=$(echo "$destinations_success" + 1 | bc)
if ! [ "$backupemailsuccess" = "0" ]; then
backup_success_report
fi
else
destinations_failure=$(echo "$destinations_failure" + 1 | bc)
if ! [ "$backupemailfailure" = "0" ]; then
backup_failure_report
fi
fi
if ! [ "$backupexcludestmpfile" = "" ]; then
rm -f "$backupexcludestmpfile"
backupexcludestmpfile=
fi
if ! [ "$backuplogtmpfile" = "" ]; then
rm -f "$backuplogtmpfile"
backuplogtmpfile=
fi
if ! [ "$backuplogtmpfile_stdout" = "" ]; then
rm -f "$backuplogtmpfile_stdout"
backuplogtmpfile_stdout=
fi
done
if [ "$destinations_failure" -eq 0 ] ; then
return 0
else
return 1
fi
}
backup_dest_local() {
desttype=1
if [ "$desthost" = "" ]; then
desthost="$(hostname -s)"
fi
if [ "$destdir" = "" ]; then
destdir=$backupdest
fi
if [ "$desttarget" = "" ]; then
desttarget=$backupdest
fi
destmountpoint=
destmounted=0
destmountdir=$destdir
debug "Backup to local destination \"$backupdest\"."
backup_dest_local_mount