Skip to content

Instantly share code, notes, and snippets.

@domenkozar
Created January 3, 2019 11:42
Show Gist options
  • Save domenkozar/a19825b9e5b806fae83df54c72acedd6 to your computer and use it in GitHub Desktop.
Save domenkozar/a19825b9e5b806fae83df54c72acedd6 to your computer and use it in GitHub Desktop.
backup.nix
{ config, pkgs, ... }:
let
uuid = "91434e29-edf5-42b7-b339-43d3a1d58359";
backup = pkgs.writeScript "usb-backup.sh" ''
#!/bin/sh
export DISPLAY=:0.0
SECONDS=0
/run/wrappers/bin/su -l -c 'notify-send "Backup starting ..." -u critical' ielectric
mount -v /dev/disk/by-uuid/${uuid} /mnt/usb
# https://borgbackup.readthedocs.io/en/stable/quickstart.html#automating-backups
# SETUP: borg init /mnt/usb/guava
export BORG_PASSPHRASE=<insert>
borg create --compression lz4 -v --stats /mnt/usb/guava::'{hostname}-{now}' /etc/ /nix/var/ /home/ielectric/ --exclude '*.stack-work*'
borg prune -v --list /mnt/usb/guava --prefix '{hostname}-' --keep-daily=7 --keep-weekly=4 --keep-monthly=6
umount -v /dev/disk/by-uuid/${uuid}
/run/wrappers/bin/su -l -c "notify-send \"Backup DONE in $SECONDS seconds\" -u critical" ielectric
echo "done."
'';
in {
systemd.services.backup-usb = {
description = "Backup to USB";
path = with pkgs; [borgbackup utillinux];
serviceConfig = {
Type = "oneshot";
Nice = 10;
ExecStart = backup;
};
};
environment.systemPackages = [ pkgs.libnotify ];
services.udev.extraRules = ''
ACTION=="add", ENV{ID_FS_UUID}=="${uuid}", RUN+="${pkgs.systemd}/bin/systemctl start backup-usb"
'';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment