borg-backup-server: add script for pruning borg backups

This adds a script that performs a manual pruning of backup
directories.

Change-Id: I9559bb8aeeef06b95fb9e172a2c5bfb5be5b480e
This commit is contained in:
Ian Wienand 2021-02-09 10:03:10 +11:00
parent 98f3d42ab0
commit 4f0bfa6d9d
4 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,40 @@
#!/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

View File

@ -7,6 +7,14 @@
include_role:
name: install-borg
- name: Install prune script
copy:
src: 'prune-borg-backups.sh'
dest: '/usr/local/bin/prune-borg-backups'
owner: root
group: root
mode: '0755'
- name: Build all borg users from backup hosts
set_fact:
borg_users: '{{ borg_users }} + [ {{ hostvars[item]["borg_user"] }} ]'

View File

@ -88,3 +88,11 @@ def test_borg_backup(host):
# unmount it for sanity
cmd = host.run('umount /opt/backups')
assert cmd.succeeded
def test_borg_server_prune(host):
hostname = host.backend.get_hostname()
if hostname.startswith('borg-backup-test'):
pytest.skip()
cmd = host.run('echo "prune" | /usr/local/bin/prune-borg-backups 2>&1 > /var/log/prune-borg-backups.log')
assert cmd.succeeded

View File

@ -357,6 +357,9 @@
- playbooks/zuul/templates/host_vars/borg-backup
- testinfra/test_borg_backups.py
host-vars:
borg-backup01.region.provider.opendev.org:
host_copy_output:
'/var/log/prune-borg-backups.log': logs
borg-backup-test01.opendev.org:
host_copy_output:
'/var/log/borg-backup-borg-backup01.region.provider.opendev.org.log': logs