backup script

Changed files
+47 -2
hosts
dell-xps
scripts
+46 -1
hosts/dell-xps/default.nix
···
-
{ pkgs, lib, ... }:
+
{ pkgs, lib, config, ... }:
{
imports = [
···
# sometimes I want to keep the cache for operating without internet
nix.gc.automatic = lib.mkForce false;
+
+
+
+
systemd.services.backup = {
+
description = "Backup service";
+
script = let backup = pkgs.writeShellScript "backup.sh" ''
+
LAST_RUN_FILE="/var/run/last_backup"
+
DISK="/dev/disk/by-label/external-hdd"
+
+
if [ -f "$LAST_RUN_FILE" ] && [ "$(( $(date +%s) - $(date +%s -r "$LAST_RUN_FILE") ))" -lt 86400 ]; then
+
echo "<24hrs"
+
exit 0
+
fi
+
+
# if no external-hdd
+
if [ ! -e $DISK ]; then
+
echo "No $DISK"
+
exit 0
+
fi
+
+
${pkgs.util-linux}/bin/mount /dev/disk/by-label/external-hdd /media/external-hdd/ || exit 1
+
if
+
${pkgs.rsync}/bin/rsync -va --exclude={".cache",".local/share/Steam/"} /home/${config.custom.username}/ /media/external-hdd/home/
+
then
+
touch "$LAST_RUN_FILE"
+
${pkgs.util-linux}/bin/umount /media/external-hdd/
+
else
+
${pkgs.util-linux}/bin/umount /media/external-hdd/
+
exit 1
+
fi
+
''; in "${backup}";
+
serviceConfig = {
+
Type = "oneshot";
+
};
+
# trigger on wake
+
wantedBy = [ "suspend.target" "hibernate.target" "hybrid-sleep.target" "suspend-then-hibernate.target" ];
+
};
+
# trigger backup on hard drive connection
+
services.udev.extraRules =
+
# NB could check device label with a trigger script that checks $0 and `RUN+="${trigger} /media/$env{ID_FS_LABEL}"`
+
# but we just assume the Seagate Expansion Desk is the same as /dev/disk/by-label/external-hdd
+
# UDEV has crap support for detecting devices
+
''
+
ACTION=="add", SUBSYSTEM=="block", KERNEL=="sd[a-z]*[0-9]*", ATTRS{model}=="Expansion Desk ", ATTRS{vendor}=="Seagate ", TAG+="systemd", ENV{SYSTEMD_WANTS}+="backup"
+
'';
}
+1 -1
scripts/backup.sh
···
#!/usr/bin/env sh
-
rsync -va ~/ /run/media/ryan/external-hdd/
+
rsync -va --exclude={".cache", ".local/share/Steam/"} ~/ /run/media/ryan/external-hdd/home/