Automate Borg Backup

Aug 27, 2019 . 4 min read

The previous post, Borg Backup, explained installation and basics of Borg, and this post introduces Pybs, a simple python script to automate backups in Linux systems. To automate the backups, Pybs uses Python, YAML, Borg and Systemd. For non-linux platforms use some other task scheduler in place of Systemd.


Download Pybs from Github and extract it in your home dir.

Pybs, by default, uses Borg repository at $HOME/backup/borgrepo located in user home directory, and for sake of explanation, we stick with it. However, for actual backup, the repository should be located in a separate disk or device, otherwise the backup is lost in case of system failure.

To run, Pybs needs Python and Python YAML. Install them with,

$ sudo apt install python python-yaml

Pybs Setup

In all files, the paths are hard-coded as /home/x. Edit files listed below and change all occurrences of /home/x to absolute path of your home directory.




We explain the Pybs configuration bit later. Let’s run Pybs and do some backup.

Run Pybs as normal user

Create Borg repository with,

$ cd $HOME
$ mkdir backup
$ borg init -e none backup/borgrepo

Next, setup systemd daily backup.

$ mkdir -p .config/systemd/user
$ cp pybs/systemd/*.service .config/systemd/user
$ systemctl --user daemon-reload
$ systemctl --user start pybs-daily

To run Pybs service as normal user, create $HOME/.config/systemd/user dir and copy systemd service files to it. After reloading systemd daemon, start pybs-daily service. Wait for sometime and see the backup status with,

$ systemctl --user status pybs-daily

To enable timer, copy and start timer files with,

$ cp pybs/systemd/*.timer .config/systemd/user/
$ systemctl --user daemon-reload
$ systemctl --user enable pybs-daily.timer
$ systemctl --user start pybs-daily.timer    
$ systemctl --user list-timers

Run Pybs as super user

To backup system wide files and directories, run systemd scripts and timers as root. For that, first create Borg repository as explained above and then place service and timer files in /etc/systemd/system dir as,

## service

$ sudo cp pybs/systemd/*.service /etc/systemd/system
$ sudo systemctl daemon-reload
$ sudo systemctl start pybs-daily
$ sudo systemctl status pybs-daily

## timer

$ sudo cp pybs/systemd/*.timer /etc/systemd/system
$ sudo systemctl daemon-reload
$ sudo systemctl enable pybs-daily.timer
$ sudo systemctl start pybs-daily.timer
$ sudo systemctl list-timers

Pybs Configuration

Let’s create a new monthly backup service to know Pybs configuration. In pybs/systemd dir, create a new env file and name it as you like with following content,




Point environment variable REPO_PATH to absolute path of your borg repository and the BASE_DIR is absolute path of your pybs directory. The BACKUP_LIST is the name of the yaml file which specifies the list of directories or files to backup. The LABEL can be named anything and pybs use it select backups from the list file which has same label. For example, if label is foo then pybs selects only the items labeled as foo for backup. The DELAY_START_SEC is useful to delay the backup so as to reduce the load at the system startup.

Next, create systemd service file, which you may name as you like, in pybs/systemd.


Description=Borg Monthly Backup Service
ExecStart=/usr/bin/bash -c '${BASE_DIR}/script/'

Set EnvironmentFile variable to absolute path of env file which you have created. Leave all other things as it is.

We also need a systemd timer.


Description=Borg Monthly Backup Timer

The file should be named same as service file base name, but with .timer extension. The backup frequency is set with OnCalendar variable. Refer systemd documentation to know more about timers.

Finally, add backup entries for monthly backup. Label the backup sets with the label specified in env file.


    label: foo
      - /home/x/Music
      - /home/x/Music/*.wmv
    label: weekly
      - /home/x/Pictures

The new backup service backups only the Music dir excluding all wmv files.

Thats all there is to configure. To run new backup service, copy and enable service and timer units as explained in Run Pybs