Ian Wienand 3d63b3b8a4 borg-backup-server: log prune output to file
This saves prune output to a log file automatically.  Add a bit more
info on the process too.

Change-Id: I2607ddbc313dfebc122609af78bb5eed63906f6b
2021-08-04 14:47:50 +10:00

45 lines
1.3 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=''
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
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 "| $(date) 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