nixos/home/services/backup-home.nix

119 lines
3.9 KiB
Nix
Raw Normal View History

2024-03-06 19:17:04 +00:00
{ pkgs, config, ... }:
2024-03-05 21:15:31 +00:00
let
# Backup home script
2024-03-27 09:22:24 +00:00
backup-home = pkgs.writeShellApplication {
name = "backup-home";
runtimeInputs = with pkgs; [
coreutils
curl
jq
libnotify
restic
];
text = ''
2024-03-07 14:17:32 +00:00
echo "========== BACKUP HOME STARTING =========="
2024-03-05 21:15:31 +00:00
export RESTIC_PASSWORD="gunter.home.2rjus.net"
export RESTIC_REPOSITORY="rest:http://10.69.12.52:8000/gunter.home.2rjus.net"
2024-05-02 10:43:30 +00:00
SECRET_PATH="$XDG_CONFIG_HOME/sops-nix/secrets/gotify_backup_home"
2024-03-09 19:31:31 +00:00
2024-05-02 10:43:30 +00:00
if ! [ -f "$SECRET_PATH" ]; then
notify-send -u critical "Backup issue" "Secret file for gotify token does not exist"
else
GOTIFY_TOKEN=$(<"$SECRET_PATH")
if [ -z "$GOTIFY_TOKEN" ]; then
notify-send -u critical "Backup issue" "No Gotify token found"
fi
2024-03-06 19:17:04 +00:00
fi
2024-03-06 00:15:04 +00:00
# Send start notification
2024-03-27 09:22:24 +00:00
notify-send -e -t 3000 "Backup started" "Backup of /home/torjus started"
2024-03-05 21:15:31 +00:00
retval=$?
if [ $retval -ne 0 ]; then
echo "Failed to send notification"
fi
2024-03-06 00:15:04 +00:00
# Do the backup
2024-03-07 14:17:32 +00:00
echo "========== BACKUP TASK STARTING =========="
2024-03-05 21:15:31 +00:00
SECONDS=0
2024-03-27 09:22:24 +00:00
restic backup /home/torjus \
2024-03-05 21:15:31 +00:00
--exclude '/home/torjus/.cache' \
--exclude '/home/torjus/.local/share/Steam' \
2024-03-06 14:16:40 +00:00
--exclude '/home/torjus/.local/share/containers' \
2024-03-27 18:05:45 +00:00
--exclude '/home/torjus/.var' \
--exclude '/home/torjus/.local/share/lutris' \
--exclude '/home/torjus/.npm' \
--exclude '/home/torjus/.factorio/mods' \
--exclude '/home/torjus/.zoom' \
2024-03-05 21:15:31 +00:00
--exclude '/home/torjus/git/nixpkgs'
retval=$?
if [ $retval -ne 0 ]; then
2024-03-27 09:22:24 +00:00
notify-send -u critical "Backup failed" "Backup of /home/torjus failed"
2024-03-06 14:16:40 +00:00
retval=$?
2024-03-06 18:59:17 +00:00
if [ $retval -ne 0 ]; then
2024-03-27 09:22:24 +00:00
curl "https://gotify.t-juice.club/message?token=$GOTIFY_TOKEN" \
2024-03-06 18:59:17 +00:00
-F "title=Backup of home@gunter failed!" \
-F "message=Please check status of backup-home service"
fi
2024-03-05 21:15:31 +00:00
fi
2024-03-27 09:22:24 +00:00
BACKUP_DURATION="$SECONDS"
2024-03-07 14:17:32 +00:00
echo "========== BACKUP TASK COMPLETE =========="
2024-03-05 21:15:31 +00:00
2024-03-06 00:15:04 +00:00
# Remove old snapshots and prune
2024-03-07 14:17:32 +00:00
echo "========== PRUNE TASK STARTING =========="
2024-03-27 09:22:24 +00:00
restic forget -d 7 -w 4 -m 6 --keep-within 1d --prune
2024-03-07 14:17:32 +00:00
echo "========== PRUNE TASK COMPLETE =========="
2024-03-06 00:15:04 +00:00
# Gather statistics
2024-03-07 14:17:32 +00:00
echo "========== STATS TASK STARTING =========="
2024-03-27 09:22:24 +00:00
stats=$(restic stats --json)
stats_raw=$(restic stats --mode=raw-data --json)
2024-03-06 00:15:04 +00:00
2024-03-27 09:22:24 +00:00
raw_size=$(jq -r '.total_size' <<< "$stats_raw" \
| numfmt --to=iec --suffix=B --format="%.2f")
total_size=$(jq -r '.total_size' <<< "$stats" \
| numfmt --to=iec --suffix=B --format="%.2f")
total_files=$(jq -r '.total_file_count' <<< "$stats" \
| numfmt --to=iec)
total_snapshots=$(jq -r '.snapshots_count' <<< "$stats")
2024-03-06 00:15:04 +00:00
message="$total_files files\n$total_snapshots snapshots\n$raw_size ($total_size)"
2024-03-07 14:17:32 +00:00
echo "========== STATS TASK COMPLETE =========="
2024-03-06 00:15:04 +00:00
# Send completion notification
2024-03-27 09:22:24 +00:00
notify-send -i checkmark -e -t 10000 \
"Backup of /home/torjus completed in ''${BACKUP_DURATION}s (''${SECONDS}s total)" "$message"
2024-03-05 21:15:31 +00:00
retval=$?
if [ $retval -ne 0 ]; then
echo "Failed to send notification"
2024-03-06 14:16:40 +00:00
exit $retval
2024-03-05 21:15:31 +00:00
fi
2024-03-07 14:17:32 +00:00
echo "========== BACKUP HOME COMPLETE =========="
2024-03-05 21:15:31 +00:00
'';
2024-03-27 09:22:24 +00:00
};
2024-03-05 21:15:31 +00:00
in
{
2024-03-06 19:17:04 +00:00
sops.secrets."gotify_backup_home" = { };
2024-03-05 08:27:58 +00:00
systemd.user.services.backup-home = {
Unit = {
Description = "Backup home directory";
2024-03-06 19:17:04 +00:00
After = [ "network.target" "sops-nix.service" ];
2024-03-05 08:27:58 +00:00
};
Service = {
Type = "oneshot";
2024-03-27 09:22:24 +00:00
ExecStart = "${backup-home}/bin/backup-home";
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 = {
2024-03-09 19:31:31 +00:00
WantedBy = [ "timers.target" "graphical-session.target" ];
2024-03-05 08:27:58 +00:00
};
};
}