Automate borg backups with Cron
Install dependencies
To be able send an email from the backup bash command, we have to install mailutils:
apt install mailutils
Create borg_backup.sh
First, create a script which will execute the backups. This could look like the following script and be under /home/server_backup/scripts/borg_backup.sh
.
#!/usr/bin/env bash
################################################
#### borg create and borg prune ####
#### PLEASE CHECK BACKUP DIRECTORIES ####
################################################
##
## Set environment variables
##
## if you don't use the standard SSH key,
## you have to specify the path to the key like this
export BORG_RSH="ssh -i /root/.ssh/id_rsa_borg"
## You can save your borg passphrase in an environment
## variable, so you don't need to type it in when using borg
# export BORG_PASSPHRASE="top_secret_passphrase"
# ACTIVATE VENV
source /usr/local/venvs/borg-env/bin/activate
##
## Set some variables
##
LOG="/var/log/borg_backup.log"
BACKUP_USER="u123456"
REPOSITORY_DIR="jobsitehr"
SERVER_EMAIL="My Server<root@mydomain.com>"
ADMIN_EMAIL="myadmin@gmail.com"
## Tip: If using with a Backup Space you have to use
## 'your-storagebox.de' instead of 'your-backup.de'
REPOSITORY="ssh://${BACKUP_USER}@${BACKUP_USER}.your-storagebox.de:23/./backups/${REPOSITORY_DIR}"
##
## Output to a logfile
##
exec > >(tee -i ${LOG})
exec 2>&1
echo "###### Backup started: $(date) ######"
## some helpers and error handling:
send_admin_mail() {
echo "$(cat $LOG)" | mailx -a "From:"$SERVER_EMAIL -s "$1 - $REPOSITORY_DIR Backup $(date)" $ADMIN_EMAIL
}
info() { printf "\n%s %s\n\n" "$( date )" "$*" >&2; }
trap 'echo $( date ) Backup interrupted >&2; send_admin_mail ERROR; exit 2' INT TERM
##
## At this place you could perform different tasks
## that will take place before the backup, e.g.
##
## - Create a list of installed software
## - Create a database dump
##
##
## Transfer the files into the repository.
## In this example the folders root, etc,
## var/www and home will be saved.
## In addition you find a list of excludes that should not
## be in a backup and are excluded by default.
##
echo "Transfer files ..."
borg create -v --stats \
$REPOSITORY::'{hostname}-{now}' \
/root \
/etc \
/var/www \
/var/lib \
/var/spool \
/home \
/mnt/HC_Volume_12737458/backup \
--exclude /dev \
--exclude /proc \
--exclude /sys \
--exclude /var/run \
--exclude /run \
--exclude /lost+found \
--exclude /var/lib/lxcfs \
--exclude /tmp \
--exclude /root/.cache
backup_exit=$?
info "Pruning repository"
# Use the `prune` subcommand to maintain 7 daily, 4 weekly and 6 monthly
# archives of THIS machine. The '{hostname}-' prefix is very important to
# limit prune's operation to this machine's archives and not apply to
# other machines' archives also:
borg prune -v \
--stats \
--list \
--prefix '{hostname}-' \
--show-rc \
--keep-hourly 12 \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 6 \
$REPOSITORY
prune_exit=$?
# use highest exit code as global exit code
global_exit=$(( backup_exit > prune_exit ? backup_exit : prune_exit ))
if [ ${global_exit} -eq 0 ]; then
info "Backup and Prune finished successfully"
subject="SUCCESS"
elif [ ${global_exit} -eq 1 ]; then
info "Backup and/or Prune finished with warnings"
subject="WARNING"
else
info "Backup and/or Prune finished with errors"
subject="ERROR"
fi
echo "###### Backup ended: $(date) ######"
send_admin_mail "$subject"
exit ${global_exit}
Resolve “-bash: ./borg_backup.sh: Permission denied”
Change the permissions of the file “borg_backup.sh” using the command below:
sudo chmod u+x borg_backup.sh
Crontab
If everything works fine, you can now run the script as a cronjob. Open crontab as root:
crontab -e
And add the following line to run a daily backup at 00:00.
0 0 * * * /home/server_backup/scripts/borg_backup.sh > /dev/null 2>&1
Comments
Post a Comment