nix/host/x86_64-linux/home/Backup.nix

113 lines
2.4 KiB
Nix
Raw Normal View History

{
2024-11-04 04:37:29 +03:00
__findFile,
pkgs,
util,
...
} @args: let
bash = import <home/program/bash> args;
script = pkgs.writeText "backupScript" (util.trimTabs ''
source ${bash.modulesFile}
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
function report() {
echo "''${*}"
notify "''${*}"
}
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
# Define constants.
path_src="/storage/hot"
path_mount="/storage/cold_1"
path_backup="''${path_mount}/backup"
2024-12-02 21:12:45 +03:00
path_data="''${path_backup}/home"
2024-11-04 04:37:29 +03:00
path_media="/storage/cold_1 /storage/cold_2"
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
# Check if backup drive is mounted.
if [ ! -f "''${path_mount}"/.mount ]; then
2024-12-08 05:46:23 +03:00
report "Backup: ''${path_mount} not mounted!"
2024-11-04 04:37:29 +03:00
exit 1
fi
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
# Check if hot storage is mounted.
if [ ! -f "''${path_src}"/.mount ]; then
2024-12-08 05:46:23 +03:00
report "Backup: ''${path_src} not mounted!"
2024-11-04 04:37:29 +03:00
exit 1
fi
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
# Cd to src storage.
cd "''${path_src}"
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
# Save media list.
2024-12-08 05:46:23 +03:00
find ''${path_media} -type d > ''${path_backup}/cold/ColdMedia.txt || report "Backup: Failed to save media list!"
2024-11-04 04:37:29 +03:00
cd ''${path_backup}/cold/
2024-12-08 05:46:23 +03:00
archive ColdMedia.txt && rm ColdMedia.txt || report "Backup: Failed to archive media list!"
2024-11-04 04:37:29 +03:00
cd -
2024-04-08 18:26:01 +03:00
2024-12-02 21:12:45 +03:00
# Backup data.
data=$(archive data/)
bupsize=$(tdu ''${data} | awk '{print $1}')
2024-12-08 05:46:23 +03:00
mv ''${data} ''${path_data}/ || report "Backup: Failed to save data!"
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
# Backup some media.
cd ''${path_src}
paper=$(archive paper/)
2024-12-08 05:46:23 +03:00
mv ''${paper} ''${path_backup}/paper/ || report "Backup: Failed to save paper!"
2024-11-04 04:37:29 +03:00
cd -
2024-04-08 18:26:01 +03:00
2024-12-08 05:46:23 +03:00
rcp_merge_fast ''${path_src}/sync/save/ ''${path_backup}/save/tmp/ || report "Backup: Failed to save game saves!"
rcp_merge_fast ''${path_src}/sync/photo/ ''${path_backup}/photo/tmp/ || report "Backup: Failed to save photos!"
2024-11-04 04:37:29 +03:00
# Prune media copies.
cd ''${path_backup}/paper/
archive_prune Paper 7
cd -
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
cd ''${path_backup}/cold/
archive_prune ColdMediaTxt 30
cd -
2024-04-08 18:26:01 +03:00
2024-12-02 21:12:45 +03:00
# Prune old data copies.
cd ''${path_data}
archive_prune Data 7
2024-11-04 04:37:29 +03:00
cd -
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
# Sync writes.
sync
2024-04-08 18:26:01 +03:00
2024-11-04 04:37:29 +03:00
# Notify completion & size.
2024-12-08 05:46:23 +03:00
notify_silent "Backup: Complete ''${bupsize}."
echo "Backup: Complete ''${bupsize}."
2024-11-04 04:37:29 +03:00
'');
in {
systemd.services.backup = util.mkStaticSystemdService {
enable = true;
description = "Home system backup.";
serviceConfig = {
Type = "oneshot";
};
path = with pkgs; [
bashInteractive
curl
gawk
gnutar
2024-12-02 21:12:45 +03:00
mount
2024-11-04 04:37:29 +03:00
procps
pv
2024-12-08 05:46:23 +03:00
rsync
2024-11-04 04:37:29 +03:00
xz
];
script = ''
${pkgs.bashInteractive}/bin/bash ${script}
'';
};
2024-11-04 04:37:29 +03:00
systemd.timers.backup = {
timerConfig = {
OnCalendar = "*-*-* 06:00:00";
Persistent = true;
Unit = "backup.service";
};
wantedBy = [
"timers.target"
];
};
}