From ffa9960c649fdd5f28dc7e118f223a048eaa5014 Mon Sep 17 00:00:00 2001 From: Fabio Scotto di Santolo Date: Sun, 15 Nov 2026 22:12:50 +0100 Subject: [PATCH] Reuse verified Nextcloud bundles within 20h before Borg/USB backups --- AGENTS.md | 13 ++++++++ ansible/roles/profile_atlas/defaults/main.yml | 5 +++ .../profile_atlas/tasks/nextcloud_backup.yml | 5 ++- .../atlas-nextcloud-backup.service.j2 | 3 ++ .../templates/atlas-nextcloud-backup.sh.j2 | 31 +++++++++++++++++++ docs/atlas-nextcloud.md | 27 +++++++++++----- 6 files changed, 76 insertions(+), 8 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 371e6d4..a895796 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -230,6 +230,19 @@ The dotfile vars follow the same split: `desktop_common_dotfiles` carries mode-i injecting the private token for JSON-RPC POST clients; keep this route authenticated and never expose the gateway directly. See `docs/atlas-aria2.md`. +- Atlas Nextcloud backup preparation (`atlas-nextcloud-backup.service`) is an ordered + requirement of the nightly `atlas-borg-backup.service` and the operator-started + `atlas-usb-backup.service`. A structurally complete bundle younger than + `atlas_nextcloud_backup_reuse_max_seconds` (20 h, asserted below 24 h so the nightly + Borg cycle always refreshes it) is reused without pausing the applications: a manual + USB run therefore reaches its LUKS prompt within seconds, while preparation failure + still blocks the dependent job. Force one fresh preparation with + `systemctl set-environment ATLAS_NEXTCLOUD_BACKUP_FORCE=1` before the start and unset + the variable afterwards. Deployed and validated on 2026-11-15: a dependency start reused + a verified bundle 14 h old and skipped preparation in 14 ms, and an operator-started + USB run reached its LUKS prompt in under a second; the FORCE path has not been exercised + in production yet. See `docs/atlas-nextcloud.md`. + ## Atlas NAS TODO Completed validation: the existing RAIDZ2 pool and datasets, SELinux, LAN firewall, SSH, Cockpit with the selected 45Drives plugins, encrypted SMB3 `Archive`, the Aegis-only NFSv4 `photobook` export, and the diff --git a/ansible/roles/profile_atlas/defaults/main.yml b/ansible/roles/profile_atlas/defaults/main.yml index ae280c1..ed7bb7a 100644 --- a/ansible/roles/profile_atlas/defaults/main.yml +++ b/ansible/roles/profile_atlas/defaults/main.yml @@ -18,6 +18,11 @@ atlas_nextcloud_redis_image: "" atlas_onlyoffice_image: "" atlas_nextcloud_backup_root: "{{ atlas_mount_root }}/backup/nextcloud" atlas_nextcloud_backup_keep: 2 +# A structurally complete bundle from an earlier verified run younger than +# this many seconds is reused instead of re-preparing. Keep it below 24h so +# the nightly Borg run, spaced 24h plus its randomized delay, always refreshes +# the bundle; 0 disables reuse. +atlas_nextcloud_backup_reuse_max_seconds: 72000 atlas_nextcloud_admin: admin atlas_nextcloud_users: [] atlas_nextcloud_apps: [] diff --git a/ansible/roles/profile_atlas/tasks/nextcloud_backup.yml b/ansible/roles/profile_atlas/tasks/nextcloud_backup.yml index 07711dd..6b1c14d 100644 --- a/ansible/roles/profile_atlas/tasks/nextcloud_backup.yml +++ b/ansible/roles/profile_atlas/tasks/nextcloud_backup.yml @@ -3,11 +3,14 @@ tags: [atlas, nextcloud_backup] when: atlas_manage_nextcloud | bool block: - - name: Validate private backup scope and local bundle retention + - name: Validate private backup scope, retention, and bundle reuse window ansible.builtin.assert: that: - atlas_nextcloud_backup_root == atlas_mount_root ~ '/backup/nextcloud' - atlas_nextcloud_backup_keep | int >= 2 + - atlas_nextcloud_backup_reuse_max_seconds | int >= 0 + # Below 24h the nightly Borg run cannot reuse the previous bundle. + - atlas_nextcloud_backup_reuse_max_seconds | int < 86400 - atlas_manage_borg_backup | bool - atlas_manage_usb_backup | bool diff --git a/ansible/roles/profile_atlas/templates/atlas-nextcloud-backup.service.j2 b/ansible/roles/profile_atlas/templates/atlas-nextcloud-backup.service.j2 index 58ae0f8..29a049f 100644 --- a/ansible/roles/profile_atlas/templates/atlas-nextcloud-backup.service.j2 +++ b/ansible/roles/profile_atlas/templates/atlas-nextcloud-backup.service.j2 @@ -14,6 +14,9 @@ StateDirectory=atlas-nextcloud-backup StateDirectoryMode=0700 ExecStart=/usr/local/sbin/atlas-nextcloud-backup ExecStopPost=/usr/local/sbin/atlas-nextcloud-backup --recover +# systemctl set-environment ATLAS_NEXTCLOUD_BACKUP_FORCE=1 makes the next run +# ignore the bundle reuse window; unset the variable afterwards. +PassEnvironment=ATLAS_NEXTCLOUD_BACKUP_FORCE TimeoutStartSec=3h TimeoutStopSec=5min Nice=10 diff --git a/ansible/roles/profile_atlas/templates/atlas-nextcloud-backup.sh.j2 b/ansible/roles/profile_atlas/templates/atlas-nextcloud-backup.sh.j2 index 4758f5f..886e809 100644 --- a/ansible/roles/profile_atlas/templates/atlas-nextcloud-backup.sh.j2 +++ b/ansible/roles/profile_atlas/templates/atlas-nextcloud-backup.sh.j2 @@ -7,6 +7,7 @@ readonly source_root={{ atlas_nextcloud_root | quote }} readonly backup_root={{ atlas_nextcloud_backup_root | quote }} readonly state=/var/lib/atlas-nextcloud-backup readonly keep={{ atlas_nextcloud_backup_keep | int }} +readonly reuse_max={{ atlas_nextcloud_backup_reuse_max_seconds | int }} readonly owner={{ atlas_admin_username | quote }} readonly uid={{ atlas_admin_uid | int }} @@ -64,6 +65,36 @@ recover() { } if [[ "${1:-}" == --recover ]]; then recover; exit; fi recover +# Fast path: a structurally complete bundle from an earlier verified run is +# reused while younger than the reuse window, without pausing the applications. +# ATLAS_NEXTCLOUD_BACKUP_FORCE=1 ignores the window and prepares one anyway. +if [[ "${ATLAS_NEXTCLOUD_BACKUP_FORCE:-0}" != 1 ]] && [[ "$reuse_max" -gt 0 ]]; then + target="$(readlink -f "$backup_root/latest" 2>/dev/null || true)" + if [[ -n "$target" && "$target" == "$backup_root/versions/"* ]]; then + name="${target##*/}" + if [[ "$name" =~ ^[0-9]{8}T[0-9]{6}Z-[0-9]+$ ]]; then + bundle_utc="${name:0:4}-${name:4:2}-${name:6:2} ${name:9:2}:${name:11:2}:${name:13:2} UTC" + created="$(date -u -d "$bundle_utc" +%s 2>/dev/null || true)" + if [[ -n "$created" ]]; then + now="$(date -u +%s)" + if [[ "$((now - created))" -le "$reuse_max" ]]; then + reusable=true + for component in manifest.txt SHA256SUMS database.dump \ + postgres-globals.sql database-toc.txt images.txt; do + [[ -s "$target/$component" ]] || { reusable=false; break; } + done + for component in app files; do + [[ -d "$target/$component" ]] || { reusable=false; break; } + done + if [[ "$reusable" == true ]]; then + echo "Reusing verified Nextcloud bundle $name (age $((now - created))s <= ${reuse_max}s); preparation skipped" + exit 0 + fi + fi + fi + fi + fi +fi [[ "$(zfs get -H -o value mounted "$dataset")" == yes ]] [[ "$(zfs get -H -o value mountpoint "$dataset")" == "$source_root" ]] [[ "$(zfs get -H -o value mounted {{ (atlas_zfs_pool ~ '/backup') | quote }})" == yes ]] diff --git a/docs/atlas-nextcloud.md b/docs/atlas-nextcloud.md index d09f596..e5b63e0 100644 --- a/docs/atlas-nextcloud.md +++ b/docs/atlas-nextcloud.md @@ -141,11 +141,22 @@ See `atlas-nextcloud-recovery-test.md` for scope, retained artifact and limitati ## Recurring consistent bundles -`atlas-nextcloud-backup.service` is now an ordered requirement of both +`atlas-nextcloud-backup.service` is an ordered requirement of both `atlas-borg-backup.service` and the operator-started `atlas-usb-backup.service`. No additional backup timer is needed: the existing Borg schedule prepares a fresh -bundle before its pool snapshot, and a manual USB run does the same. Preparation -failure blocks the dependent job rather than silently using an old dump. +bundle before its pool snapshot. A structurally complete bundle from an earlier +verified run is reused instead while younger than +`atlas_nextcloud_backup_reuse_max_seconds` (20 h): a manual USB run then skips +the application pause, reaches its LUKS prompt within seconds and captures the +latest Borg-prepared bundle, while the remaining pool snapshot reflects the +moment of the run. Reuse does not re-hash the bundle; it was checksum-verified +at publication. The window is asserted below 24 h so the nightly Borg run, +spaced 24 h plus its randomized delay, never reuses the previous night's bundle. +Preparation failure still blocks the dependent job rather than silently using an +old dump; only a verified, bounded-age bundle may be reused. Set +`ATLAS_NEXTCLOUD_BACKUP_FORCE=1` with `systemctl set-environment` (the unit +passes it through) to ignore the window once and prepare a fresh bundle; unset +the variable afterwards. The helper checks the mounted datasets and healthy active application state, serializes preparations and briefly pauses cron, Nextcloud and ONLYOFFICE. It @@ -171,10 +182,12 @@ sudo systemctl start atlas-nextcloud-backup.service sudo systemctl show atlas-nextcloud-backup.service -p Result -p ExecMainExitTimestamp ``` -The second command briefly interrupts the applications and is an explicit manual -run of the recurring job, not a normal deployment side effect. The preparation -unit is not enabled as a boot backup; only interrupted-job recovery is enabled. -Do not stop a Borg/USB job, break its lock or unmount its source snapshot to run a test. +A manual start briefly interrupts the applications only when it prepares a fresh +bundle; inside the reuse window it logs `Reusing verified ...` and finishes in +seconds without touching them. It remains an explicit manual run of the +recurring job, not a normal deployment side effect. The preparation unit is not +enabled as a boot backup; only interrupted-job recovery is enabled. Do not stop a +Borg/USB job, break its lock or unmount its source snapshot to run a test. On 2026-10-04, both a new encrypted Borg archive and USB version `20261004T101101Z-3420114` were independently extracted and their consistent