diff --git a/hosts/common/configs/system/backup/backup.completion.zsh b/hosts/common/configs/system/backup/backup.completion.zsh deleted file mode 100644 index abfed1b..0000000 --- a/hosts/common/configs/system/backup/backup.completion.zsh +++ /dev/null @@ -1,16 +0,0 @@ -_backup_completion() { - local options=( - '-m[Partition to mount for backup]:partition:($(_partitions))' - '-b[Backup directory]:backup directory:_files -/' - ) - local curcontext="$curcontext" state line - typeset -A opt_args - - _partitions() { - lsblk -rno NAME | sed 's/^/\/dev\//' - } - - _arguments -s $options -} - -compdef _backup_completion backup diff --git a/hosts/common/configs/system/backup/backup.sh b/hosts/common/configs/system/backup/backup.sh deleted file mode 100644 index a5a9011..0000000 --- a/hosts/common/configs/system/backup/backup.sh +++ /dev/null @@ -1,67 +0,0 @@ -# shellcheck shell=bash - -if [[ "$EUID" -ne 0 ]]; then - echo "Please run the script as root." - exit 1 -fi - -usage() { - echo "Usage: $0 [-m partition] [-b backup_location]" - exit 1 -} - -cleanup() { - if [ -d "/persist/user.bak" ]; then btrfs -q subvolume delete "/persist/user.bak"; fi - if [ -n "$backup_location" ]; then rm -f "$backup_location.tmp"; fi - - if [ -n "$mount_location" ]; then - if mount | grep -q "$mount_location"; then umount "$mount_location"; fi - if [ -d "$mount_location" ]; then rmdir "$mount_location"; fi - fi -} - -partition="" -backup_location="" -mount_location="" - -trap cleanup EXIT - -while getopts "m:b:" opt; do - case "$opt" in - m) partition="$OPTARG" ;; - b) backup_location="$OPTARG" ;; - *) usage ;; - esac -done - -if [ -n "$partition" ]; then - mkdir -p "/mnt" - mount_location=$(mktemp -d /mnt/backup.XXXXXX) - echo "Mounting $partition at $mount_location..." - mount "$partition" "$mount_location" -fi - -if [ -z "$mount_location" ]; then - if [[ "$backup_location" != /* ]]; then - backup_location="$(realpath "$backup_location")" - fi -else - if [[ "$backup_location" = /* ]]; then - echo "Error: When a partition is mounted, backup_location must be relative." - exit 1 - fi - - backup_location="$(realpath "$mount_location/$backup_location")" -fi - -backup_location="$backup_location/$(hostname)-$(date +%Y-%m-%d-%H-%M-%S).btrfs.gz" - -echo "Creating /persist/user snapshot..." -btrfs -q subvolume snapshot -r "/persist/user" "/persist/user.bak" - -echo "Creating backup at $backup_location..." -btrfs -q send "/persist/user.bak" > "$backup_location.tmp" - -mv "$backup_location.tmp" "$backup_location" - -echo "Backup completed successfully!" diff --git a/hosts/common/configs/system/backup/default.nix b/hosts/common/configs/system/backup/default.nix deleted file mode 100644 index c2d8367..0000000 --- a/hosts/common/configs/system/backup/default.nix +++ /dev/null @@ -1,18 +0,0 @@ -{ pkgs, ... }: -{ - environment.systemPackages = [ - (pkgs.writeShellApplication { - name = "backup"; - runtimeInputs = with pkgs; [ - btrfs-progs - coreutils - util-linux - ]; - text = builtins.readFile ./backup.sh; - }) - ]; - - home-manager.sharedModules = [ - { programs.zsh.initContent = builtins.readFile ./backup.completion.zsh; } - ]; -} diff --git a/hosts/common/configs/system/btrbk/default.nix b/hosts/common/configs/system/btrbk/default.nix new file mode 100644 index 0000000..9814043 --- /dev/null +++ b/hosts/common/configs/system/btrbk/default.nix @@ -0,0 +1,33 @@ +{ ... }: +{ + systemd.tmpfiles.rules = [ + "d /persist/user.bak 0755 root root" + "d /persist/state.bak 0755 root root" + ]; + + services.btrbk = { + ioSchedulingClass = "idle"; + niceness = 19; + instances = { + persist-user = { + onCalendar = "hourly"; + settings.volume."/persist" = { + subvolume = "user"; + snapshot_dir = "user.bak"; + snapshot_preserve_min = "latest"; + snapshot_preserve = "48h 14d 4w 6m"; + }; + }; + + persist-state = { + onCalendar = "daily"; + settings.volume."/persist" = { + subvolume = "state"; + snapshot_dir = "state.bak"; + snapshot_preserve_min = "latest"; + snapshot_preserve = "7d 4w 3m"; + }; + }; + }; + }; +} diff --git a/hosts/common/configs/system/impermanence/options.nix b/hosts/common/configs/system/impermanence/options.nix index a5399a5..918a594 100644 --- a/hosts/common/configs/system/impermanence/options.nix +++ b/hosts/common/configs/system/impermanence/options.nix @@ -129,7 +129,10 @@ in "directory" ]; default = "none"; - description = "Whether to create the file or directory in persistence if it does not exist."; + description = '' + Whether to create the file or directory + in persistence if it does not exist. + ''; }; }; } diff --git a/hosts/common/configs/system/impermanence/scripts/wipe.sh b/hosts/common/configs/system/impermanence/scripts/wipe.sh index 62c03e4..bf12f46 100644 --- a/hosts/common/configs/system/impermanence/scripts/wipe.sh +++ b/hosts/common/configs/system/impermanence/scripts/wipe.sh @@ -13,11 +13,22 @@ mount "$DEVICE" /mnt/btrfs if [[ -e /mnt/btrfs/@ ]]; then mkdir -p /mnt/btrfs/@.bak - timestamp=$(date --date="@$(stat -c %Y /mnt/btrfs/@)" "+%Y-%m-%d_%H:%M:%S") - mv /mnt/btrfs/@ "/mnt/btrfs/@.bak/$timestamp" + timestamp=$(date --date="@$(stat -c %Y /mnt/btrfs/@)" "+%Y%m%dT%H%M") + base="@.$timestamp" + + target="/mnt/btrfs/@.bak/$base" + if [[ -e "$target" ]]; then + i=1 + while [[ -e "/mnt/btrfs/@.bak/${base}_$i" ]]; do + (( i++ )) + done + target="/mnt/btrfs/@.bak/${base}_$i" + fi + + mv /mnt/btrfs/@ "$target" fi -find /mnt/btrfs/@.bak/ -maxdepth 1 -mtime +14 | while IFS= read -r i; do +find /mnt/btrfs/@.bak/ -maxdepth 1 -mtime +7 | while IFS= read -r i; do delete_subvolume_recursively "$i" done diff --git a/hosts/elara/default.nix b/hosts/elara/default.nix index 2dd572d..79fab52 100644 --- a/hosts/elara/default.nix +++ b/hosts/elara/default.nix @@ -8,10 +8,10 @@ ./hardware - ../common/configs/system/backup ../common/configs/system/bluetooth ../common/configs/system/boot ../common/configs/system/brightnessctl + ../common/configs/system/btrbk ../common/configs/system/btrfs ../common/configs/system/cloudflared ../common/configs/system/cpu diff --git a/hosts/elara/users/nikara/default.nix b/hosts/elara/users/nikara/default.nix index d593bac..25de7bc 100644 --- a/hosts/elara/users/nikara/default.nix +++ b/hosts/elara/users/nikara/default.nix @@ -89,7 +89,7 @@ in (import ./configs/gui/vscode { inherit user home; }) ]; - # echo "password" | mkpasswd -s + # mkpasswd -s sops.secrets."${user}-password" = { sopsFile = ../../../../secrets/sas/secrets.yaml; key = "password"; diff --git a/hosts/himalia/default.nix b/hosts/himalia/default.nix index 5fe0620..174fc5b 100644 --- a/hosts/himalia/default.nix +++ b/hosts/himalia/default.nix @@ -6,10 +6,10 @@ ./hardware - ../common/configs/system/backup ../common/configs/system/bluetooth ../common/configs/system/boot ../common/configs/system/brightnessctl + ../common/configs/system/btrbk ../common/configs/system/btrfs ../common/configs/system/cpu ../common/configs/system/documentation diff --git a/hosts/himalia/users/nick/default.nix b/hosts/himalia/users/nick/default.nix index fdd1c4b..e24b45c 100644 --- a/hosts/himalia/users/nick/default.nix +++ b/hosts/himalia/users/nick/default.nix @@ -89,7 +89,7 @@ in (import ./configs/gui/vscode { inherit user home; }) ]; - # echo "password" | mkpasswd -s + # mkpasswd -s sops.secrets."${user}-password" = { sopsFile = ../../../../secrets/personal/secrets.yaml; key = "password"; diff --git a/hosts/installer/users/nick/default.nix b/hosts/installer/users/nick/default.nix index 19ecfd1..f3885e7 100644 --- a/hosts/installer/users/nick/default.nix +++ b/hosts/installer/users/nick/default.nix @@ -37,7 +37,7 @@ in (import ./configs/console/ssh { inherit user home; }) ]; - # echo "password" | mkpasswd -s + # mkpasswd -s sops.secrets."${user}-password" = { sopsFile = ../../../../secrets/personal/secrets.yaml; key = "password"; diff --git a/hosts/jupiter/default.nix b/hosts/jupiter/default.nix index 441f637..5be03b0 100644 --- a/hosts/jupiter/default.nix +++ b/hosts/jupiter/default.nix @@ -8,6 +8,7 @@ ../common/configs/system/boot ../common/configs/system/brightnessctl + ../common/configs/system/btrbk ../common/configs/system/btrfs ../common/configs/system/cpu ../common/configs/system/documentation diff --git a/hosts/jupiter/users/nick/default.nix b/hosts/jupiter/users/nick/default.nix index 0dd4d7e..745748a 100644 --- a/hosts/jupiter/users/nick/default.nix +++ b/hosts/jupiter/users/nick/default.nix @@ -35,7 +35,7 @@ in (import ./configs/console/podman { inherit user home; }) ]; - # echo "password" | mkpasswd -s + # mkpasswd -s sops.secrets."${user}-password" = { sopsFile = ../../../../secrets/personal/secrets.yaml; key = "password"; diff --git a/hosts/jupiter/users/storm/default.nix b/hosts/jupiter/users/storm/default.nix index 2914482..75a23a5 100644 --- a/hosts/jupiter/users/storm/default.nix +++ b/hosts/jupiter/users/storm/default.nix @@ -19,7 +19,7 @@ in (import ./configs/console/podman { inherit user home; }) ]; - # echo "password" | mkpasswd -s + # mkpasswd -s sops.secrets."${user}-password" = { sopsFile = ../../../../secrets/personal/secrets.yaml; key = "password";