-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup.sh
executable file
·80 lines (64 loc) · 1.67 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
#!/bin/bash
MAJORVER=0
MINORVER=2
SRCDIR="$1";DSTDIR="$2";
if [ -z "$DSTDIR" ] || [ -z "$SRCDIR" ]; then
echo "$0 SRCDIR DSTDIR"
exit 1
fi
if [ ! -d "$SRCDIR" ]; then
echo "No srcdir $SRCDIR"
exit 1
fi
if [ ! -d "$DSTDIR" ]; then
echo "No dstdir $DSTDIR"
exit 1
fi
function log {
echo "$1" >> "$DSTDIR/log.tmp"
echo "$1"
}
function secondsToTimestamp {
echo - | awk -v "S=$1" '{printf "%dh:%dm:%ds",S/(60*60),S%(60*60)/60,S%60}'
}
SECSTART=`date +"%s"`
START=`date +"%d-%m-%Y %H:%M:%S"`
NUMBER_OF_ERRORS=0
function backup {
cd $SRCDIR
for file in *
do
TIMEBEGIN=`date +"%H:%M:%S"`
if [ -h "$file" ]; then
SRC=`readlink "$file"`
DIRNAME=`echo "$file" | awk -F/ '{print $NF}'`
if ! `rdiff-backup --exclude-symbolic-links "$SRC" "$DSTDIR"/"$DIRNAME"` 2> "$DSTDIR/error.txt"
then
TIMEEND=`date +"%H:%M:%S"`
ERR="`< ./error.txt`"
log " $TIMEBEGIN - $TIMEEND : $SRC -> $DSTDIR/$DIRNAME [FAILED] Error: '$ERR'"
NUMBER_OF_ERRORS=`echo 1+$NUMBER_OF_ERRORS | bc`
else
TIMEEND=`date +"%H:%M:%S"`
log " $TIMEBEGIN - $TIMEEND : $SRC -> $DSTDIR/$DIRNAME [OK]"
fi
if [ -f "$DSTDIR/error.txt" ]; then
rm $DSTDIR/error.txt
fi
fi
done
}
log "--> BEGIN $START <--"
log " Backup script ver=$MAJORVER.$MINORVER"
## Do backup
backup $SRCDIR $DSTDIR
STOP=`date +"%d-%m-%Y %H:%M:%S"`
SECSTOP=`date +"%s"`
ELAPSED=`echo "$SECSTOP - $SECSTART " | bc`
TIMECONVERT=`secondsToTimestamp "$ELAPSED"`
log " Backup took $TIMECONVERT, with $NUMBER_OF_ERRORS errors"
log "--> END $STOP <--"
log " "
#Store the log
cat $DSTDIR/log.tmp >> $DSTDIR/backup.log
rm $DSTDIR/log.tmp