Skip to content

Commit

Permalink
Merge pull request #109 from djmaze/add-incomplete-backup-hook
Browse files Browse the repository at this point in the history
Add incomplete backup hook
  • Loading branch information
djmaze committed Apr 18, 2022
2 parents 61d12e9 + 65412d2 commit 7ffb3b3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,17 @@ You can add one or multiple commands by specifying the following environment var
POST_COMMANDS_FAILURE: |-
/my/scripts/mail-failure.sh
POST_COMMANDS_INCOMPLETE: |-
/my/scripts/mail-incomplete.sh
POST_COMMANDS_EXIT: |-
docker start my_container
The commands specified are executed one by one.

- `POST_COMMANDS_SUCCESS` commands will be executed after a successful backup run.
- `POST_COMMANDS_SUCCESS` commands will be executed after a fully successful backup run (i.e. all files could be read).
- `POST_COMMANDS_FAILURE` commands will be executed after a failed backup run.
- `POST_COMMANDS_INCOMPLETE` commands will be executed if the backup is incomplete (i.e. one or more files could not be read).
- `POST_COMMANDS_EXIT` will always be executed, after both successful or failed backup runs.

### Notification example
Expand Down
10 changes: 8 additions & 2 deletions backup
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,13 @@ set +e
restic --repo="${RESTIC_REPOSITORY}" backup "${backup_args[@]}" "${tag_options[@]}" "${backup_sources[@]}"
rc=$?
set -e

if [ $rc -ne 0 ]; then
run_commands "${POST_COMMANDS_FAILURE:-}"
exit $rc
if [ $rc -eq 3 ] && [ -n "${POST_COMMANDS_INCOMPLETE:-}" ]; then
run_commands "${POST_COMMANDS_INCOMPLETE:-}"
else
run_commands "${POST_COMMANDS_FAILURE:-}"
fi
fi

echo Backup successful
Expand All @@ -69,4 +73,6 @@ fi
end=$(date +%s)
echo Finished backup at $(date +"%Y-%m-%d %H:%M:%S") after $((end-start)) seconds

[ $rc -ne 0 ] && exit $rc

run_commands "${POST_COMMANDS_SUCCESS:-}"
25 changes: 24 additions & 1 deletion spec/backup_spec.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,30 @@ HERE
The status should eq 1
End

It "Runs failure command if backup is incomplete and incomplete command is not specified"
cat <<HERE >"$extra_env"
RESTIC_BACKUP_SOURCES=/proc/self/mem
POST_COMMANDS_FAILURE=echo Total failure!
HERE
When call docker_exec backup
The stderr should not include "Fatal:"
The output should include "Total failure!"
The status should eq 3
End

It "Runs incomplete command if backup is incomplete and incomplete command is specified"
cat <<HERE >"$extra_env"
RESTIC_BACKUP_SOURCES=/proc/self/mem
POST_COMMANDS_INCOMPLETE=echo Partial failure!
POST_COMMANDS_FAILURE=echo Total failure!
HERE
When call docker_exec backup
The stderr should not include "Fatal:"
The output should not include "Total failure!"
The output should include "Partial failure!"
The status should eq 3
End

It "Forgets old backups after backup"
cat <<HERE >"$extra_env"
RESTIC_FORGET_ARGS=--keep-last 1
Expand All @@ -84,5 +108,4 @@ HERE
The status should be success
The output should match pattern "*keep 1 snapshots*"
End

End

0 comments on commit 7ffb3b3

Please sign in to comment.