From 90b5fc0f886cdc74eb731e946a0cc4d84631bafa Mon Sep 17 00:00:00 2001 From: rnhmjoj Date: Tue, 11 Jul 2023 12:15:05 +0200 Subject: [PATCH] jobs: prune backups every 7 days --- jobs.nix | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/jobs.nix b/jobs.nix index 2c712dd..665aa55 100644 --- a/jobs.nix +++ b/jobs.nix @@ -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 = let - saved = '' + saved = pkgs.writeText "backup-saved" '' /etc/lvm /var/lib /home ''; - excluded = '' + excluded = pkgs.writeText "backup-excluded" '' /var/lib/systemd /var/lib/udisks2 /var/lib/postgresql @@ -74,7 +84,8 @@ with lib; in { 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 onFailure = [ "notify-failed@backup.service" ]; @@ -88,29 +99,24 @@ with lib; path = with pkgs; [ bup git nfs-utils sudo gzip postgresql ]; script = '' - set -e - # mount repository mkdir -p "$BUP_DIR" - mount.nfs -o nolock 192.168.1.3:/maxwell "$BUP_DIR" # init backup ! test -e $BUP_DIR/bupindex && bup init # build indices and save - saved=${pkgs.writeText "backup-saved" saved} - excluded=${pkgs.writeText "backup-excluded" excluded} while read -r dir; do name=$(basename "$dir") echo indexing $name... - bup index "$dir" --exclude-from="$excluded" + bup index "$dir" --exclude-from="${excluded}" echo done echo saving $name... bup save -n "$name" "$dir" echo done - done < "$saved" + done < "${saved}" # postgresql backup dir=/tmp/postgresql @@ -125,9 +131,12 @@ with lib; bup save -n postgresql "$dir" --strip-path=/tmp echo done - echo pruning... - bup prune-older --keep-all-for 6m --keep-monthlies-for 2y --unsafe - echo done + # prune backups every week + if test $(( $(date +%s) / 86400 % 7 )) -eq 0; then + echo pruning... + bup prune-older --keep-all-for 6m --keep-monthlies-for 2y --unsafe + echo done + fi ''; };