Last active
August 30, 2020 01:08
-
-
Save truxnell/7686d62c6456a5153aaee1ea2a4f4329 to your computer and use it in GitHub Desktop.
Backup script using borg
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 | |
set -eu | |
# Variables needed by borg | |
export BORG_REPO='ssh://[email protected]:123/path/to/repo/location/' | |
export BORG_PASSPHRASE='PASSPHRASEGOESHERE' | |
export BORG_REMOTE_PATH=borg | |
export BORG_RSH='ssh -oBatchMode=yes' | |
# Variables for the script itself | |
BORG_EXEC=/usr/bin/borg | |
BORG_EXCLUDE_FILE=/etc/borg_exclude | |
LOGPATH=/var/log/borg | |
LOGFILE=borg_`date +"%Y-%m-%d"`.log | |
OLDLOGFILE=dailybackup_`date -d"60 days ago" +"%Y-%m-%d"`.log | |
#### | |
## Functions | |
#### | |
errorcheck() | |
{ | |
if [ "${1}" -ne "0" ]; then | |
echo "Returned ${1} : ${2}" | |
writelog "ERROR! Exiting! Returned ${1} : ${2}" | |
exit $1 | |
fi | |
} | |
writelog() | |
{ | |
echo `date +"%Y-%m-%d %H:%M:%S"` ": ${1}" >> $LOGPATH/$LOGFILE | |
} | |
#### | |
## Script Body | |
#### | |
writelog "Beginning Backup" | |
writelog "Cleaning up old log files" | |
# If the old local log file exists, delete it. | |
if [ -e $LOGPATH/$OLDLOGFILE ]; then | |
rm $LOGPATH/$OLDLOGFILE | |
errorcheck $? "Deleting $LOGPATH/$OLDLOGFILE" | |
writelog "Deleted $LOGPATH/$OLDLOGFILE" | |
fi | |
writelog "Old log file cleanup complete" | |
# Initialize the backup; not needed for routine work. | |
# $BORG_EXEC init --encryption=repokey-blake2 | |
writelog "Beginning borg create" | |
# Do the backup. | |
# Add /var/www back in for production usage. | |
$BORG_EXEC \ | |
create \ | |
--stats \ | |
--list \ | |
--exclude-from $BORG_EXCLUDE_FILE \ | |
::{hostname}-{now:%Y-%m-%dT%H:%M:%S} \ | |
/etc/ \ | |
/opt/ \ | |
/usr/local/ \ | |
/var/log/ \ | |
/var/www/ \ | |
/var/spool/cron/crontabs/ \ | |
/etc/fstab \ | |
/etc/ssh/ | |
>> $LOGPATH/$LOGFILE 2>&1 | |
errorcheck $? "Running borg create" | |
writelog "Completed borg create" | |
writelog "Beginning borg prune" | |
# Clean up the old backups. | |
$BORG_EXEC \ | |
prune \ | |
--stats \ | |
--list \ | |
--keep-daily=7 \ | |
--keep-weekly=4 \ | |
--keep-monthly=1 \ | |
>> $LOGPATH/$LOGFILE 2>&1 | |
errorcheck $? "Running borg prune" | |
writelog "Completed borg prune" | |
writelog "Backup Complete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment