Modifier la rétention des sauvegardes Borg

What app is this about, and its version: Borg 1.4.2~ynh1
What YunoHost version are you running: 12.1.35
What type of hardware are you using: Virtual machine

Describe your issue

Hello,
I searched (probably poorly) but couldn’t find how to change the retention of backups made with Borg.
Could someone help me please?

Bonjour,
j’ai cherché (sûrement mal) mais je n’ai pas trouvé comment modifier la rétention des sauvegardes faites avec Borg.
Quelqu’un pourrait m’aider svp ?

Share relevant logs or error messages

in this file:

/etc/yunohost/hooks.d/backup_method/05-borg_app

edit this line:

"$borg" prune --glob-archives "${name}-*" --keep-hourly 2 --keep-daily=7 --keep-weekly=8 --keep-monthly=12

When YNH upgrades borg app this file is copied to 05-borg_app.backup AND changed to default values.

Reference: borg prune — Borg - Deduplicating Archiver 1.4.2.post1 documentation

Ok thanks.
To check if it works, then run a manual backup or wait the next auto backup ?

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 :slightly_smiling_face:

1 Like

Hi @Nicouax !

First, THANK YOU ! Your reply is very awesome :slight_smile:

I’m trying your solution and i will come back to give my feedback.

1 Like

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.