-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup.sh
executable file
·118 lines (104 loc) · 3.44 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
#!/usr/bin/env bash
#
# Backup script to save backup to repository and forgets old snapshots (execute manually or by CHECK_CRON schedule)
# - restic backup
# - restic forget
source /bin/log.sh
mkdir -p /var/log/restic
lastLogfile="/var/log/restic/lastrun.log"
backup_dir="/data"
start=`date +%s`
healthcheck() {
suffix=$1
if [ -n "$HEALTHCHECK_URL" ]; then
log "INFO" "Reporting healthcheck $suffix ..."
[[ ${1} == "/start" ]] && m="" || m=$(cat ${lastLogfile} | tail -n 300)
curl -fSsL --retry 3 -X POST \
--user-agent ${USER_AGENT} \
--data-raw "$m" "${HEALTHCHECK_URL}${suffix}"
if [ $? != 0 ]; then
log "ERROR" "HEALTHCHECK_URL seems to be wrong..."
exit 1
fi
else
log "DEBUG" "No HEALTHCHECK_URL provided. Skipping healthcheck."
fi
}
# always execute /bin/pre-default.sh
log "DEBUG" "Starting pre-default.sh"
/bin/pre-default.sh
if [ $? -ne 0 ]; then
log "ERROR" "pre-default.sh was not successful."
exit 1
fi
# /hooks/pre-backup.sh
if [ -f "/hooks/pre-backup.sh" ]; then
log "INFO" "Starting pre-backup script"
/hooks/pre-backup.sh
if [ $? -ne 0 ]; then
log "ERROR" "pre-backup.sh was not successful."
exit 1
fi
else
log "DEBUG" "Pre-backup script not found"
fi
log "INFO" "Starting Backup"
echo "Starting Backup at $(date +"%Y-%m-%d %H:%M:%S")" > $lastLogfile
log "DEBUG" "BACKUP_CRON: ${BACKUP_CRON}"
log "DEBUG" "RESTIC_TAG: ${RESTIC_TAG}"
log "DEBUG" "RESTIC_FORGET_ARGS: ${RESTIC_FORGET_ARGS}"
log "DEBUG" "RESTIC_JOB_ARGS: ${RESTIC_JOB_ARGS}"
log "DEBUG" "RESTIC_REPOSITORY: ${RESTIC_REPOSITORY}"
log "DEBUG" "Save directory tree:"
tree -a -P *.gz -P *.dump -P .exclude_from_backup -L 3 ${backup_dir} >> $lastLogfile
log "DEBUG" "Healthcheck start"
healthcheck /start
log "INFO" "Start the restic backup"
restic backup ${backup_dir} ${RESTIC_JOB_ARGS} --tag=${RESTIC_TAG} >> $lastLogfile 2>&1
backupRC=$?
if [[ $backupRC == 0 ]]; then
log "INFO" "Backup Successful"
healthcheck /0
else
log "ERROR" "Backup Failed with Status ${backupRC}"
restic unlock >> $lastLogfile 2>&1
healthcheck /fail
fi
if [[ $backupRC == 0 ]] && [ -n "${RESTIC_FORGET_ARGS}" ]; then
log "INFO" "Forget old snapshots based on RESTIC_FORGET_ARGS = ${RESTIC_FORGET_ARGS}"
restic forget ${RESTIC_FORGET_ARGS} >> $lastLogfile 2>&1
rc=$?
if [[ $rc == 0 ]]; then
log "INFO" "Finished restic forget"
else
log "ERROR" "Forget Failed with Status: ${rc}"
restic unlock >> $lastLogfile 2>&1
healthcheck /fail
fi
fi
end=`date +%s`
log "INFO" "Finished Backup after $((end-start)) seconds"
echo "Finished Backup at $(date +"%Y-%m-%d %H:%M:%S") after $((end-start)) seconds" >> $lastLogfile
if [ -n "${MSMTP_ARGS}" ]; then
log "INFO" "Executing mail command"
echo -e "Subject: Restic-Backup \n\n$(cat ${lastLogfile})" | msmtp ${MSMTP_ARGS}
ms=$?
if [[ $ms == 0 ]]; then
log "INFO" "Mail notification successfully sent."
else
log "ERROR" "Sending mail notification FAILED."
fi
else
log "DEBUG" "MSMTP_ARGS not defined. Therefore no mail notification."
fi
# /hooks/post-backup.sh
if [ -f "/hooks/post-backup.sh" ]; then
log "INFO" "Starting post-backup script"
/hooks/post-backup.sh $backupRC
if [ $? -ne 0 ]; then
log "ERROR" "post-backup.sh was not successful."
exit 1
fi
else
log "DEBUG" "Post-backup script not found"
fi