feat: add error handling and lock checking

This commit is contained in:
Alex Kelly 2023-01-16 21:33:37 -05:00
parent 3f678f78bc
commit 248ef13ee9

View file

@ -24,8 +24,6 @@ RETENTION_YEARS=1
source /etc/restic/env
# Paths to binaries
CURL="/usr/bin/curl"
TEE="/usr/bin/tee"
@ -40,13 +38,24 @@ timestamp () {
date "+%Y-%m-%d %H:%M:%S"
}
run_checklock () {
OUTPUT=$(${RESTIC} list locks|wc -l)
if (( OUTPUT != 0 ));then
return 1
else
return 0
fi
}
run_backup () {
#Do the backup
${ECHO} "Starting backup on $HOST at $(timestamp)"
${RESTIC} backup \
${ECHO} "Starting backup on $HOST at $(timestamp)"
${RESTIC} backup -n \
--tag "$BACKUP_TAG" \
--exclude-file $BACKUP_EXCLUDES \
$BACKUP_PATHS
$BACKUP_PATHS &
wait $!
return $?
}
@ -59,14 +68,16 @@ run_forget (){
--keep-daily $RETENTION_DAYS \
--keep-weekly $RETENTION_WEEKS \
--keep-monthly $RETENTION_MONTHS \
--keep-yearly $RETENTION_YEARS
--keep-yearly $RETENTION_YEARS &
wait $!
return $?
}
run_check () {
# Check if everything is fine
${ECHO} "Running check"
${RESTIC} check
${RESTIC} check &
wait $!
return $?
}
@ -99,12 +110,41 @@ notify () {
}
# Do the actual work
if run_backup && ${RESTIC} unlock && run_forget && cleanup; then
ERRORS=""
STATUS_TOTAL=0
if run_checklock; then
${ECHO} "Restic backup on $HOST starting at $(timestamp)"| ${TEE} -a "$LOG_FILE" >(notify)
locks=0
else
${ECHO} "Backup on $HOST aborted at $(timestamp) due to locks" | ${TEE} -a "$LOG_FILE" >(notify)
exit 1
fi
if ! run_backup | tee -a $LOG_FILE; then
backup_status=1
ERRORS="$ERRORS [backup step failed]"
fi
if ! run_forget | tee -a $LOG_FILE; then
forget_status=1
ERRORS="$ERRORS [forget step failed]"
fi
if ! run_check | tee -a $LOG_FILE; then
check_status-1
ERRORS="$ERRORS [check step failed]"
fi
cleanup | tee -a $LOG_FILE
#
#if run_backup && ${RESTIC} unlock && run_forget && cleanup; then
# run_check
#cleanup
#status_total=$(( check_status + backup_status + forget_status ))
#if (( status_total == 0 ));then
if (( status_total == 0 ));then
${ECHO} "Backup on $HOST completed successfully at $(timestamp)" | ${TEE} -a "$LOG_FILE" >(notify)
else
${ECHO} "Backup on $HOST completed at $(timestamp) with issues (see $LOG_FILE)" | ${TEE} -a "$LOG_FILE" >(notify)
${ECHO} "Backup on $HOST completed at $(timestamp) with issues: $ERRORS (see $LOG_FILE)" | ${TEE} -a "$LOG_FILE" >(notify)
fi