nixos/home/services/backup-home.nix

61 lines
1.7 KiB
Nix
Raw Normal View History

2024-03-05 21:15:31 +00:00
{ pkgs, ... }:
let
# Backup home script
backup-home = pkgs.writeShellScriptBin "backup-home.sh"
''
export RESTIC_PASSWORD="gunter.home.2rjus.net"
export RESTIC_REPOSITORY="rest:http://10.69.12.52:8000/gunter.home.2rjus.net"
${pkgs.libnotify}/bin/notify-send -e -t 3000 "Backup started" "Backup of /home/torjus started"
retval=$?
if [ $retval -ne 0 ]; then
echo "Failed to send notification"
exit 1
fi
SECONDS=0
${pkgs.restic}/bin/restic backup /home/torjus \
--exclude '/home/torjus/.cache' \
--exclude '/home/torjus/.local/share/Steam' \
--exclude '/home/torjus/git/nixpkgs'
retval=$?
if [ $retval -ne 0 ]; then
notify-send -u critical "Backup failed" "Backup of /home/torjus failed"
exit 1
fi
${pkgs.restic}/bin/restic forget -d 7 -w 4 -m 6 --prune
${pkgs.libnotify}/bin/notify-send -e -t 3000 "Backup completed" "Backup of /home/torjus completed in $SECONDS seconds."
retval=$?
if [ $retval -ne 0 ]; then
echo "Failed to send notification"
exit 1
fi
'';
in
{
2024-03-05 08:27:58 +00:00
systemd.user.services.backup-home = {
Unit = {
Description = "Backup home directory";
After = [ "network.target" ];
};
Service = {
Type = "oneshot";
2024-03-05 21:15:31 +00:00
ExecStart = "${backup-home}/bin/backup-home.sh";
2024-03-05 08:27:58 +00:00
};
};
systemd.user.timers.backup-home = {
Unit = {
Description = "Backup home directory";
After = [ "network.target" ];
};
Timer = {
2024-03-05 21:15:31 +00:00
OnCalendar = "*-*-* 0,6,12,18:00:00";
2024-03-05 08:27:58 +00:00
Persistent = true;
};
Install = {
WantedBy = [ "timers.target" ];
};
};
}