Code

Renaming borgmatic backups

I backed up a component using borgmatic, but I forgot to specify the hostname for the borgmatic container in the docker compose config. This freaked me out because backups disappeared all of a sudden.

I was looking for backups with:

docker compose exec backup borgmatic list

And there was nothing.

The cause of the problem was the default backup naming convention used by borgmatic, which uses the hostname, and which changes on each container restart.

I was able to see all backups with:

docker compose exec backup borg list $REPOSITORY

To fix this, I specified the missing hostname on the borgmatic container, and then ran this script to rename pre-existing backups:

#!/usr/bin/env bash

set -ex

REPOSITORY=$1
EXPECTED_PREFIX=$2

for line in $(docker compose exec backup borg list --format '{archive}{NL}' $REPOSITORY); do
  if [[ $line == "$EXPECTED_PREFIX"-* ]]; then
    continue
  fi
  new_line=$(echo $line | sed -e "s/^[a-z0-9]\+/$EXPECTED_PREFIX/g")
  docker compose exec backup borg rename $REPOSITORY::$line $new_line
done