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"
|
|
|
|
|
2024-03-06 00:15:04 +00:00
|
|
|
# Send start notification
|
2024-03-05 21:15:31 +00:00
|
|
|
${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
|
|
|
|
|
2024-03-06 00:15:04 +00:00
|
|
|
# Do the backup
|
2024-03-05 21:15:31 +00:00
|
|
|
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
|
|
|
|
|
2024-03-06 00:15:04 +00:00
|
|
|
# Remove old snapshots and prune
|
|
|
|
${pkgs.restic}/bin/restic forget -d 7 -w 4 -m 6 --keep-within 1d --prune
|
|
|
|
|
|
|
|
# Gather statistics
|
|
|
|
stats=$(${pkgs.restic}/bin/restic stats --json)
|
|
|
|
stats_raw=$(${pkgs.restic}/bin/restic stats --mode=raw-data --json)
|
|
|
|
|
|
|
|
raw_size=$(${pkgs.jq}/bin/jq -r '.total_size' <<< $stats_raw \
|
|
|
|
| ${pkgs.coreutils}/bin/numfmt --to=iec --suffix=B --format="%.2f")
|
|
|
|
total_size=$(${pkgs.jq}/bin/jq -r '.total_size' <<< $stats \
|
|
|
|
| ${pkgs.coreutils}/bin/numfmt --to=iec --suffix=B --format="%.2f")
|
|
|
|
total_files=$(${pkgs.jq}/bin/jq -r '.total_file_count' <<< $stats \
|
|
|
|
| ${pkgs.coreutils}/bin/numfmt --to=iec)
|
|
|
|
total_snapshots=$(${pkgs.jq}/bin/jq -r '.snapshots_count' <<< $stats)
|
|
|
|
message="$total_files files\n$total_snapshots snapshots\n$raw_size ($total_size)"
|
|
|
|
|
|
|
|
# Send completion notification
|
|
|
|
${pkgs.libnotify}/bin/notify-send -i checkmark -e -t 10000 \
|
|
|
|
"Backup of /home/torjus completed in ''${SECONDS}s" "$message"
|
2024-03-05 21:15:31 +00:00
|
|
|
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-06 00:15:04 +00:00
|
|
|
OnCalendar = "*-*-* *:00:00";
|
2024-03-05 08:27:58 +00:00
|
|
|
Persistent = true;
|
|
|
|
};
|
|
|
|
Install = {
|
|
|
|
WantedBy = [ "timers.target" ];
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|