Improve backup-home
Some checks failed
pre-commit / pre-commit (push) Failing after 17s

This commit is contained in:
Torjus Håkestad 2024-03-27 10:22:24 +01:00
parent d5d12e79d8
commit 83719ec59c

View File

@ -1,22 +1,27 @@
{ pkgs, config, ... }: { pkgs, config, ... }:
let let
# Backup home script # Backup home script
backup-home = pkgs.writeShellScriptBin "backup-home.sh" backup-home = pkgs.writeShellApplication {
'' name = "backup-home";
runtimeInputs = with pkgs; [
coreutils
curl
jq
libnotify
restic
];
text = ''
echo "========== BACKUP HOME STARTING ==========" echo "========== BACKUP HOME STARTING =========="
export RESTIC_PASSWORD="gunter.home.2rjus.net" export RESTIC_PASSWORD="gunter.home.2rjus.net"
export RESTIC_REPOSITORY="rest:http://10.69.12.52:8000/gunter.home.2rjus.net" export RESTIC_REPOSITORY="rest:http://10.69.12.52:8000/gunter.home.2rjus.net"
GOTIFY_TOKEN=$(<"$XDG_RUNTIME_DIR/secrets/gotify_backup_home") GOTIFY_TOKEN=$(<"$XDG_RUNTIME_DIR/secrets/gotify_backup_home")
echo "DEBUG: GOTIFY_TOKEN=$GOTIFY_TOKEN"
echo "DEBUG: BUSADDR=$DBUS_SESSION_BUS_ADDRESS"
if [ -z "$GOTIFY_TOKEN" ]; then if [ -z "$GOTIFY_TOKEN" ]; then
${pkgs.libnotify}/bin/notify-send -u critical "Backup issue" "No Gotify token found" notify-send -u critical "Backup issue" "No Gotify token found"
fi fi
# Send start notification # Send start notification
${pkgs.libnotify}/bin/notify-send -e -t 3000 "Backup started" "Backup of /home/torjus started" notify-send -e -t 3000 "Backup started" "Backup of /home/torjus started"
retval=$? retval=$?
if [ $retval -ne 0 ]; then if [ $retval -ne 0 ]; then
echo "Failed to send notification" echo "Failed to send notification"
@ -25,46 +30,46 @@ let
# Do the backup # Do the backup
echo "========== BACKUP TASK STARTING ==========" echo "========== BACKUP TASK STARTING =========="
SECONDS=0 SECONDS=0
${pkgs.restic}/bin/restic backup /home/torjus \ restic backup /home/torjus \
--exclude '/home/torjus/.cache' \ --exclude '/home/torjus/.cache' \
--exclude '/home/torjus/.local/share/Steam' \ --exclude '/home/torjus/.local/share/Steam' \
--exclude '/home/torjus/.local/share/containers' \ --exclude '/home/torjus/.local/share/containers' \
--exclude '/home/torjus/git/nixpkgs' --exclude '/home/torjus/git/nixpkgs'
retval=$? retval=$?
if [ $retval -ne 0 ]; then if [ $retval -ne 0 ]; then
${pkgs.libnotify}/bin/notify-send -u critical "Backup failed" "Backup of /home/torjus failed" notify-send -u critical "Backup failed" "Backup of /home/torjus failed"
retval=$? retval=$?
if [ $retval -ne 0 ]; then if [ $retval -ne 0 ]; then
# TODO: put token in sops curl "https://gotify.t-juice.club/message?token=$GOTIFY_TOKEN" \
${pkgs.curl}/bin/curl "https://gotify.t-juice.club/message?token=$GOTIFY_TOKEN" \
-F "title=Backup of home@gunter failed!" \ -F "title=Backup of home@gunter failed!" \
-F "message=Please check status of backup-home service" -F "message=Please check status of backup-home service"
fi fi
fi fi
BACKUP_DURATION="$SECONDS"
echo "========== BACKUP TASK COMPLETE ==========" echo "========== BACKUP TASK COMPLETE =========="
# Remove old snapshots and prune # Remove old snapshots and prune
echo "========== PRUNE TASK STARTING ==========" echo "========== PRUNE TASK STARTING =========="
${pkgs.restic}/bin/restic forget -d 7 -w 4 -m 6 --keep-within 1d --prune restic forget -d 7 -w 4 -m 6 --keep-within 1d --prune
echo "========== PRUNE TASK COMPLETE ==========" echo "========== PRUNE TASK COMPLETE =========="
# Gather statistics # Gather statistics
echo "========== STATS TASK STARTING ==========" echo "========== STATS TASK STARTING =========="
stats=$(${pkgs.restic}/bin/restic stats --json) stats=$(restic stats --json)
stats_raw=$(${pkgs.restic}/bin/restic stats --mode=raw-data --json) stats_raw=$(restic stats --mode=raw-data --json)
raw_size=$(${pkgs.jq}/bin/jq -r '.total_size' <<< $stats_raw \ raw_size=$(jq -r '.total_size' <<< "$stats_raw" \
| ${pkgs.coreutils}/bin/numfmt --to=iec --suffix=B --format="%.2f") | numfmt --to=iec --suffix=B --format="%.2f")
total_size=$(${pkgs.jq}/bin/jq -r '.total_size' <<< $stats \ total_size=$(jq -r '.total_size' <<< "$stats" \
| ${pkgs.coreutils}/bin/numfmt --to=iec --suffix=B --format="%.2f") | numfmt --to=iec --suffix=B --format="%.2f")
total_files=$(${pkgs.jq}/bin/jq -r '.total_file_count' <<< $stats \ total_files=$(jq -r '.total_file_count' <<< "$stats" \
| ${pkgs.coreutils}/bin/numfmt --to=iec) | numfmt --to=iec)
total_snapshots=$(${pkgs.jq}/bin/jq -r '.snapshots_count' <<< $stats) total_snapshots=$(jq -r '.snapshots_count' <<< "$stats")
message="$total_files files\n$total_snapshots snapshots\n$raw_size ($total_size)" message="$total_files files\n$total_snapshots snapshots\n$raw_size ($total_size)"
echo "========== STATS TASK COMPLETE ==========" echo "========== STATS TASK COMPLETE =========="
# Send completion notification # Send completion notification
${pkgs.libnotify}/bin/notify-send -i checkmark -e -t 10000 \ notify-send -i checkmark -e -t 10000 \
"Backup of /home/torjus completed in ''${SECONDS}s" "$message" "Backup of /home/torjus completed in ''${BACKUP_DURATION}s (''${SECONDS}s total)" "$message"
retval=$? retval=$?
if [ $retval -ne 0 ]; then if [ $retval -ne 0 ]; then
echo "Failed to send notification" echo "Failed to send notification"
@ -72,8 +77,10 @@ let
fi fi
echo "========== BACKUP HOME COMPLETE ==========" echo "========== BACKUP HOME COMPLETE =========="
''; '';
};
in in
{ {
sops.secrets."gotify_backup_home" = { }; sops.secrets."gotify_backup_home" = { };
systemd.user.services.backup-home = { systemd.user.services.backup-home = {
@ -83,7 +90,7 @@ in
}; };
Service = { Service = {
Type = "oneshot"; Type = "oneshot";
ExecStart = "${backup-home}/bin/backup-home.sh"; ExecStart = "${backup-home}/bin/backup-home";
}; };
}; };
systemd.user.timers.backup-home = { systemd.user.timers.backup-home = {