Automated backup solution using Borg Backup for critical system directories.
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.
Required packages:
borgbackup
: For creating and managing backupslogrotate
: For managing log file rotationcron
orsystemd
: For scheduling backup jobs.
You can install them on Arch Linux with:
pacman -S borg cronie logrotate
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.
- Clone this repository
- Ensure you have all required dependencies installed
- Make sure you have sufficient permissions to access the backup locations
cp dist.env conf.env
and modifyconf.env
accordingly- Set up the encryption passphrase:
echo "BORG_PASSPHRASE={your_long_passphrase}" >> /etc/environment
From conf.env
:
BACKUP_FOLDER
: (Required) The destination folder where backups will be storedBACKUP_RETENTION_DAYS
: (Optional) How long (in days) to keep the backups before pruning. Default: 7FOLDERS_TO_BACKUP
: (Required) Which top-level system folders to backupCRON_SCHEDULE
: (Required) The schedule for automatic backups. The format depends on the trigger mechanism (eithercron
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.
The following directories are backed up by default:
- /home
- /etc
- /boot
You can change the directories for backup by modifying conf.env
.
Deploy the backup system:
sudo bash deploy.sh
The default schedule 0 13 * * *
runs backups daily at 13:00.
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
- 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
- If backups fail, check the logs in
/var/log/borg_backups/
. Alternatively, if usingsystemd
, check the logs usingjournalctl -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