forked from lobaro/restic-backup-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backup.sh
executable file
·104 lines (91 loc) · 3.19 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
#!/bin/sh
lastLogfile="/var/log/backup-last.log"
lastMailLogfile="/var/log/mail-last.log"
lastMicrosoftTeamsLogfile="/var/log/microsoft-teams-last.log"
copyErrorLog() {
cp ${lastLogfile} /var/log/backup-error-last.log
}
logLast() {
echo "$1" >> ${lastLogfile}
}
if [ -f "/hooks/pre-backup.sh" ]; then
echo "Starting pre-backup script ..."
/hooks/pre-backup.sh
else
echo "Pre-backup script not found ..."
fi
if [ -n "$RESTIC_PRE_BACKUP" ]; then
echo "Starting pre-backup command ..."
eval "$RESTIC_PRE_BACKUP"
echo "Done"
else
echo "Pre-backup command not found ..."
fi
start=`date +%s`
rm -f ${lastLogfile} ${lastMailLogfile}
echo "Starting Backup at $(date +"%Y-%m-%d %H:%M:%S")"
echo "Starting Backup at $(date)" >> ${lastLogfile}
logLast "BACKUP_CRON: ${BACKUP_CRON}"
logLast "RESTIC_TAG: ${RESTIC_TAG}"
logLast "RESTIC_FORGET_ARGS: ${RESTIC_FORGET_ARGS}"
logLast "RESTIC_JOB_ARGS: ${RESTIC_JOB_ARGS}"
logLast "RESTIC_REPOSITORY: ${RESTIC_REPOSITORY}"
logLast "AWS_ACCESS_KEY_ID: ${AWS_ACCESS_KEY_ID}"
# Do not save full backup log to logfile but to backup-last.log
restic backup /data ${RESTIC_JOB_ARGS} --tag=${RESTIC_TAG?"Missing environment variable RESTIC_TAG"} >> ${lastLogfile} 2>&1
backupRC=$?
logLast "Finished backup at $(date)"
if [[ $backupRC == 0 ]]; then
echo "Backup Successful"
else
echo "Backup Failed with Status ${backupRC}"
restic unlock
copyErrorLog
fi
if [[ $backupRC == 0 ]] && [ -n "${RESTIC_FORGET_ARGS}" ]; then
echo "Forget about old snapshots based on RESTIC_FORGET_ARGS = ${RESTIC_FORGET_ARGS}"
restic forget ${RESTIC_FORGET_ARGS} >> ${lastLogfile} 2>&1
rc=$?
logLast "Finished forget at $(date)"
if [[ $rc == 0 ]]; then
echo "Forget Successful"
else
echo "Forget Failed with Status ${rc}"
restic unlock
copyErrorLog
fi
fi
end=`date +%s`
echo "Finished Backup at $(date +"%Y-%m-%d %H:%M:%S") after $((end-start)) seconds"
if [ -n "${TEAMS_WEBHOOK_URL}" ]; then
teamsTitle="Restic Last Backup Log"
teamsMessage=$( cat ${lastLogfile} | sed 's/"/\"/g' | sed "s/'/\'/g" | sed ':a;N;$!ba;s/\n/\n\n/g' )
teamsReqBody="{\"title\": \"${teamsTitle}\", \"text\": \"${teamsMessage}\" }"
sh -c "curl -H 'Content-Type: application/json' -d '${teamsReqBody}' '${TEAMS_WEBHOOK_URL}' > ${lastMicrosoftTeamsLogfile} 2>&1"
if [ $? == 0 ]; then
echo "Microsoft Teams notification successfully sent."
else
echo "Sending Microsoft Teams notification FAILED. Check ${lastMicrosoftTeamsLogfile} for further information."
fi
fi
if [ -n "${MAILX_ARGS}" ]; then
sh -c "mailx -v -S sendwait ${MAILX_ARGS} < ${lastLogfile} > ${lastMailLogfile} 2>&1"
if [ $? == 0 ]; then
echo "Mail notification successfully sent."
else
echo "Sending mail notification FAILED. Check ${lastMailLogfile} for further information."
fi
fi
if [ -f "/hooks/post-backup.sh" ]; then
echo "Starting post-backup script ..."
/hooks/post-backup.sh $backupRC
else
echo "Post-backup script not found ..."
fi
if [ -n "$RESTIC_POST_BACKUP" ]; then
echo "Starting post-backup command ..."
eval "$RESTIC_POST_BACKUP"
echo "Done"
else
echo "Post-backup command not found ..."
fi