Local and remote backups for macOS and Linux using BorgBackup

· 388 words · 2 minute read

Updates:

  • Oct 2018: there is now a more detailed guide available for macOS.
  • Sept 2018: there is now a hosting solution for Borg repos. See this post

When I recently switched my Macbook, I got very frustrated with Time Machine. I had used it for occasional local backups of my home folder and was planning to move my data from the new to the old machine.

Unfortunately, the Migration Assistant failed to even find my Time Machine drive and I ended up simply rsyncing everything from the Time Machine backup to a new user folder. After that was done I added a new user in macOS and it just ran a chmod over the whole folder.

After this experience it’s clear that you could as well do your backups with any tool that backs up files, while saving yourself the trouble of Time Machine completely.

The best tool I found for the job is BorgBackup. It supports compression, deduplication, remote backups and many kinds of filters. Doing an incremental backup of 1m files takes about 5 minutes locally. Here are some rough steps to get you started:

  1. Install Borg via Homebrew brew cask install borgbackup or Apt apt install borgbackup. If you plan on doing remote backups, Borg needs to be installed on the server as well.
  2. Initialize a new backup. For local backups:
    borg init --encryption=none /Volumes/external-disk/Backups/my-machine (I’m not using encryption here because the drive is encrypted). For remote backups it’s about the same:
    borg init --encryption=none my-backup-host.net:/backups/my-machine
  3. Next create a file called ~/.borg-filter. This will have the files you do NOT want to backup. An example:
*.ab
*/.DS_Store
*/.tox
/Users/manu/.cocoapods
/Users/manu/.Trash
/Users/manu/.pyenv/versions
/Users/manu/.gem
/Users/manu/.npm
/Users/manu/.cpanm

This will include some folders you can easily recreate. Last you should prepare a backup command. To backup specific important folders to a remote server, I use something like:

function borg-backup() {
  NOW=$(date +"%Y-%m-%d_%H-%M")
  borg create -v --stats -C zlib --list --filter=AM --exclude-from ~/.borg-filter $BORG_REPO::$NOW \
    ~/Desktop \
    ~/Documents \
    ~/Pictures \
    ~/Library/Fonts

  borg prune -v --list $BORG_REPO --keep-daily=3 --keep-weekly=4 --keep-monthly=12
}

This will backup my Desktop, Documents, Pictures and Fonts to a new time-stamped snapshot. The last command will rotate and delete old backups. The variable $BORG_BACKUP has the repo name chosen in the previous step.

Also be sure to read the official documentation to tune all options to your needs.