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