Skip to content

Instantly share code, notes, and snippets.

@jpascal
Forked from perfecto25/restic_cron.sh
Created October 22, 2023 07:33
Show Gist options
  • Save jpascal/53989216c70deba4f27b364116311a59 to your computer and use it in GitHub Desktop.
Save jpascal/53989216c70deba4f27b364116311a59 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="user@company.com"
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