puter/hosts/vessel/backup.nix

64 lines
1.8 KiB
Nix
Raw Normal View History

2024-02-04 20:51:11 +00:00
{
2024-02-26 18:27:27 +00:00
attrName,
config,
2024-02-04 20:51:11 +00:00
lib,
2024-02-26 18:27:27 +00:00
pkgs,
2024-02-04 20:51:11 +00:00
...
2024-02-26 18:27:27 +00:00
}: let
2024-08-18 00:55:37 +00:00
backupPath = "/srv/backup";
backups = {
storage = "/srv/storage";
safe = "/srv/safe";
sync = config.services.syncthing.dataDir;
2024-02-04 20:51:11 +00:00
};
2024-08-18 00:55:37 +00:00
in {
systemd = lib.mkMerge (map (
backupName: let
systemdName = "${backupName}-backup";
in {
timers.${systemdName} = {
description = "Local rsync Backup ${backupName}";
wantedBy = ["timers.target"];
timerConfig = {
OnCalendar = "*-*-* 03:00:00";
Persistent = true;
Unit = "${systemdName}.service";
};
};
2024-02-04 20:51:11 +00:00
2024-08-18 00:55:37 +00:00
services.${systemdName} = {
description = "Local rsync Backup ${backupName}";
serviceConfig = {
Type = "oneshot";
User = "root";
Group = "root";
};
script = ''
${lib.getExe pkgs.rsync} --verbose --verbose --archive --update --delete --mkpath ${backups.${backupName}} ${backupPath}/${backupName}/
'';
};
}
) (lib.attrNames backups));
2024-02-04 20:51:11 +00:00
2024-08-18 00:55:37 +00:00
fileSystems.${backupPath} = {
label = "backup";
fsType = "ext4";
options = ["noatime"];
2024-02-04 20:51:11 +00:00
};
2024-02-26 18:27:27 +00:00
2024-02-27 20:38:42 +00:00
age.secrets."restic-${attrName}".file = ../../secrets/restic-${attrName}.age;
2024-02-26 18:27:27 +00:00
services.restic.backups.${attrName} = {
2024-02-27 20:37:58 +00:00
repository = "sftp:u385962@u385962.your-storagebox.de:/restic/${attrName}";
2024-02-26 18:27:27 +00:00
initialize = true;
2024-08-18 00:55:37 +00:00
paths = [backups.safe backups.sync];
2024-02-26 18:27:27 +00:00
passwordFile = config.age.secrets."restic-${attrName}".path;
pruneOpts = ["--keep-daily 7" "--keep-weekly 5" "--keep-monthly 12"];
2024-03-08 21:46:46 +00:00
timerConfig = {
OnCalendar = "*-*-* 03:00:00";
Persistent = true;
2024-02-26 18:27:27 +00:00
};
2024-03-08 21:46:46 +00:00
extraOptions = ["sftp.args='-i /etc/ssh/ssh_host_ed25519_key -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'"];
2024-02-26 18:27:27 +00:00
};
2024-02-04 20:51:11 +00:00
}