jobs: prune backups every 7 days
This commit is contained in:
parent
5bac3f4410
commit
90b5fc0f88
29
jobs.nix
29
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
|
||||
|
||||
# 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
|
||||
'';
|
||||
};
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user