Skip to content

Instantly share code, notes, and snippets.

@perfecto25
Created February 28, 2019 23:20
Show Gist options
  • Save perfecto25/18abbd6cfbaa4e9eb30251470e9b7504 to your computer and use it in GitHub Desktop.
Save perfecto25/18abbd6cfbaa4e9eb30251470e9b7504 to your computer and use it in GitHub Desktop.
Restic cron script
#!/bin/bash
# Runs Restic backup on a schedule via cron, emails with status
# 1. add a cred file with your repo logins to /etc/restic/cred
# 2.
FROM="restic@$(hostname)"
EMAIL="[email protected]"
LOG="/var/log/restic.log"
RDIR="/etc/restic"
### keep last # of days of snapshots
KEEPDAYS=5
log() {
echo -e "$(date "+%m%d%Y_%H%M%S"): ${1}" | tee -a $LOG
}
notify() {
echo -e "${1}" | mail -r "${FROM}" -s "Errors running Restic Backup on host: $(hostname)" "${EMAIL}"
}
cd $RDIR
echo -e "\n" | tee -a $LOG
if [ ! -f cred ]
then
log "${RDIR}/cred file not present, exiting..\n\n"
exit 1
fi
source ${RDIR}/cred
log "starting backup.."
msg=$(restic backup --files-from=include --exclude-file=exclude >> $LOG 2>&1)
if [ $? -eq 1 ]
then
notify "[restic backup]\n${msg}"
log "${msg}\n-----------------------------------------"
exit 1
fi
msg=$(restic check >> $LOG 2>&1)
if [ $? -eq 1 ]
then
notify "[restic check]\n${msg}"
log "${msg}\n--------------------------------------"
exit 1
fi
log "removing old snapshots.."
msg=$(restic forget --keep-daily ${KEEPDAYS} --prune)
if [ $? -eq 1 ]
then
notify "[restic forget]\n${msg}"
log "${msg}"
exit 1
fi
log "end of run\n-----------------------------------------\n\n"
# notify OK
echo -e "Snapshot complete, snapshots older than $KEEPDAYS days deleted." | mail -r "${FROM}" -s "Restic Backup OK on: $(hostname)" ${EMAIL}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment