wip moving to borgbackup

This commit is contained in:
Motiejus Jakštys 2023-01-26 12:05:59 -05:00
parent 3cb53fb324
commit 7ca87d396e

View File

@ -5,14 +5,47 @@
{ config, pkgs, lib, ... }: { config, pkgs, lib, ... }:
let let
snapshot_dirs = {
"/var/log" = "rpool/nixos/var/log";
"/var/lib" = "rpool/nixos/var/lib";
};
ssh_pubkeys = { ssh_pubkeys = {
motiejus = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC+qpaaD+FCYPcUU1ONbw/ff5j0xXu5DNvp/4qZH/vOYwG13uDdfI5ISYPs8zNaVcFuEDgNxWorVPwDw4p6+1JwRLlhO4J/5tE1w8Gt6C7y76LRWnp0rCdva5vL3xMozxYIWVOAiN131eyirV2FdOaqTwPy4ouNMmBFbibLQwBna89tbFMG/jwR7Cxt1I6UiYOuCXIocI5YUbXlsXoK9gr5yBRoTjl2OfH2itGYHz9xQCswvatmqrnteubAbkb6IUFYz184rnlVntuZLwzM99ezcG4v8/485gWkotTkOgQIrGNKgOA7UNKpQNbrwdPAMugqfSTo6g8fEvy0Q+6OXdxw5X7en2TJE+BLVaXp4pVMdOAzKF0nnssn64sRhsrUtFIjNGmOWBOR2gGokaJcM6x9R72qxucuG5054pSibs32BkPEg6Qzp+Bh77C3vUmC94YLVg6pazHhLroYSP1xQjfOvXyLxXB1s9rwJcO+s4kqmInft2weyhfaFE0Bjcoc+1/dKuQYfPCPSB//4zvktxTXud80zwWzMy91Q4ucRrHTBz3PrhO8ys74aSGnKOiG3ccD3HbaT0Ff4qmtIwHcAjrnNlINAcH/A2mpi0/2xA7T8WpFnvgtkQbcMF0kEKGnNS5ULZXP/LC8BlLXxwPdqTzvKikkTb661j4PhJhinhVwnQ=="; motiejus = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQC+qpaaD+FCYPcUU1ONbw/ff5j0xXu5DNvp/4qZH/vOYwG13uDdfI5ISYPs8zNaVcFuEDgNxWorVPwDw4p6+1JwRLlhO4J/5tE1w8Gt6C7y76LRWnp0rCdva5vL3xMozxYIWVOAiN131eyirV2FdOaqTwPy4ouNMmBFbibLQwBna89tbFMG/jwR7Cxt1I6UiYOuCXIocI5YUbXlsXoK9gr5yBRoTjl2OfH2itGYHz9xQCswvatmqrnteubAbkb6IUFYz184rnlVntuZLwzM99ezcG4v8/485gWkotTkOgQIrGNKgOA7UNKpQNbrwdPAMugqfSTo6g8fEvy0Q+6OXdxw5X7en2TJE+BLVaXp4pVMdOAzKF0nnssn64sRhsrUtFIjNGmOWBOR2gGokaJcM6x9R72qxucuG5054pSibs32BkPEg6Qzp+Bh77C3vUmC94YLVg6pazHhLroYSP1xQjfOvXyLxXB1s9rwJcO+s4kqmInft2weyhfaFE0Bjcoc+1/dKuQYfPCPSB//4zvktxTXud80zwWzMy91Q4ucRrHTBz3PrhO8ys74aSGnKOiG3ccD3HbaT0Ff4qmtIwHcAjrnNlINAcH/A2mpi0/2xA7T8WpFnvgtkQbcMF0kEKGnNS5ULZXP/LC8BlLXxwPdqTzvKikkTb661j4PhJhinhVwnQ==";
vno1_root = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMiWb7yeSeuFCMZWarKJD6ZSxIlpEHbU++MfpOIy/2kh"; vno1_root = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMiWb7yeSeuFCMZWarKJD6ZSxIlpEHbU++MfpOIy/2kh";
}; in { };
mountLatest = ({mountpoint, zfs_name}:
''
set -euo pipefail
${pkgs.util-linux}/bin/umount ${mountpoint}/.snapshot-latest || : &>/dev/null
mkdir -p ${mountpoint}/.snapshot-latest
${pkgs.util-linux}/bin/mount -t zfs $(${pkgs.zfs}/bin/zfs list -H -t snapshot -o name ${zfs_name} | sort | tail -1) ${mountpoint}/.snapshot-latest
''
);
umountLatest = ({mountpoint, ...}:
''set -euo pipefail
"${pkgs.util-linux}/bin/umount ${mountpoint}/.snapshot-latest"
''
);
backup_paths = {
var_lib = {
mountpoint = "/var/lib";
zfs_name = "rpool/nixos/var/lib";
paths = [
"/var/lib/.snapshot-latest/gitea"
"/var/lib/.snapshot-latest/headscale"
];
backup_at = "*-*-* 00:05:00";
};
var_log = {
mountpoint = "/var/log";
zfs_name = "rpool/nixos/var/log";
paths = [ "/var/log/.snapshot-latest/caddy/" ];
patterns = [ "access-beta.jakstys.lt.log-*.zst" ];
backup_at = "*-*-* 00:10:00";
};
};
in {
imports = imports =
[ [
/etc/nixos/hardware-configuration.nix /etc/nixos/zfs.nix /etc/nixos/hardware-configuration.nix /etc/nixos/zfs.nix
@ -65,6 +98,7 @@ let
lsof lsof
file file
htop htop
mosh
#ncdu #ncdu
sqlite sqlite
ripgrep ripgrep
@ -115,42 +149,36 @@ let
autosnap = true; autosnap = true;
autoprune = true; autoprune = true;
}; };
datasets."rpool/nixos/home".use_template = [ "prod" ]; datasets = lib.mapAttrs' (name: value: {
datasets."rpool/nixos/var/log".use_template = [ "prod" ]; name = value.zfs_name;
datasets."rpool/nixos/var/lib".use_template = [ "prod" ]; value = { use_template = ["prod"]; };
}) backup_paths;
extraArgs = [ "--verbose" ]; extraArgs = [ "--verbose" ];
}; };
restic.backups = { borgbackup.jobs = lib.mapAttrs' (name: value:
var = { let
paths = [ snapshot = { mountpoint = value.mountpoint; zfs_name = value.zfs_name; };
"/var/lib/.snapshot-latest/gitea" rwpath = value.mountpoint + "/.snapshot-latest";
"/var/lib/.snapshot-latest/headscale" in {
"/var/log/.snapshot-latest/caddy/access-beta.jakstys.lt.log-*.zst" name = name;
]; value = {
repository = "sftp:zh2769@zh2769.rsync.net:hel1-a.servers.jakst"; doInit = true;
initialize = true; repo = "zh2769@zh2769.rsync.net:borg/hel1-a.servers.jakst";
passwordFile = "/var/src/secrets/restic/password"; encryption = {
pruneOpts = [ mode = "repokey-blake2";
"--keep-daily 7" passCommand = "cat /var/src/secrets/borgbackup/password";
];
backupPrepareCommand = let
prepLines = lib.mapAttrsToList (name: value: ''
${pkgs.util-linux}/bin/umount ${name}/.snapshot-latest || : &>/dev/null
mkdir -p ${name}/.snapshot-latest
${pkgs.util-linux}/bin/mount -t zfs $(${pkgs.zfs}/bin/zfs list -H -t snapshot -o name ${value} | sort | tail -1) ${name}/.snapshot-latest
'') snapshot_dirs;
in "set -euo pipefail\n" + lib.concatStringsSep "\n" prepLines;
backupCleanupCommand = let
cleanupLines = lib.mapAttrsToList (name: value: "${pkgs.util-linux}/bin/umount ${name}/.snapshot-latest") snapshot_dirs;
cleanupString = lib.concatStringsSep "\n" cleanupLines;
in "set -euo pipefail\n" + cleanupString;
timerConfig = {
OnCalendar = "00:10";
RandomizedDelaySec = "1h";
};
}; };
paths = value.paths;
compression = "auto,lzma";
startAt = "*-*-* 00:05:00";
readWritePaths = [ rwpath ];
preHook = mountLatest snapshot;
postHook = umountLatest snapshot;
} // lib.optionalAttrs (value ? patterns) {
patterns = value.patterns;
}; };
}) backup_paths;
openssh = { openssh = {
enable = true; enable = true;
@ -306,6 +334,10 @@ let
}; };
}; };
#systemd.services."make-snapshot-dirs" = {
# requiredBy =
#};
# Do not change # Do not change
system.stateVersion = "22.11"; system.stateVersion = "22.11";
} }