#!/bin/bash set -e echo "This script will prune each archive in the backups of all backed up hosts" echo "Enter 'noop' to test, or 'prune' to actually prune" read -p "Operation: " borg_op if [[ ${borg_op} == 'noop' ]]; then BORG_OP='--dry-run' elif [[ ${borg_op} == 'prune' ]]; then BORG_OP='' else echo "*** Invalid input" exit 1 fi pushd /opt/backups for u in borg-*; do BORG_REPO=/opt/backups/$u/backup sudo BORG_OP=${BORG_OP} BORG_RELOCATED_REPO_ACCESS_IS_OK=y BORG_REPO=${BORG_REPO} -u ${u} -s <<'EOF' # Look at all archives and strip the timestamp, leaving just the archive names # We limit the prune by --prefix so each archive is considered separately archives=$(/opt/borg/bin/borg list ${BORG_REPO} | awk '{$1 = substr($1, 0, length($1)-20); print $1}' | sort | uniq) for prefix in ${archives}; do echo echo echo "+------" echo "| Pruning ${BORG_REPO} archive ${prefix}" echo "+------" /opt/borg/bin/borg prune --prefix ${prefix} ${BORG_OP} --verbose --list --show-rc --keep-daily 7 --keep-weekly 4 --keep-monthly 12 done EOF done