-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·846 lines (809 loc) · 24.2 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
#!/bin/bash
# -*- coding: UTF8 -*-
# ---------------------------------------------
# @author: Guillaume Seren
# @since: 09/03/2015
# source: https://github.com/GuillaumeSeren/backup
# file: backup.sh
# Licence: GPLv3
# ---------------------------------------------
# TaskList {{{1
# @TODO: Add stdout log for systemd support
# @TODO: Add support for relative path to getValidateTo/From()
# @TODO: Add version of the script in the log.
# @TODO: Refactor main to *only* call functions (and not process).
# @TODO: Function to check used space before doing archive, log size.
# @TODO: Better log, calculate size moved / read / copy.
# @TODO: Use tee to copy echo ouput and add it to log
# @TODO: Add speed stat mo/s ko/s go/s in the log.
# @TODO: Move log to /var/log + package + logrotate
# @TODO: Add a way to get the rsync/tar status.
# @TODO: We need better test over ssh before rm/add.
# @TODO: Count the files on a given period (day/week/month/year).
# @TODO: Add getFileNameNotInPeriod of 2 timestamps.
# @TODO: Improve cleaning mode filter, add param to configure it.
# @TODO: Add new mode agent to parse log and output (mail) important event.
# @TODO: Add mode 2 way SYNC2W to provide 2 way sync, the newer is taken.
# Error Codes {{{1
# 0 Ok
# 1 Error in cmd / options
# 2 Error log file
# 3 The last call is still running
# 4 The getFileNameByDay is called with no filename (first parm).
# 5 The getValidateFrom arg is not readable, check fs perm.
# 6 The getValidateTo arg is not readable/writeable, check fr perm.
# 7 The bwlimit is null.
# 8 Missing dependencies
# 9 Some default param is missing
# 10 Error unknown options
# 11 Error in function exitWrapper
# 12 Error in statusCall
# 13 Disk avail is not enought for the sizeMoved by rsync
# 14 The getFileTypeNotInPeriod filename is missing
# 15 Compression method is bad
# 15 Compression option is bad
# Default variables {{{1
dependencies='date dirname sha1sum cut rev tar rsync'
# Flags :
flagGetOpts=0
# https://xkcd.com/1179/
dateNow="$(date -u +"%Y-%m-%dT%H:%M:%SZ")"
# simple timing
# Convert dateNow to %s timestamp
timeStart="$(date -d "$dateNow" "+%s")"
logPath="$(dirname "$0")"
lockHash="$(echo "$@" | sha1sum | cut -d ' ' -f1)"
lockFile="$logPath/${lockHash}.lock"
logFile="$(echo "$0" | rev | cut -d"/" -f1 | rev)"
# This is for the actual run
logFileActual="$logPath/${lockHash}-${logFile%.*}-${dateNow}.log"
logFile="$logPath/${logFile%.*}.log"
# TAR archive infos
tarParams="-Jcf"
tarExtension=".tar.xz"
# FUNCTION usage() {{{1
# Return the helping message for the use.
function usage()
{
cat << DOC
${0} options
Backup a target in a location path.
OPTIONS:
-h, --help Show this message.
-v, --verbose Activate verbose mode, show debug messages.
-f, --from Location* from.
-t --to Location* to.
*Locations can be remote or local:
-Local: (~/foo or /foo/bar/).
-Remote :
- ssh_alias:~/foo
- [email protected]:~/foo
-m, --mode [opt] Define mode, can be:
"TARB": Create a tarball. (LOCAL ONLY)
"SYNC": Sync 2 directory (default).
Note that the sync is 1 way (from -> to).
"CLEAN": Clean old tarball, (keep only today's backup).
"SYNCRM": Delete the missing (cleaned) files on the reference.
-l, --limit Limit the bandwith available:
0 Is no limit (default).
By default the value will be in KiB.
You can specify other suffixes see rsync man page.
-c, --compression Define the tar compression method.
".tar"
".tar.gz"
".tar.xz" <- Default
-j, --jopts Define / override the tar compression options.
"-cf"
"-czf"
"-Jcf" <- Default
-e, --email Specify a email to contact if error.
-a, --archivename Define archive name instead of using location basename
Examples:
Sync 2 directory
${0} -f server:/var/www/foo -t /var/save/bar/ -m SYNC
Make a tarball of a path, save it in the location.
${0} -f server:/var/www/foo -t /var/save/dump/ -m TARB
Delete old tarball:
${0} -f server:/var/www/foo -t /var/save/bar/ -m CLEAN
SYNCRM 2 directory
${0} -f server:/var/www/foo -t /var/save/bar/ -m SYNCRM
DOC
}
# FUNCTION createlogFile() {{{1
function createLogFile() {
local logFileLocal=''
if [[ -n "$1" ]]; then
logFileLocal="$1"
else
# If no parm take default log file
logFileLocal="$logFile"
fi
# Touch the file
if [[ ! -f "$logFileLocal" ]]; then
earlyLog="Creation log file: $logFileLocal"
touch "$logFileLocal"
fi
# If the file is still no variable
if [ ! -w "$logFileLocal" ]; then
echo "The log file is not writeable, please check permissions."
exitWrapper 2
fi
echo "$earlyLog"
}
# FUNCTION log() {{{1
# $1 Message
# $2 Verbose level (VERBOSE, ALERT)
function log() {
local globalLog=''
local earlyLog=''
# We need to check if the file is available
if [[ ! -w "$logFile" ]]; then
globalLog="$(createLogFile "${logFile}")"
fi
# Do we have some early log to catch
if [[ -n "${globalLog}" ]]; then
echo "${dateNow} ${idScriptCall} ${globalLog}" >> "${globalLog}" 2>&1
# Clear earlyLog after displaying it
unset globalLog
fi
if [[ ! -w "${logFileActual}" ]]; then
earlyLog="$(createLogFile "${logFileActual}")"
fi
# Do we have some early log to catch
if [[ -n "$earlyLog" ]]; then
echo "${dateNow} ${idScriptCall} ${earlyLog}" >> "${logFile}" 2>&1
# Clear earlyLog after displaying it
unset earlyLog
fi
# test if it is writeable
# Export the create / open / check file outside
# Ouput message + verbose level if set
if [[ -n "$1" && -z "$2" ]]; then
echo "${dateNow} ${idScriptCall} $1" >> "${logFileActual}" 2>&1
elif [[ -n "$1" && -n "$2" && "$2" == "VERBOSE" ]]; then
# This is verbose stuff not critical for production
if [[ -n "${cmdVerbose}" && "${cmdVerbose}" == 1 ]]; then
echo "${dateNow} ${idScriptCall} $1" >> "${logFileActual}" 2>&1
fi
elif [[ -n "$1" && -n "$2" && "$2" == "ALERT" ]]; then
echo "${dateNow} ${idScriptCall} $1" >> "${logFileActual}" 2>&1
# Do we have the alert flag
echo "${dateNow} ${idScriptCall} $1"
fi
}
# FUNCTION getUniqueName() {{{1
function getUniqueName() {
# dateNow is already defined at the beginning
local uniqueName=''
if [[ -n "$1" ]]; then
# The name is derivative from the target pathname
# but you can give other things
uniqueName="$1"
fi
echo "${uniqueName}_${dateNow}"
}
# FUNCTION getFileNameOnDay() {{{1
# Return the filename if the date pattern is on a given day (default today).
function getFileNameOnDay() {
local dateDay=''
# Check the filename
if [[ -z "$1" ]]; then
echo "Error: You can not call the getFileNameByDay without filename"
cleanLockFile
exitWrapper 4
fi
# Check the date (day)
if [[ -n $2 ]]; then
#@TODO: Add a regex to validate the format
dateDay="$2"
else
dateDay="$(date -d "$dateNow" "+%Y-%m-%d")"
fi
# Let's check if the filename contain the chosen date:
if [[ "${1}" =~ ^$dateDay-.*+$ ]]; then
echo "${1}"
fi
}
# FUNCTION getFileNotOnDay() {{{1
# Return the filename if the date pattern is not on a given day (default today).
function getFileNameNotOnDay() {
# local dateDay=''
# Check the filename
if [[ -z "$1" ]]; then
echo "Error: You can not call the getFileNameNotOnDay without filename"
cleanLockFile
exitWrapper 4
fi
# Check the date (day)
if [[ -n "$2" ]]; then
#@TODO: Add a regex to validate the format
dateDay="$2"
else
dateDay="$(date -d "$dateNow" "+%Y-%m-%d")"
fi
# Let's check if the filename contain the chosen date:
if [[ ! "${1}" =~ ^$dateDay-.*+$ ]]; then
echo "${1}"
fi
}
# # FUNCTION getFileTypeNotInPeriod() {{{1
# function getFileTypeNotInPeriod() {
# local filename
# if [[ -z "$1" ]]; then
# echo "Error: You can not call the getFileTypeNotInPeriod without filename"
# cleanLockFile
# exitWrapper 5
# else
# filename="$1"
# fi
# if [[ -z "$2" || -z "$3" ]]; then
# echo "Error: You can not call the getFileTypeNotInPeriod without dates"
# cleanLockFile
# exitWrapper 6
# else
# dateFrom="$2"
# dateTo="$3"
# fi
# # date -d "1970-01-01T00:00:00Z" "+%s"
# # We need to use clean ISO 8601 in backup names
# }
# FUNCTION cleanLockFile() {{{1
# clean lock file.
function cleanLockFile() {
# test if lock file has well been made
if [[ -n "$lockFile" ]]; then
log "cleaning the lock file: $lockFile" "VERBOSE"
rmFile "$lockFile"
fi
}
# FUNCTION getUrlType() {{{1
function getUrlType
{
# Return the type of the URL.
# SSH -> 192.168.0.1:~/mypath/
# SSH -> [email protected]:~/mypath/
# SSH -> ssh_alias:~/mypath/
# LOCAL -> ~/mypath/
# Regex :
local sRegPathLocal='^(/)?([a-zA-Z]+.(/)?)+$'
local sRegPathSshIp='^([0-9]{1,3}\.){3}[0-9]{1,3}:(~/.+)?(/.+)?$'
local sRegPathSshIpUser='^([a-zA-Z0-9]+)@([0-9]{1,3}\.){3}[0-9]{1,3}:(~/.+)?(/.+)?$'
local sRegPathSshAlias='^(.+):(~/.+)?(/.+)?$'
# Get param
if [[ -n "$1" ]]; then
local url=$1
fi
local urlType=""
if [[ $url =~ $sRegPathSshIp ||
$url =~ $sRegPathSshIpUser ||
$url =~ $sRegPathSshAlias ]]; then
# It's SSH
urlType="ssh"
elif [[ $url =~ $sRegPathLocal ]]; then
# It's local
urlType="local"
else
# Sinon c'est une url inconnu.
urlType="unknown"
fi
echo $urlType
}
# FUNCTION getValidateFrom() {{{1
function getValidateFrom() {
local from=""
local fromReturn=""
# validate the target
if [[ -n "$1" ]]; then
from="$1"
else
# Without arg we take the default if set
from="$cmdFrom"
fi
# We need to detect the type of url:
local urlType=""
urlType="$(getUrlType "$from")"
if [[ "$urlType" == "local" ]]; then
# Now test if the target is available
if [[ -r "$from" ]]; then
# target is valid
fromReturn="$from"
else
# target is not
fromReturn=""
fi
else
# The URL is not local so we don't test it
fromReturn="$from"
fi
echo "$fromReturn"
}
# FUNCTION getValidateTo() {{{1
function getValidateTo() {
local to=""
local toReturn=""
# validate the target
if [[ -n "$1" ]]; then
to="$1"
else
# Without arg we take the default if set
to="$cmdTo"
fi
# We need to detect the type of url:
local urlType=""
urlType="$(getUrlType "$to")"
if [[ "$urlType" == "local" ]]; then
# We may also need to check if it is a mount point and if it is check it is
# mounted before checking permission, like with nfs / gluster
# Now test if the target is available
if [[ -r "$to" && -w "$to" ]]; then
# target is valid
toReturn="$to"
else
# target is not
toReturn=""
fi
else
# The URL is not local so we don't test it
toReturn="$toReturn"
fi
echo "$toReturn"
}
# FUNCTION getFileSize {{{1
function getFileSize() {
# Default size
local returnSize="0"
# get the file size
if [[ -n "${1}" ]]; then
returnSize=$(du -b "${1}" | cut -f1)
fi
echo "${returnSize}"
}
# FUNCTION rmFile() {{{1
function rmFile() {
# Before deleting the file we check that exist and type and perm
if [[ -n "${1}" ]]; then
if [[ ! -e "${1}" ]]; then
# if file not exist
log "File ${1} not exist"
elif [[ ! -f "${1}" ]]; then
# file is not a regular file
log "File ${1} is not a regular file"
elif [[ ! -r "${1}" && ! -w "${1}" ]]; then
log "File ${1} permissions problem (r/w)"
else
# I hope we can delete it now
rm "${1}"
fi
fi
}
# FUNCTION rmDir() {{{1
function rmDir() {
# Before deleting the directory we check that exist and type and perm
if [[ -n "${1}" ]]; then
if [[ ! -e "${1}" ]]; then
# if file not exist
log "Directory ${1} not exist"
elif [[ ! -d "${1}" ]]; then
# file is not a regular file
log "Directory ${1} is not a directory"
elif [[ ! -r "${1}" && ! -w "${1}" ]]; then
log "Directory ${1} permissions problem (r/w)"
else
# I hope we can delete it now
rm -r "${1}"
fi
fi
}
# FUNCTION checkDependencies() {{{1
# Test if needed dependencies are available.
function checkDependencies()
{
local deps_ok=''
for dep in $1
do
if ! which "$dep" &>/dev/null; then
echo "This script requires $dep to run but it is not installed"
deps_ok='NO'
fi
done
if [[ "$deps_ok" == "NO" ]]; then
echo "This script need : $1"
echo "Please install them, before using this script !"
exitWrapper 8
else
return 0
fi
}
# FUNCTION exitWrapper() {{{1
function exitWrapper()
{
# Embed the needed process to do while exiting
if [[ -n "$1" && "$1" != 0 ]]; then
if [[ -e "${logFileActual}" && -e "${logFile}" ]]; then
cat "${logFileActual}" >> "${logFile}"
fi
if [[ -n "${cmdMail}" ]]; then
cat "${logFileActual}" | mail -s "Backup has failed: ${logFileActual}" "${cmdMail}"
else
echo "The backup script failed with error ${1}"
fi
exit "${1}"
elif [[ -n "$1" && "$1" == 0 ]]; then
# it is a non error exit (help)
exit "${1}"
else
echo "The backup script failed with error 11"
if [[ -e "${logFileActual}" && -e "${logFile}" ]]; then
cat "${logFileActual}" >> "${logFile}"
fi
if [[ -n "${cmdMail}" ]]; then
cat "${logFileActual}" | mail -s "Backup has failed: ${logFileActual}" "${cmdMail}"
fi
exit "11"
fi
}
# FUNCTION getStatusCall() {{{1
function getStatusCall() {
local output=''
if [[ -n "${1}" && -n "${2}" ]]; then
output="MODE ${1} ${2}"
else
echo "Bad getStatusCall() parm1: ${1} param2: ${2}"
exit 12
fi
echo "${output}"
}
# FUNCTION getMode() {{{1
function getMode() {
# local cmdMode=''
# check valid mode
if [[ "$cmdMode" == "SYNC" ]]; then
cmdMode="SYNC"
elif [[ "$cmdMode" == "SYNCRM" ]]; then
cmdMode="SYNCRM"
elif [[ "$cmdMode" == "TARB" ]]; then
cmdMode="TARB"
elif [[ "$cmdMode" == "CLEAN" ]]; then
cmdMode="CLEAN"
else
# Default mode
cmdMode="SYNC"
fi
echo "${cmdMode}"
}
# FUNCTION getBwLimit() {{{1
function getRsyncBwLimit() {
local rsyncBwLimit=''
if [[ -n "${1}" ]]; then
rsyncBwLimit="${1}"
else
rsyncBwLimit="--bwlimit=0"
fi
echo "${rsyncBwLimit}"
}
# FUNCTION getValidateCompression() {{{1
function getValidateCompression() {
local compression=''
# Compression is .tar.gz or .tar, so it shoud start with .
if [[ -n $1 &&
${1:0:1} == '.' ]]; then
compression="$1"
fi
echo "$compression"
}
# FUNCTION getValidateCompressionOption() {{{1
getValidateCompressionOptions() {
local compressionOptions=''
if [[ -n $1 &&
${1:0:1} == '-' ]]; then
compressionOptions="$1"
fi
echo "$compressionOptions"
}
# GETOPTS {{{1
# Get the param of the script.
optspec=":f:t:c:j:a:m:l:-:evh"
while getopts "$optspec" optchar; do
flagGetOpts=1
# Short options
case "${optchar}" in
h)
usage
exitWrapper 0
;;
f)
cmdFrom="$(getValidateFrom "$OPTARG")"
if [[ "$cmdFrom" == "" ]]; then
echo "The from target is invalid: $OPTARG"
echo "Please check reading permissions of your file system"
exitWrapper 5
fi
;;
t)
cmdTo="$(getValidateTo "$OPTARG")"
if [[ "$cmdTo" == "" ]]; then
echo "The to target is invalid: $OPTARG"
echo "Please check reading permissions of your file system"
exitWrapper 6
fi
;;
l)
rsyncBwLimit="$OPTARG"
if [[ -z "$rsyncBwLimit" ]]; then
echo "Your rsync bwlimit can not be null"
usage
exitWrapper 7
fi
rsyncBwLimit="--bwlimit=${rsyncBwLimit}"
;;
m)
cmdMode="$OPTARG"
;;
e)
cmdMail="$OPTARG"
checkDependencies "mail"
;;
c)
cmdCompression="$OPTARG"
if [[ -n "$(getValidateCompression "$cmdCompression")" ]]; then
tarExtension="$cmdCompression"
else
echo "Bad tar compression $cmdCompression"
exit 15
fi
;;
j)
cmdCompressionOptions="$OPTARG"
if [[ -n "$(getValidateCompressionOptions "$cmdCompressionOptions")" ]]; then
tarParams="$cmdCompressionOptions"
else
echo "Bad tar compressionOption $cmdCompressionOptions"
exit 16
fi
;;
a)
cmdArchiveName="$OPTARG"
if [[ -n "$cmdArchiveName" ]]; then
archiveName="$cmdArchiveName"
fi
;;
v)
cmdVerbose=1
;;
-)
case "${OPTARG}" in
# Long options
# https://stackoverflow.com/questions/402377/using-getopts-in-bash-shell-script-to-get-long-and-short-command-line-options
help)
usage
exitWrapper 0
;;
from)
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
cmdFrom="$(getValidateFrom "$val")"
if [[ -z "$cmdFrom" ]]; then
echo "The from target is invalid: $OPTARG"
echo "Please check reading permissions of your file system"
exitWrapper 5
fi
;;
to)
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
cmdTo="$(getValidateTo "$val")"
if [[ -z "$cmdTo" ]]; then
echo "The to target is invalid: $OPTARG"
echo "Please check reading permissions of your file system"
exitWrapper 6
fi
;;
limit)
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
rsyncBwLimit="$val"
if [[ -z "$rsyncBwLimit" ]]; then
echo "Your rsync bwlimit can not be null"
usage
exitWrapper 7
fi
rsyncBwLimit="--bwlimit=${rsyncBwLimit}"
;;
mode)
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
cmdMode="$val"
;;
verbose)
cmdVerbose=1
;;
email)
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
cmdMail="$val"
checkDependencies "mail"
;;
compression)
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
cmdCompression="$val"
if [[ -n "$(getValidateCompression "$cmdCompression")" ]]; then
tarExtension="$cmdCompression"
else
echo "Bad tar compression $cmdCompression"
exit 15
fi
;;
jcopts)
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
cmdCompressionOptions="$val"
if [[ -n "$(getValidateCompressionOptions "$cmdCompressionOptions")" ]]; then
tarParams="$cmdCompressionOptions"
else
echo "Bad tar compressionOption $cmdCompressionOptions"
exit 16
fi
;;
archivename)
val="${!OPTIND}"; OPTIND=$(( OPTIND + 1 ))
cmdArchiveName="$val"
if [[ -n "$cmdArchiveName" ]]; then
archiveName="$cmdArchiveName"
fi
;;
*)
echo "Unknown long option --${OPTARG}" >&2
usage >&2;
exitWrapper 1
;;
esac;;
*)
echo "Unknown short option -${OPTARG}" >&2
usage >&2;
exitWrapper 10
;;
esac
done
# We check if getopts did not find no any param
if [[ "$flagGetOpts" == 0 ]]; then
echo 'This script cannot be launched without options.'
usage
exitWrapper 1
elif [[ -z "${cmdFrom}" || -z "${cmdTo}" || -z "${cmdMode}" ]]; then
echo 'You need to setup at least 3 params: from, to, mode'
exitWrapper 9
fi
# FUNCTION getArchiveName() {{{1
function getArchiveName() {
local localArchiveName=''
if [[ -n $cmdArchiveName ]]; then
localArchiveName="$archiveName"
else
localArchiveName="$(basename "$cmdFrom")"
fi
echo "$localArchiveName"
}
# FUNCTION main() {{{1
function main() {
local idScriptCall=''
# Use the PID of the current shell
idScriptCall="$$"
log "Save $cmdFrom to $cmdTo"
log "Check dependencies: ${dependencies}" "VERBOSE"
log "Compression: $cmdCompression"
log "CompressionOptions: $cmdCompressionOptions"
checkDependencies "$dependencies"
# local cmdMode=''
cmdMode=$(getMode "${cmdMode}")
# local rsyncBwLimit=''
rsyncBwLimit=$(getRsyncBwLimit "${rsyncBwLimit}")
local statusCall=''
statusCall=$(getStatusCall "${cmdMode}" "${rsyncBwLimit}")
log "${statusCall}"
# Check the lockfile (defined in default var)
if [ -f "$lockFile" ]; then
log "The last call is still running" "ALERT"
local lockFileContent=''
lockFileContent="$(cat "$lockFile")"
log "Running since $(date -d @"$lockFileContent") ABORTING !!" "ALERT"
exitWrapper 3
else
# Only display it if verbose is set
log "creating the lock file: $lockFile" "VERBOSE"
touch "$lockFile"
echo "$timeStart" > "$lockFile"
fi
case "${cmdMode}" in
'SYNC')
# To check if available disk is > than needed by copy
local sizeMoved=''
sizeMoved=$(rsync -a --stats --dry-run "$rsyncBwLimit" "$cmdFrom" "$cmdTo" | grep -i 'Total transferred file size: ' | cut -d':' -f2 | cut -d' ' -f2)
sizeMoved=${sizeMoved//,/}
local diskAvail=''
diskAvail=$(df --output=avail -B1 "$cmdTo" | grep -v 'Avail')
if [[ $diskAvail -lt $sizeMoved ]]; then
log "Disk space avail ($diskAvail) is not enought for ($sizeMoved) !!" "ALERT"
exitWrapper 13
fi
log "$(rsync -az "$rsyncBwLimit" "$cmdFrom" "$cmdTo")"
;;
'SYNCRM')
# Calculate files that are in the cmdTo but deleted on from.
IFS=$'\n'
fileList=($(rsync -avz "$rsyncBwLimit" --delete --dry-run "$cmdFrom" "$cmdTo" | grep 'delet' | sed s/'deleting '//))
# fileList=($(rsync -avz --delete --dry-run "$cmdFrom" "$cmdTo" | grep 'delet' | sed s/'deleting '//))
unset IFS
log "SYNCRM Nb fichier détectés: ${#fileList[@]}"
sizeFileDeleted=0
declare -a aSyncRm
for (( i=0; i<"${#fileList[@]}"; i++ ))
do
if [[ "${fileList[$i]}" == "" || "${fileList[$i]}" =~ ^[[:space:]]++$ ]]; then
log "SYNCRM error on the file: ${fileList[$i]}"
else
# Remove the last / if any in the cmdTo name
targetRm="${cmdTo%/}/${fileList[$i]}"
log "Delete: ${targetRm}"
if [[ -d "${targetRm}" ]]; then
rmDir "${targetRm}"
else
sizeFileDeleted=$(( sizeFileDeleted + $(getFileSize "${targetRm}") ))
rmFile "${targetRm}"
fi
aSyncRm+=("${fileList[$i]}")
fi
done
log "SYNCRM list done: ${#aSyncRm[@]} deleted item(s)"
log "SYNCRM size freed: ${sizeFileDeleted}"
;;
'TARB')
log "MODE TARBALL"
# Delete the last / if any
cmdFrom="${cmdFrom%/}"
archiveName="$(getArchiveName)"
log "ArchiveName: $archiveName"
pathName="$(basename "$cmdFrom")"
tarName="$(getUniqueName "$archiveName")${tarExtension}"
sizeFileDeleted=0
log "Archive name: $tarName"
log "$(tar "${tarParams}" "${cmdTo}/${tarName}" -C "${cmdFrom%$pathName}" "${pathName}/")"
sizeFileDeleted="$(getFileSize "${cmdTo}/${tarName}")"
log "TARB file size: ${sizeFileDeleted}"
;;
'CLEAN')
log "MODE CLEAN"
archiveName="$(getArchiveName)"
log "ArchiveName: $archiveName"
sizeFileDeleted=0
# List all files by name
IFS=$'\n'
fileList=($(find "$cmdTo"/ -maxdepth 1 -type f -name "${archiveName}*${tarExtension}"))
unset IFS
declare -a aFileToClean
for (( i=0; i<"${#fileList[@]}"; i++ ))
do
# Check file not today for the clean
fileMatch="$(getFileNameNotOnDay "$(basename "${fileList[$i]}")" "${archiveName}_$(date -d "$dateNow" "+%Y-%m-%d")")"
if [[ -n "$fileMatch" ]]; then
# There was a match
aFileToClean+=("${fileList[$i]}")
fi
done
# Cleaning loop
for (( i=0; i<"${#aFileToClean[@]}"; i++ ))
do
log "rm ${aFileToClean[$i]}"
sizeFileDeleted=$(( sizeFileDeleted + $(getFileSize "${aFileToClean[$i]}") ))
rmFile "${aFileToClean[$i]}"
done
log "CLEAN done: ${#aFileToClean[@]} deleted item(s)"
log "CLEAN size freed: ${sizeFileDeleted}"
;;
esac
timeEnd="$(date +"%s")"
cleanLockFile
log "duration (sec): $(( timeEnd - timeStart ))"
if [[ -e "${logFileActual}" ]]; then
# if we go here we can add local log to global
cat "${logFileActual}" >> "${logFile}"
# Then clean logFileActual
rm "${logFileActual}"
fi
}
main
# }}}
# vim: set ft=sh ts=2 sw=2 tw=80 foldmethod=marker et :