Hi @Maiphyl,
I recently set up backups with Borg and asked myself the very same question.
When using the Borg app, backups are triggered by the YunoHost hook mechanism and more specifically by the backup_method hook executed during the yunohost backup create command. For more informations about YunoHost hooks, see this page: Hooks | Yunohost
As @ghose pointed out, editing the prune command in the /etc/yunohost/hooks.d/backup_method/05-borg_app file allows you to customize the default backup retention values, but these changes will be overwritten with every Borg app update. For a slightly more robust solution, you can implement your own script that would run right after 05-borg_app in order to tighten Borg retention policy. Your custom script won’t be removed when the Borg app is updated.
Create the file that will contain your custom script:
sudo micro /etc/yunohost/hooks.d/backup_method/06-borg_app_overlay
I use Micro but any text editor will do (Nano is installed by default on all Debian distributions, I believe). You can name your file whatever you like but the “06-” prefix is important because it will make your script run after 05-borg_app.
This file must contain:
#!/usr/bin/env bash
# Custom backup_method hook overlay that tighten Borg retention policy
# Enforce pipe error management
set -Eeuo pipefail
# Set path to Borg binary
borg="/var/www/borg/venv/bin/borg"
# Load Borg app environment
if [[ -f /var/www/borg/.env ]]; then
set -a
source /var/www/borg/.env
set +a
fi
# Ensure Borg uses the same logging configuration as the main hook
export BORG_LOGGING_CONF='/var/www/borg/logging.conf'
# Set variables related to backup_method hook arguments
action="$1"
name="$3"
# Set retention variables
KEEP_DAILY=7
KEEP_WEEKLY=2
KEEP_MONTHLY=1
case "$action" in
need_mount)
# Do nothing
true
;;
backup)
echo "Overlay hook - Tightening retention policy for ${name}-*"
"$borg" prune --list --glob-archives "${name}-*" \
--keep-daily="${KEEP_DAILY}" \
--keep-weekly="${KEEP_WEEKLY}" \
--keep-monthly="${KEEP_MONTHLY}"
;;
mount)
# Do nothing
true
;;
*)
echo "Overlay hook - Unknown action: '$action'" >&2
exit 1
;;
esac
exit 0
You can edit the KEEP_DAILY, KEEP_WEEKLY and KEEP_MONTHLY variables to your liking.
You can then restrict file permissions (the script only needs to be readable by root):
sudo chmod 400 /etc/yunohost/hooks.d/backup_method/06-borg_app_overlay
To check if this works, I have not yet found a better way than waiting for the next scheduled backup (running yunohost backup create would also create local backups for all your apps, and running systemctl start borg wouldn’t go through YunoHost hook mechanism).
Keep in mind that after implementing your custom script, Borg will try to prune each repo twice per backup. This introduces a slight overhead and means this solution is only suitable if you want to keep fewer backups than what the Borg app offers by default (because your custom script will run after the 05-borg_app script has already removed some backups according to the default settings).
As I just came up with this solution, I would be glad to hear any ideas that could improve it or any alternative to modify Borg backup retention 