jobs: prune backups every 7 days

This commit is contained in:
Michele Guerini Rocco 2023-07-11 12:15:05 +02:00
parent 5bac3f4410
commit 90b5fc0f88
Signed by: rnhmjoj
GPG Key ID: BFBAF4C975F76450

View File

@ -55,16 +55,26 @@ with lib;
''; '';
}; };
systemd.mounts = lib.singleton
{
description = "backup NFS volume";
after = [ "network-online.target" ];
what = "192.168.1.3:/maxwell";
where = "/mnt/backup";
type = "nfs";
options = "nolock";
};
systemd.services.backup = systemd.services.backup =
let let
saved = '' saved = pkgs.writeText "backup-saved" ''
/etc/lvm /etc/lvm
/var/lib /var/lib
/home /home
''; '';
excluded = '' excluded = pkgs.writeText "backup-excluded" ''
/var/lib/systemd /var/lib/systemd
/var/lib/udisks2 /var/lib/udisks2
/var/lib/postgresql /var/lib/postgresql
@ -74,7 +84,8 @@ with lib;
in { in {
description = "system backup"; description = "system backup";
after = [ "network-online.target" ]; after = [ "network-online.target" "mnt-backup.mount" ];
bindsTo = [ "mnt-backup.mount" ];
startAt = "*-*-* 03:00"; # every day at 3:00 startAt = "*-*-* 03:00"; # every day at 3:00
onFailure = [ "notify-failed@backup.service" ]; onFailure = [ "notify-failed@backup.service" ];
@ -88,29 +99,24 @@ with lib;
path = with pkgs; [ bup git nfs-utils sudo gzip postgresql ]; path = with pkgs; [ bup git nfs-utils sudo gzip postgresql ];
script = '' script = ''
set -e
# mount repository # mount repository
mkdir -p "$BUP_DIR" mkdir -p "$BUP_DIR"
mount.nfs -o nolock 192.168.1.3:/maxwell "$BUP_DIR"
# init backup # init backup
! test -e $BUP_DIR/bupindex && bup init ! test -e $BUP_DIR/bupindex && bup init
# build indices and save # build indices and save
saved=${pkgs.writeText "backup-saved" saved}
excluded=${pkgs.writeText "backup-excluded" excluded}
while read -r dir; do while read -r dir; do
name=$(basename "$dir") name=$(basename "$dir")
echo indexing $name... echo indexing $name...
bup index "$dir" --exclude-from="$excluded" bup index "$dir" --exclude-from="${excluded}"
echo done echo done
echo saving $name... echo saving $name...
bup save -n "$name" "$dir" bup save -n "$name" "$dir"
echo done echo done
done < "$saved" done < "${saved}"
# postgresql backup # postgresql backup
dir=/tmp/postgresql dir=/tmp/postgresql
@ -125,9 +131,12 @@ with lib;
bup save -n postgresql "$dir" --strip-path=/tmp bup save -n postgresql "$dir" --strip-path=/tmp
echo done echo done
echo pruning... # prune backups every week
bup prune-older --keep-all-for 6m --keep-monthlies-for 2y --unsafe if test $(( $(date +%s) / 86400 % 7 )) -eq 0; then
echo done echo pruning...
bup prune-older --keep-all-for 6m --keep-monthlies-for 2y --unsafe
echo done
fi
''; '';
}; };