-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
executable file
·132 lines (108 loc) · 2.64 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
#!/usr/bin/env bash
LOG_TMP_PATH="/var/log/current-backup/"
#Argument to date
LOG_PREFIX="[%Y/%m/%d (%a) %H:%M:%S] "
function fail {
echo $1
exit $2
}
logForMail=""
function logLow {
logger -t "c-hack-backup" "$1"
logForMail="$logForMail"$'\n'"$1"
}
function logFile {
while read line ;do
logLow "$line"
done < "$1"
}
function log {
out="$(date "+$LOG_PREFIX")$1"
echo "$out"
logLow "$out"
}
if [ $# -ne 1 ] ;then
fail "Need the path to the config file." 11
fi
dir="$(dirname "$(realpath "$0")")"
mailOn="notSet"
mailRecp=""
mailSubj=""
backups=()
exitCodes=()
while IFS= read -r line
do
if [[ "$line" == "#"* ]] ;then
#Ignore
:
elif [[ "$line" == "mail"* ]] ;then
if [ "$mailOn" == "notSet" ] ;then
remainingLine=${line#* }
mailOn=${remainingLine%% *}
if ! ( [ "$mailOn" == "on" ] || [ "$mailOn" == "off" ] ) ;then
fail "Config error! mail should be followed by either on or off." 21
fi
if [ "$mailOn" == "on" ] ;then
remainingLine=${remainingLine#* }
mailRecp=${remainingLine%% *}
remainingLine=${remainingLine#* }
mailSubj=$remainingLine
fi
else
fail "Config error! mail can only occur once!" 22
fi
elif [[ "$line" == "backup"* ]] ;then
backups["${#backups[@]}"]=${line#* }
else
fail "Config error! Unknown option: $line" 23
fi
done < "$1"
if [ "$mailOn" == "notSet" ] ;then
fail "Config error! No mail line!" 24
fi
backupCount=${#backups[@]}
log "Starting backup of $backupCount domains."
for (( i=0; i<$backupCount; i++ )); do
backupArgs=${backups[$i]}
vmName=${backupArgs%% *}
logName=$LOG_TMP_PATH"backup_"$(date "+%Y-%m-%d-%H-%m")"_"$vmName".log"
log "------------------"
log "Domain: $vmName"
log "------------------"
"$dir/backup.py" $backupArgs | "$dir/logOutput.py" $logName ; exit=${PIPESTATUS[0]}
sync
sleep 1
logFile "$logName"
log "------------------"
log "Exit Code: $exit"
if [ $exit -eq 111 ] ;then
log "!!! Seems the backup was interrupted. Stopping here!!!"
elif [ $exit -ne 0 ] ;then
log "!!! This is not good !!!"
fi
log "------------------"
log ""
exitCodes[$i]=$exit
if [ $exit -eq 111 ] ;then
break
fi
done
find "$LOG_TMP_PATH" -type f -delete
if [ "$mailOn" == "off" ] ;then
exit 0
fi
exitSum=0
for (( i=0; i<${#exitCodes[@]}; i++ )) ;do
exitC=${exitCodes[$i]}
let exitSum=exitSum+exitC
done
mailSubj="$mailSubj"" Backup "
if [ $exitSum -eq 0 ] ;then
mailSubj="$mailSubj""successfull."
else
mailSubj="$mailSubj""failed."
fi
log "Sending mail"
echo "$logForMail" | mail -s "$mailSubj" $mailRecp
log "End of backup"
log ""