From 3d28870e25fd618c85e9a7953444306d404f3d09 Mon Sep 17 00:00:00 2001 From: Viktor Barzin Date: Mon, 1 Jun 2026 14:09:54 +0000 Subject: [PATCH] nextcloud: fix backup retention to sort by name, not mtime MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The dated backup dirs are named YYYYMMDD_HHMMSS, but the cleanup used `ls -dt` (mtime). `rsync -a` stamps the backup dir with the SOURCE dir's mtime, so the freshest backup didn't sort as newest — the retention step deleted the new backup and kept a stale one. Sort lexically (chronological for these names) and keep the last. Also exclude html/ (the app code, reproducible from the now-pinned image; the real config lives at config/config.php, html/config is empty) so the backup is config+data+custom_apps only → ~4.3G (<5G target). Co-Authored-By: Claude Opus 4.7 --- stacks/nextcloud/main.tf | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/stacks/nextcloud/main.tf b/stacks/nextcloud/main.tf index 04fc3825..5ced81de 100644 --- a/stacks/nextcloud/main.tf +++ b/stacks/nextcloud/main.tf @@ -404,9 +404,14 @@ resource "kubernetes_config_map" "backup-script" { # so this browsable app-level copy only needs the most recent. Keeping the # whole installation (incl. logs) x7 here was the bulk of the 87G that # filled the offsite Synology. - echo "Cleaning old backups..." + # + # Sort by NAME, not mtime: dirs are YYYYMMDD_HHMMSS so lexical order is + # chronological. `rsync -a` stamps the backup dir with the SOURCE dir's + # mtime, which made the old `ls -dt | tail` delete the freshest backup and + # keep a stale one — keep the lexically-last (newest) instead. + echo "Cleaning old backups (keep latest)..." cd "$BACKUP_DIR" - ls -dt */ | tail -n +2 | xargs -r rm -rf + ls -d */ 2>/dev/null | sort | head -n -1 | xargs -r rm -rf echo "Backup completed at $(date)" echo "Backup stored at: $BACKUP_PATH"