Skip to content

gchamon/borg-automated-backups

Repository files navigation

borg-automated-backups

Automated backup solution using Borg Backup for critical system directories.

Description

This project provides scripts to automatically backup important system directories using Borg Backup. It includes automatic log rotation and scheduled backups via cron jobs. While similar functionality can be achieved using borgmatic, this project offers a lightweight, customizable solution focused on basic backup needs.

Dependencies

Required packages:

  • borgbackup: For creating and managing backups
  • logrotate: For managing log file rotation
  • cron or systemd: For scheduling backup jobs.

You can install them on Arch Linux with:

pacman -S borg cronie logrotate

Choosing between systemd and cron

systemd is the recommended package. cron is significantly simpler than systemd, but it requires the system being active at the backup schedule, otherwise the backup won't trigger. systemd will trigger the backup as soon as the system comes online after the trigger schedule. This means that with systemd the system will never miss a backup, while with cron it can potentially never trigger the backup, if the trigger schedule is poorly chosen.

Preparation steps

  1. Clone this repository
  2. Ensure you have all required dependencies installed
  3. Make sure you have sufficient permissions to access the backup locations
  4. cp dist.env conf.env and modify conf.env accordingly
  5. Set up the encryption passphrase:
echo "BORG_PASSPHRASE={your_long_passphrase}" >> /etc/environment

Configuration

Environment Variables

From conf.env:

  • BACKUP_FOLDER: (Required) The destination folder where backups will be stored
  • BACKUP_RETENTION_DAYS: (Optional) How long (in days) to keep the backups before pruning. Default: 7
  • FOLDERS_TO_BACKUP: (Required) Which top-level system folders to backup
  • CRON_SCHEDULE: (Required) The schedule for automatic backups. The format depends on the trigger mechanism (either cron orsystemd). Note that the program won't validate the schedule syntax and will accept it as-is.
  • CRON_TRIGGER: (Required) Which mechanism to trigger the cron jobs. Accepted values: cron, systemd
  • DEPLOY_BORG_BINARY: (Optional) Set to "true" if you want the script to deploy its own Borg binary instead of using the system package. Defaults to false. When set, it downloads the binary from Borg Backup release page.

From /etc/environment:

  • BORG_PASSPHRASE: (Required) The passphrase used to encrypt your backups.

Default Backup Directories

The following directories are backed up by default:

  • /home
  • /etc
  • /boot

You can change the directories for backup by modifying conf.env.

Installation

Deploy the backup system:

sudo bash deploy.sh

The default schedule 0 13 * * * runs backups daily at 13:00.

Removal

To reverse the deployment of this tools, you just have to remove the deployed files and stop the systemd unit:

sudo rm -rf /opt/borg-backups/*.sh /var/log/borg_backups/

# Remove cron schedule if necessary
[[ -f "/etc/cron.d/borg_backup" ]] && sudo rm -rf "/etc/cron.d/borg_backup"

# Remove systemd unit files if necessary
if [[ -f "/etc/systemd/system/borg-backup.timer" ]]; then
  sudo systemctl disable --now borg-backup.timer
  sudo rm -rf /etc/systemd/system/borg-backup.{timer,service}
  sudo systemctl daemon-reload
fi

Backup Strategy

  • Daily backups are kept for 7 days
  • Logs are rotated weekly and kept for 2 rotations
  • Backups are compressed using LZ4 compression
  • Each directory is backed up to its own Borg repository

Troubleshooting

  • If backups fail, check the logs in /var/log/borg_backups/. Alternatively, if using systemd, check the logs using journalctl -u borg-backup
  • Ensure BORG_PASSPHRASE is correctly set in /etc/environment
  • Verify that the backup destination has sufficient space
  • Check that the backup user has appropriate permissions

About

Automated backup solution using Borg Backup for critical system directories

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published