borgctl - borgbackup without bash scripts
borgbackup is cool and easy to use. But we all end up writing bash scripts for creating/listing/pruning/checking/... backups. borgctl is a borg wrapper that uses yaml configuration files to make life a bit easier.
Motivation (my use cases)
I don't want to write a bash script on every server to make backups. I don't want to have an extra script for listing/pruning backups. I want to backup to multiple storage backends. I backup different things to different remote backends (money constraint). I don't want to store my borg passphrase on disk. I also don't want to enter it for every backend. I want to make backups in append-only-mode. To prune/compact backups, Yubikey authentication is mandatory. I want to see when my last backup was done (monitoring). I want usability and easy deployment.
Now, I make backups with
borgctl -c backend1-append.yml -c backend2-append.yml -c backend3-append.yml create
I prune/compact backups with (needs Yubikey)
borgctl -c backend1-full.yml -c backend2-full.yml -c backend3-full.yml --cron
My i3 status bar shows a red B:7d if my last backup is 7 days old. On some servers, I like to create, prune and compact backups by just using borgctl --cron. There is also borgmagic, but it does not handle all my use cases (to admit: I first reinvented the wheel and then stumbled across borgmatic. I also thought it just takes a day to write a small python script ...)
Features
Yaml configuration files: Specify what to backup and where to backup
Usability: Just call borgctl create, borgctl list, borgctl prune, ...
Flexibility: Run borgctl with multiple configuration files to handle different use cases (e. g. backup to multiple remote backends or run borg create in append-only-mode and borg prune/compact with a Yubikey)
Ask the user for the borg passphrase (if you don't want to store it on disk, just specify ask as passphrase in the config file). Re-use the entered password for other remote storage backends (you don't have to re-enter it again)
For semi-automated setups: There are some helpers to generate ssh keys and print the authorized_keys entry (restrict access to repository)
Run multiple borg commands by running borgctl --cron (like create + prune + compact, commands can be specified in the config file)
Monitoring: Write a state file (text file with timestamp) after successfully executing borg commands
Logging: Write everything to a log file that automatically rotates
Easy to deploy and use
Quickstart

Usage
kmille@linbox:~ borgctl --help
usage: borgctl \[-h\] \[-l \[LIST\]\] \[-d\] \[-s\] \[-a\] \[-c CONFIG\] \[--cron\] \[-p\] \[--version\] \{break-lock,check,compact,config,create,delete,diff,extract,export-tar,import-tar,info,init,key,list,mount,prune,rename,umount,upgrade,with-lock\} ...
borgctl is a simple borgbackup wrapper. Running version 0.4.9.
The working directory is /etc/borgctl/ for root or $XDG\_CONFIG\_HOME/borgctl or ~/.config/borgctl for non-root users.
The log directory is /var/log/borgctl/ for root or $XDG\_STATE\_HOME/borgctl or ~/.local/state/borgctl for non-root users.
positional arguments:
\{break-lock,check,compact,config,create,delete,diff,extract,export-tar,import-tar,info,init,key,list,mount,prune,rename,umount,upgrade,with-lock\}
options:
-h, --help show this help message and exit
-l \[LIST\], --list \[LIST\]
list existing borgctl config files in /home/kmille/.config/borgctl
-d, --generate-default-config
write default config to /home/kmille/.config/borgctl/default.yml or prints it to stdout if the file already exists
-s, --generate-ssh-key
write a new ed25519 ssh key to ~/.ssh/borg\_$config and update the config file
-a, --generate-authorized-keys
print the authorized\_keys entry. It also shows the retricted entry
-c CONFIG, --config CONFIG
specify the config file to use. Defaults to default.yml. You can specify multiple config files with -c default.yml -c local-disk.yml. If the config file contains a /, then a relative/absolute path is asumed. If not, /home/kmille/.config/borgctl/$config will be used
--cron run multiple borg commands in a row. The commands to run are specified in the config file (cron\_commands)
-p, --generate-passphrase
generate a diceware like passphrase
--version show version and exit
Installation
You can find the latest release on the Github Release page. There are different ways to get borgctl up and running.
Use the PyPi package with
pip install borgctl
The source and binary package can also be found on the Release page (*.whl and *.tar.gz)
Use the Debian package
Install the latest package with:
cd /tmp
export LATEST\_RELEASE=$(curl -s https://api.github.com/repos/kmille/borgctl/tags | jq -r '.\[0\].name')
wget "https://github.com/kmille/borgctl/releases/download/$LATEST\_RELEASE/borgctl\_$LATEST\_RELEASE-1\_amd64.deb"
apt install ./borgctl\*.deb
rm ./borgctl\*.deb
The deb package should work on all Debian based distros. It is created by create-deb-package.sh. It requires borgbackup and python3-ruamel.yaml as dependencies.
yay borgctl
You can also download borgctl-*any.pkg.tar.zst and install it with pacman -U.
Configuration
borgctl uses config files. If you run borgctl without specifying a config file, it expects a default.yml in the config directory. You can specify one or more config files with -c/--config. If the config file contains a /, the config file is interpreted as relative/absolute path. There is also a logging configuration (logging.conf) stored, which is used by borg and borgctl itself.
The output of borg and borgctl will be written to borg.log. The file gets logrotated automatically.
The state files are also written to the log directory. You can use borgctl --list to list all config files.
For example:
kmille@linbox:~ sudo borgctl --list
backend1-append.yml
...
kmille@linbox:~ sudo borgctl list
2024-01-09 10:37:17,967 INFO Using config file /etc/borgctl/default.yml
kmille@linbox:~ sudo borgctl -c backend1-append.yml list
2024-01-09 10:37:55,148 INFO Using config file /etc/borgctl/backend1-append.yml
kmille@linbox:/etc sudo borgctl -c borgctl/backend1-append.yml list
2024-01-09 10:38:47,498 INFO Using config file borgctl/backend1-append.yml
Default configuration file generated by borgclt --generate-default-config:
---
repository: ""
ssh_key: ""
prefix: ""
passphrase: ""
mount_point: "/mnt"
borg_create_backup_dirs:
- "/"
borg_create_excludes:
- ".cache"
borg_create_arguments:
- "--one-file-system"
- "--compression=lz4"
borg_prune_arguments:
- "--keep-last 10"
- "--dry-run"
cron_commands:
- "create"
- "prune"
- "compact"
state_commands:
- "create"
- "prune"
ignore_return_codes: []
# https://borgbackup.readthedocs.io/en/stable/usage/general.html#environment-variables
envs:
"BORG_RELOCATED_REPO_ACCESS_IS_OK": "yes"
"BORG_EXIT_CODES": "modern"
borg_binary: "/usr/bin/borg"
Walkthrough/How borgctl behaves
borgctl needs a configuration file. If you run it without specifying one, a default.yml in the default config location is expected to exist. If you run it and there is no default.yml, you can use --generate-default-config to create one:
root@linbox:~ borgctl list
2023-12-26 10:58:31,564 INFO Using config file /etc/borgctl/default.yml
2023-12-26 10:58:31,564 ERROR Could not load config file /etc/borgctl/default.yml
Please use --generate-default-config to create a default config
root@linbox:~ borgctl --generate-default-config
2023-12-26 10:59:28,107 INFO Please make a backup of the passphrase!
2023-12-26 10:59:28,112 INFO Successfully wrote config file to /etc/borgctl/default.yml
/etc/borgctl/default.yml is based on the default config file and gets new diceware-like password for the borg passphrase . Also the hostname is set as borg prefix. It's crucial to make a backup of the passphrase!
Generating ssh keys
root@linbox:~ borgctl --generate-ssh-key
2023-12-26 11:06:12,784 INFO Using config file /etc/borgctl/default.yml
2023-12-26 11:06:12,785 INFO Running \['ssh-keygen', '-t', 'ed25519', '-N', '', '-q', '-f', '/root/.ssh/borg\_default', '-C', 'kmille\_borg\_default@linbox'\]
2023-12-26 11:06:12,794 INFO Successfully created ssh key /root/.ssh/borg\_default
2023-12-26 11:06:12,805 INFO Updated ssh\_key in /etc/borgctl/default.yml
root@linbox:~ cat /root/.ssh/borg\_default.pub
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINpVZZjIAzeyGq0oLKVeLzEECoe7RXg6YpAcPIsakboF kmille\_borg\_default@linbox
If you specify a ssh key in the config file, it will be used as output file (if it does not exist). If not, the default name for the ssh key is $currentUsername\_borg\_$configPrefix.
Generating authorized_keys entry
root@linbox:~ borgctl --generate-authorized\_keys
2023-12-26 11:07:47,549 INFO Using config file /etc/borgctl/default.yml
2023-12-26 11:07:47,551 INFO Using ssh key /root/.ssh/borg\_default.pub from config file
2023-12-26 11:07:47,551 INFO Add this line to authorized\_keys:
ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINpVZZjIAzeyGq0oLKVeLzEECoe7RXg6YpAcPIsakboF kmille\_borg\_default@linbox
2023-12-26 11:07:47,551 INFO Use this line for restricted access:
command="borg serve --restrict-to-path /opt/test-backup",restrict ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINpVZZjIAzeyGq0oLKVeLzEECoe7RXg6YpAcPIsakboF kmille\_borg\_default@linbox
2023-12-26 11:07:47,553 INFO Or this all-in-one command:
echo -e 'command="borg serve --restrict-to-path /opt/test-backup",restrict ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINpVZZjIAzeyGq0oLKVeLzEECoe7RXg6YpAcPIsakboF kmille\_borg\_default@linbox\\n' | ssh backup-host1 'cat \>\> /home/backuper/.ssh/authorized\_keys'
List all config files
root@linbox:~ borgctl --list
backend1-append.yml
backend2-append.yml
backend1-full.yml
backend2-full.yml
root@linbox:~ borgctl --list full
backend1-full.yml
backend2-full.yml
Running borg commands with borgctl
Please update the config file (what to backup (borg\_create\_backup\_dirs and borg\_create\_excludes and where to backup (repository)). Then you can create your first backup:
root@linbox:~ borgctl create
2023-12-26 11:11:35,197 INFO Using config file /etc/borgctl/default.yml
2023-12-26 11:11:35,199 INFO Executing: BORG\_REPO="/root/borg-repo" BORG\_LOGGING\_CONF="/etc/borgctl/logging.conf" BORG\_RSH="ssh -i /root/.ssh/borg\_default" BORG\_RELOCATED\_REPO\_ACCESS\_IS\_OK="yes" /usr/bin/borg --verbose create --progress --stats --one-file-system --compression=lz4 --exclude=.cache ::linbox\_2023-12-26\_11:11:35 /usr/bin
2023-12-26 11:11:35,718 INFO Creating archive at "/root/borg-repo::linbox\_2023-12-26\_11:11:35"
2023-12-26 11:12:07,934 INFO ------------------------------------------------------------------------------
2023-12-26 11:12:07,935 INFO Repository: /root/borg-repo
2023-12-26 11:12:07,936 INFO Archive name: linbox\_2023-12-26\_11:11:35
2023-12-26 11:12:07,937 INFO Archive fingerprint: 079e9c226455ff1b816be986bf49029c3cdf28ec04e9f6bf2d387ea9a4f24d87
2023-12-26 11:12:07,937 INFO Time (start): Tue, 2023-12-26 11:11:35
2023-12-26 11:12:07,938 INFO Time (end): Tue, 2023-12-26 11:12:06
2023-12-26 11:12:07,938 INFO Duration: 31.04 seconds
2023-12-26 11:12:07,939 INFO Number of files: 3869
2023-12-26 11:12:07,940 INFO Utilization of max. archive size: 0%
2023-12-26 11:12:07,940 INFO ------------------------------------------------------------------------------
2023-12-26 11:12:07,941 INFO Original size Compressed size Deduplicated size
2023-12-26 11:12:07,942 INFO This archive: 1.55 GB 777.29 MB 777.13 MB
2023-12-26 11:12:07,942 INFO All archives: 1.55 GB 777.29 MB 777.43 MB
2023-12-26 11:12:07,943 INFO
2023-12-26 11:12:07,943 INFO Unique chunks Total chunks
2023-12-26 11:12:07,944 INFO Chunk index: 4310 4322
2023-12-26 11:12:07,945 INFO ------------------------------------------------------------------------------
2023-12-26 11:12:08,101 INFO Updated state file /var/log/borgctl/borg\_state\_default\_create.txt
root@linbox:~
root@linbox:~ cat /var/log/borgctl/borg\_state\_default\_create.txt
2023-12-26\_11:12:08\#
root@linbox:~
root@linbox:~ ls /var/log/borgctl/borg.log
-rw-r--r-- 1 root root 11K Dec 26 11:12 /var/log/borgctl/borg.log
The executed borg command is also printed to stderr. You can adjust the parameters borgctl uses. For example you can add --json /usr/local/bin/, which will be added to borg: