Prune backups automatically over time.

This commit is contained in:
Andreas B. Mundt 2020-01-21 16:41:57 +01:00
parent 23766673be
commit e1766a7cfd

View file

@ -1,21 +1,23 @@
#!/bin/bash
set -eu
REPOSITORY="{{ backup_repo }}"
BACKUP=({{ backup_dirs|join(' ') }} {{ backup_dirs_extra|join(' ') }})
EXTRAOPTIONS=({{ backup_opts }} {{ backup_opts_extra }})
OPTIONS=({{ backup_opts }} {{ backup_opts_extra }})
export BORG_REPO="{{ backup_repo }}"
export BORG_PASSCOMMAND="cat {{ borg_pwd_file }}"
MOUNTED=""
MNT="$(echo "$REPOSITORY" | sed "s|\(^.*/mnt\).*|\1|")"
MNT="$(echo "$BORG_REPO" | sed "s|\(^.*/mnt\).*|\1|")"
if grep -q "$MNT" /etc/fstab ; then
[ -d "$REPOSITORY" ] || mount -v "$MNT" && MOUNTED="TRUE"
[ -d "$BORG_REPO" ] || mount -v "$MNT" && MOUNTED="TRUE"
fi
if [ ! -d "$REPOSITORY" ] ; then
mkdir -vp --mode=0750 "$REPOSITORY"
borg init --encryption=repokey "$REPOSITORY"
borg key export "$REPOSITORY" "{{ borg_key_backup }}"
if [ ! -d "$BORG_REPO" ] ; then
mkdir -vp --mode=0750 "$BORG_REPO"
borg init --encryption=repokey
borg key export "{{ borg_key_backup }}"
fi
if [ -e "{{ nc_dir }}/config/config.php" ] ; then
@ -30,9 +32,14 @@ if [ -e "{{ nc_dir }}/config/config.php" ] ; then
echo "done."
fi
ARCHIVE="$(date +%Y-%m-%d-%H:%M)"
echo "Backup ${BACKUP[@]} to $REPOSITORY."
borg create -v "${EXTRAOPTIONS[@]}" "$REPOSITORY::$ARCHIVE" "${BACKUP[@]}"
echo "Backup ${BACKUP[@]} to $BORG_REPO."
borg create -v --show-rc "${OPTIONS[@]}" ::'{hostname}-{now}' "${BACKUP[@]}"
borg prune -v --list --prefix='{hostname}-' --show-rc \
--keep-daily 14 \
--keep-weekly 8 \
--keep-monthly 6 \
--keep-yearly 10
if [ -e "{{ nc_dir }}/config/config.php" ] ; then
sudo -u www-data /usr/bin/php {{ nc_dir }}/occ maintenance:mode --off