Clark Boylan cb8226128e Skip purged borg backups during backup pruning
We recently updated our borg backup system to support explicit purging
of backups. When purged there are no longer any backups to prune so we
need to skip any backups in this state. Update the pruning script to do
so.

Change-Id: Ib23a72c949c05855176fe54637f406b7797b37ae
2024-11-25 09:17:57 -08:00

68 lines
2.2 KiB
Bash

#!/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=''
if [ -z ${NO_LOG_FILE+x} ]; then
LOG_FILE="/opt/backups/prune-$(date '+%Y-%m-%d-%H-%M-%S').log"
echo "*** Logging output to ${LOG_FILE}"
exec 1>${LOG_FILE}
exec 2>&1
fi
else
echo "*** Invalid input"
exit 1
fi
pushd /opt/backups
for u in borg-*; do
BORG_BASE=/opt/backups/$u
BORG_REPO=${BORG_BASE}/backup
BORG_RETIRED=${BORG_BASE}/.retired
_prune_flags='--keep-daily 7 --keep-weekly 4 --keep-monthly 12'
_retired=''
if [[ -f "${BORG_RETIRED}" ]] && [[ ! -d "${BORG_REPO}" ]]; then
# This repo was retired and purged. We don't need to prune it.
echo "$(date) Skipping ${BORG_REPO} it is retired and purged."
continue
elif [[ -f "${BORG_RETIRED}" ]]; then
_prune_flags='--keep-daily 1'
_retired=' (retired)'
fi
sudo BORG_OP=${BORG_OP} BORG_UNKNOWN_UNENCRYPTED_REPO_ACCESS_IS_OK=yes BORG_REPO=${BORG_REPO} _retired="${_retired}" _prune_flags="${_prune_flags}" -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
# Long-running aborted backups might leave a ".checkpoint" archive around; ignore
# these as prune will remove them automatically
#
# Note we are assuming the archives are in the format made by our backup scripts,
# which include -YYYY-MM-DDTHH:MM:SS on the end.
archives=$(/opt/borg/bin/borg list ${BORG_REPO} | awk '$1 !~ /\.checkpoint$/ { print substr($1, 0, length($1)-20) }' | sort | uniq)
echo "+------"
echo "| $(date) Pruning ${BORG_REPO}${_retired}"
for prefix in ${archives};
do
echo "| $(date) - archive ${prefix}"
/opt/borg/bin/borg prune --prefix ${prefix} ${BORG_OP} --verbose --list --show-rc ${_prune_flags}
done
echo "| $(date) done!"
echo "+------"
echo
EOF
done