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