-
-
Save mniehe/601885ba155888b435aa91496a701a3e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This script backs up files using restic and sends emails on success or errors. | |
# | |
# Files that the script expects: | |
# /usr/local/etc/restic/repo.env: shell variables that define the restic repository | |
# /usr/local/etc/restic/include.txt: paths to back up | |
# /usr/local/etc/restic/exclude.txt: paths to not include in the backup, even if they are in include.txt | |
# | |
# Inspired by https://gist.github.com/perfecto25/18abbd6cfbaa4e9eb30251470e9b7504 | |
FROM="restic@homeserver" | |
TO="[email protected]" | |
EMAIL_SUBJECT_PREFIX="[Restic] " | |
LOG="/var/log/restic/$(date +\%Y\%m\%d_\%H\%M\%S).log" | |
EXCLUDE="/usr/local/etc/restic/exclude.txt" | |
INCLUDE="/usr/local/etc/restic/include.txt" | |
notify() { | |
cat "${LOG}" | mail -r "${FROM}" -s "${EMAIL_SUBJECT_PREFIX}${1}" "${TO}" | |
} | |
source /usr/local/etc/restic/repo.env | |
# backup | |
echo -e "restic backup\n------------\n" >> "$LOG" | |
restic backup --exclude-file="$EXCLUDE" --files-from="$INCLUDE" >> "$LOG" 2>&1 | |
if [ $? -ne 0 ] | |
then | |
notify "backup error" | |
exit 2 | |
fi | |
# check consistency of the repository | |
echo -e "\n------------\nrestic check\n-----------\n" >> "$LOG" | |
restic check >> "$LOG" 2>&1 | |
if [ $? -ne 0 ] | |
then | |
notify "check error" | |
exit 3 | |
fi | |
# remove outdated snapshots | |
echo -e "\n-------------\nrestic forget\n------------\n" >> "$LOG" | |
restic forget \ | |
--keep-daily 7 \ | |
--keep-weekly 4 \ | |
--keep-monthly 3 \ | |
--keep-yearly 3 \ | |
>> "$LOG" 2>&1 | |
if [ $? -ne 0 ] | |
then | |
notify "forget error" | |
exit 4 | |
fi | |
# prune once a month | |
day=$(date +%d) | |
if [[ $day =~ 0[1-7] ]] | |
then | |
echo -e "\n------------\nrestic prune\n-----------\n" >> "$LOG" | |
restic prune >> "$LOG" 2>&1 | |
if [ $? -ne 0 ] | |
then | |
notify "prune error" | |
exit 5 | |
fi | |
fi | |
notify "success" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment