1

Background

I would like to use Borg Backup as a backup solution for my Ubuntu server. They offer a very convenient sh script that they say you can just run as a cron job, but it is not working for me.

Problem

How do I automate borg backup? If the best way is to use cron, then what is wrong with my impementation?

Data

Root's Crontab:

# 5 2 * * * sh /home/***/Scripts/borgBackup_run.sh >/dev/null      # Back up the root partition daily (2:05a)
* * * * * sh /home/***/Scripts/borgBackup_run.sh >/dev/null      # Test the script

borgBackup_run.sh:

Modified from the original to be very bare-bones:

#!/bin/sh

echo 'Starting backup now...' >> 
/home/***/Logs/borgBackup_create.log

#/usr/bin/borg create --verbose --filter AME --list --stats --show-rc --compression lz4 --exclude-caches --one-file-system --exclude-from ~/Scripts/borgBackup_exclude.txt /mnt/RootBackup/borg::'{hostname}-{now:%Y-%m-%d}' /
/usr/bin/borg create /mnt/RootBackup/borg::cronTest /home/***/Logs/ # Backup a small folder as a test

echo 'Done.' >> /home/***/Logs/borgBackup_create.log

"ls -l ~/Scripts/borgBackup_run.sh"

-rwxr-xr-x 1 root root 455 Sep 27 12:34 Scripts/borgBackup_run.sh

Notes:

When borgBackup_run.sh is run from the commandline, as: sudo Scripts/borgBackup_run.sh, the script works perfectly, creating a new archive in /mnt/RootBackup/borg.

Additionally, after each minute (when the cronjob runs), the .log file at /home/***/Logs/borgBackup_create.log shows both Starting backup now... and Done., indicating that the script has run all the way through, but borg did not work for some reason.

I have tried removing sh from the crontab file, using bash instead, and changing the interobang to #!/bin/bash instead of #!/bin/sh. I have also tried wrapping the line /user/bin/borg create ... line in borgBackup_run.sh in bash -c "...".

Any help would be greatly appreciated!

  • 2
    And what does borg itself say? I see you've tried options like --verbose and --stats. Surely the stdout/stderr goes somewhere; either to root's email inbox, or to the local /var/spool/mail/root mailbox, or at least to syslog. – grawity Sep 27 '18 at 17:58
  • 2
    Change the end of the crontab entry from ‘>/dev/null’ to ‘>/path/to/somewhere/cron_output 2>&1’ and see if you catch anything meaningful in that file. Also the /usr/bin/borg..... line of the script doesn’t seem to be re-directing output to your .log file. Currently output would be sent to /dev/null – Tyson Sep 27 '18 at 18:12
  • Thank you for both of your comments! @grawity: I previously had the stdout of borg directed to that same borgBackup_create.log file, however I will try redirecting strerr, as well. – sgbrown Sep 27 '18 at 18:34
  • @Tyson: I had directed the stdout and stderr of the script in the cron file to a log previously. It creates the .log file, but does not add any content. – sgbrown Sep 27 '18 at 18:35
  • Just suggesting what I’ve done in the past to solve cron issues. In one case a typo didn’t become obvious until I quit trashing stdout and errout at the cron level. – Tyson Sep 28 '18 at 12:49
1

I figured out the problem, and am posting the solution in case anyone in the future has a similar problem.

I set up my repository to be unencrypted, which caused borg to attempt to run interactively (asking stdout if backing up to an unencrypted repository is ok). To make borg run non-interactively, I had to export an "automatic answerer" in borgBackup_run.sh, before the call to borg create .... The new line in the script looks like:

export BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes

The name of this variable was found in the man page for borg. A number of other "automatic answerers" can also be found in the man page, to ensure that borg runs non-interactively in a script.

1

I just finished creating a similar cron script. Included is a sync of the borg backup files to BackBlaze's cloud storage platform (similar to amazon s3, but cheaper). Hopefully this will be useful to someone else looking for automating borg with a low-cost offsite backup!

#!/bin/sh

echo "Starting backup for `date`\n"

# setup script variables
export BORG_PASSPHRASE="secret-passphrase-here!"
export BORG_REPO="/path/to/repo"
export BACKUP_TARGETS="/path1/to/backup /path2/to/backup"
export BACKUP_NAME="backup-and-remote-folder-name"

# create borg backup archive
cmd="borg create ::`date +%Y%m%d`-$BACKUP_NAME $BACKUP_TARGETS --stats"
$cmd

# prune old archives to keep disk space in check
borg prune -v --list --keep-daily=3 --keep-weekly=2

# sync backups to offsite storage
b2 authorize-account accountID applictionKey
b2 sync --delete --replaceNewer $BORG_REPO b2://bucket-name/$BACKUP_NAME

# all done!
echo "Backup complete at `date`\n";

Currently running once a day with the following crontab

0 11 * * * /path/to/script/backup.sh >> /path/to/logfile/backup.log 2>&1

Requires a BackBlaze account and their CLI tool CLI installati

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.