I’ve set up a configuration for automated Restic backups, mostly using the Restic page on the NixOS wiki and the options in services.restic.backups. I want to use Btrfs snapshots to ensure atomicity of my backups since Restic doesn’t have native support for Btrfs snapshots. My script has two problems:
- It relies on two scripts that are external to my Nix configuration, which isn’t ideal. Also, the service seems to attempt to run Restic even if the Btrfs snapshot scripts fail.
- I’m unsure how best to pass the host information and SSH keys to the Restic systemd service. Ideally I could keep that information in
~/.sshas I normally would and point the systemd service there. Unfortunately, Restic upstream doesn’t (seem to) support manual specification of the location of a key file.
Here is the relevant portion of my system configuration:
environment.systemPackages = [ pkgs.restic ];
services.restic.backups.mybackup = {
initialize = true;
repository = "sftp:user@serveraddress:/repo/path";
timerConfig = {
OnCalendar = "6 hours";
Persistent = true;
};
backupPrepareCommand = "/etc/nixos/restic_snapshot_init.sh";
paths = [
"/var/snapshots/Data"
];
backupCleanupCommand = "/etc/nixos/restic_snapshot_teardown.sh";
passwordFile = "/etc/nixos/ignore/restic-password";
};
/etc/nixos/restic_snapshot_init.sh:
#!/bin/sh
set -e
/run/current-system/sw/bin/btrfs subvolume snapshot -r /home/user/Data/ /var/snapshots/Data
/run/current-system/sw/bin/btrfs subvolume snapshot -r /var/snapshots/Data /var/snapshots/"Data $(date)"
/etc/nixos/restic_snapshot_teardown.sh:
#!/bin/sh
set -e
/run/current-system/sw/bin/btrfs subvolume delete /var/snapshots/Data