only do snapshot we will clean old roots in actual user-space instead of initrd
This commit is contained in:
parent
4bef267c13
commit
0ed9d90349
1 changed files with 8 additions and 76 deletions
|
|
@ -225,7 +225,7 @@ lib.mkMerge [
|
||||||
# TODO rotate root
|
# TODO rotate root
|
||||||
}
|
}
|
||||||
# Reset root for erase your darlings/impermanence/preservation
|
# Reset root for erase your darlings/impermanence/preservation
|
||||||
(lib.mkIf false {
|
(lib.mkIf true {
|
||||||
boot.initrd.systemd.services.bcachefs-reset-root = {
|
boot.initrd.systemd.services.bcachefs-reset-root = {
|
||||||
description = "Reset bcachefs root subvolume before pivot";
|
description = "Reset bcachefs root subvolume before pivot";
|
||||||
|
|
||||||
|
|
@ -256,35 +256,25 @@ lib.mkMerge [
|
||||||
serviceConfig = {
|
serviceConfig = {
|
||||||
Type = "oneshot";
|
Type = "oneshot";
|
||||||
KeyringMode = "shared";
|
KeyringMode = "shared";
|
||||||
Environment = "PATH=${
|
# Environment = "PATH=${
|
||||||
lib.makeBinPath [
|
# lib.makeBinPath [
|
||||||
pkgs.coreutils
|
# # pkgs.coreutils
|
||||||
pkgs.util-linux
|
# ]
|
||||||
pkgs.findutils
|
# }:/bin:/sbin";
|
||||||
pkgs.gawk
|
|
||||||
pkgs.bcachefs-tools
|
|
||||||
]
|
|
||||||
}:/bin:/sbin";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
script = ''
|
script = ''
|
||||||
# 1. Enable Debugging
|
# 1. Enable Debugging
|
||||||
set -x
|
set -x
|
||||||
|
|
||||||
echo $PATH
|
|
||||||
|
|
||||||
# 2. Define Cleanup Trap (Robust)
|
# 2. Define Cleanup Trap (Robust)
|
||||||
cleanup() {
|
cleanup() {
|
||||||
# If the script failed before creating the new root, make sure we create it
|
|
||||||
if [[ ! -e /primary_tmp/@root ]]; then
|
if [[ ! -e /primary_tmp/@root ]]; then
|
||||||
echo "Cleanup: Creating new @root"
|
echo "Cleanup: Creating new @root"
|
||||||
bcachefs subvolume create /primary_tmp/@root
|
bcachefs subvolume create /primary_tmp/@root
|
||||||
fi
|
fi
|
||||||
# Robust replacement for 'mountpoint -q'
|
|
||||||
if ls /primary_tmp; then
|
|
||||||
echo "Cleanup: Unmounting /primary_tmp"
|
echo "Cleanup: Unmounting /primary_tmp"
|
||||||
umount /primary_tmp
|
umount /primary_tmp || true
|
||||||
fi
|
|
||||||
}
|
}
|
||||||
trap cleanup EXIT
|
trap cleanup EXIT
|
||||||
|
|
||||||
|
|
@ -297,7 +287,6 @@ lib.mkMerge [
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# 5. Snapshot & Prune Logic
|
|
||||||
if [[ -e /primary_tmp/@root ]]; then
|
if [[ -e /primary_tmp/@root ]]; then
|
||||||
mkdir -p /primary_tmp/@snapshots/old_roots
|
mkdir -p /primary_tmp/@snapshots/old_roots
|
||||||
|
|
||||||
|
|
@ -309,63 +298,6 @@ lib.mkMerge [
|
||||||
|
|
||||||
echo "Deleting current @root"
|
echo "Deleting current @root"
|
||||||
bcachefs subvolume delete /primary_tmp/@root
|
bcachefs subvolume delete /primary_tmp/@root
|
||||||
|
|
||||||
# --- PRUNING LOGIC ---
|
|
||||||
echo "Pruning snapshots..."
|
|
||||||
|
|
||||||
# Get list of snapshots sorted by name (which effectively sorts by date: YYYY-MM-DD...)
|
|
||||||
# ls -r puts newest first
|
|
||||||
snapshots=$(ls -1Ar /primary_tmp/@snapshots/old_roots)
|
|
||||||
|
|
||||||
declare -A kept_weeks
|
|
||||||
declare -A kept_months
|
|
||||||
processed_count=0
|
|
||||||
|
|
||||||
for snap in $snapshots; do
|
|
||||||
keep=false
|
|
||||||
|
|
||||||
# Parse date from filename (Replace _ with space for date command)
|
|
||||||
date_str=$(echo "$snap" | sed 's/_/ /')
|
|
||||||
|
|
||||||
# Get metadata
|
|
||||||
ts=$(date -d "$date_str" +%s)
|
|
||||||
week_id=$(date -d "$date_str" +%Y-W%U)
|
|
||||||
month_id=$(date -d "$date_str" +%Y-%m)
|
|
||||||
now=$(date +%s)
|
|
||||||
days_old=$(( (now - ts) / 86400 ))
|
|
||||||
|
|
||||||
# RULE 1: Keep 5 most recent (Always)
|
|
||||||
if [ $processed_count -lt 5 ]; then
|
|
||||||
keep=true
|
|
||||||
fi
|
|
||||||
|
|
||||||
# RULE 2: Weekly for last month (Age < 32 days)
|
|
||||||
# "at least 1 root from each week from the last month"
|
|
||||||
if [ $days_old -le 32 ]; then
|
|
||||||
if [ -z "''${kept_weeks[$week_id]}" ]; then
|
|
||||||
keep=true
|
|
||||||
kept_weeks[$week_id]=1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
# RULE 3: Monthly for older snapshots
|
|
||||||
# "at least 1 root from a month ago" (implies monthly retention indefinitely)
|
|
||||||
if [ $days_old -gt 32 ]; then
|
|
||||||
if [ -z "''${kept_months[$month_id]}" ]; then
|
|
||||||
keep=true
|
|
||||||
kept_months[$month_id]=1
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
|
|
||||||
if [ "$keep" = true ]; then
|
|
||||||
echo "Keeping: $snap"
|
|
||||||
else
|
|
||||||
echo "Deleting: $snap"
|
|
||||||
bcachefs subvolume delete "/primary_tmp/@snapshots/old_roots/$snap"
|
|
||||||
fi
|
|
||||||
|
|
||||||
processed_count=$((processed_count + 1))
|
|
||||||
done
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Trap handles creating new root and unmount
|
# Trap handles creating new root and unmount
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue