btw i use nix
1{ pkgs, config, ... }:
2
3{
4 age.secrets.restic-gecko.file = ../../secrets/restic-gecko.age;
5 services.restic.backups.${config.networking.hostName} = {
6 repository = "rest:http://100.64.0.9:8000/${config.networking.hostName}/";
7 passwordFile = config.age.secrets.restic-gecko.path;
8 initialize = true;
9 paths = [
10 "/home/ryan"
11 "/etc/NetworkManager/system-connections"
12 ];
13 exclude = [
14 "/home/ryan/videos"
15 "/home/ryan/.thunderbird"
16 "/home/ryan/.cache"
17 "/home/ryan/.local/share/Steam"
18 ];
19 timerConfig = {
20 OnCalendar = "03:00";
21 Persistent = true;
22 };
23 extraBackupArgs = [ "-vv" ];
24 };
25
26 systemd.services."restic-backups-${config.networking.hostName}" = {
27 # fail to start on metered connection
28 preStart = ''
29 DEVICE=$(${pkgs.iproute2}/bin/ip route list 0/0 | sed -r 's/.*dev (\S*).*/\1/g')
30 METERED=$(${pkgs.networkmanager}/bin/nmcli -f GENERAL.METERED dev show "$DEVICE" | ${pkgs.gawk}/bin/awk '/GENERAL.METERED/ {print $2}')
31 if [ "$METERED_STATUS" = "yes" ]; then
32 echo "Connection is metered. Aborting start."
33 exit 1
34 fi
35 systemctl start notify-backup-started
36 '';
37 unitConfig.OnFailure = "notify-backup-failed.service";
38 };
39
40 systemd.services."notify-backup-started" = {
41 enable = true;
42 description = "Notify on backup start";
43 serviceConfig = {
44 Type = "oneshot";
45 User = config.users.users.${config.custom.username}.name;
46 };
47 script = ''
48 export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u ${config.custom.username})/bus"
49 ${pkgs.libnotify}/bin/notify-send "Starting backup..."
50 '';
51 };
52
53 systemd.services."notify-backup-failed" = {
54 enable = true;
55 description = "Notify on failed backup";
56 serviceConfig = {
57 Type = "oneshot";
58 User = config.users.users.${config.custom.username}.name;
59 };
60 script = ''
61 export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/$(id -u ${config.custom.username})/bus"
62 ${pkgs.libnotify}/bin/notify-send --urgency=critical \
63 "Backup failed" \
64 "$(journalctl -u restic-backups-daily -n 5 -o cat)"
65 '';
66 };
67}