Files
nix-home-manager/home/backup.nix

69 lines
2.0 KiB
Nix

{ config, pkgs, lib, ... }:
let
hostname = config.networking.hostName or (builtins.getEnv "HOSTNAME");
backupTarget = "/mnt/backup/borg"; # Adjust this path to your remote or local backup repo
backupRepo = "${backupTarget}/${hostname}";
backupPaths = {
"t440p" = [ "Documents" "Projects" ];
"steamdeck" = [ ".config" ];
"xaorus" = [ "Pictures" "Videos" ];
};
folders = builtins.map (dir: "${config.home.homeDirectory}/${dir}") (backupPaths.${hostname} or []);
cfg = config.homeModules.backup;
in
lib.mkIf cfg.enable {
home.packages = with pkgs; [ borgbackup ];
systemd.user.services.borg-backup = {
Unit = {
Description = "Borg Backup for ${hostname}";
Wants = [ "network-online.target" ];
After = [ "network-online.target" ];
};
Service = {
Type = "oneshot";
ExecStart = ''
${pkgs.borgbackup}/bin/borg create \
--verbose \
--filter AME \
--list \
--stats \
--show-rc \
--compression lz20 \
${backupRepo}::"{now:%Y-%m-%d_%H-%M}" \
${builtins.concatStringsSep " \\\n " folders} \
--exclude ${config.home.homeDirectory}/.cache \
--exclude ${config.home.homeDirectory}/Downloads \
--exclude ${config.home.homeDirectory}/node_modules \
--exclude '*/.git' \
--exclude '*/venv' \
--exclude '*/target'
'';
ExecStopPost = ''
${pkgs.borgbackup}/bin/borg prune -v --list ${backupRepo} \
--keep-daily=7 \
--keep-weekly=4 \
--keep-monthly=3
'';
};
Install = {
WantedBy = [ "default.target" ];
};
};
# Optional: allow the backup location to be automounted
xdg.userDirs.extraConfig = {
XDG_BACKUP_DIR = "${backupTarget}";
};
# Optional: set environment variable to simplify CLI usage
home.sessionVariables = {
BORG_REPO = backupRepo;
BORG_PASSPHRASE = ""; # or use environment.d or a secrets mechanism
};
}