Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

Keep one upgrade snapshot and no tree copy

Juan Denis3cc90ecd30ff
diff --git a/scripts/test/test_update.sh b/scripts/test/test_update.sh
index 012d9c68..6c160e5a 100644
--- a/scripts/test/test_update.sh
+++ b/scripts/test/test_update.sh
@@ -644,6 +644,60 @@ else
bad "retention: rc=$ret_rc, left trees=$left_trees db=$left_dbs dump=$left_dumps (want 3/3/3)"
fi
+# --------------------------------------------------------------------------
+# T10i — plan 85 B3: with SERVERKIT_BACKUP_RETENTION unset, a filesystem under
+# 50 GiB keeps ONE upgrade snapshot (each is a full DB copy), a larger one
+# keeps 3, and an unreadable size keeps the old default of 3.
+# --------------------------------------------------------------------------
+t="$WORK/t10i"; mkdir -p "$t/bin" "$t/backups"
+_keep_with_df() { # $1 = total KiB df reports ('' = df fails)
+ if [ -n "$1" ]; then
+ printf '#!/usr/bin/env bash\necho "Filesystem 1024-blocks Used Available Capacity Mounted"\necho "/dev/vda1 %s 1 1 1%% /"\n' "$1" > "$t/bin/df"
+ else
+ printf '#!/usr/bin/env bash\nexit 1\n' > "$t/bin/df"
+ fi
+ chmod +x "$t/bin/df"
+ ( set -Eeuo pipefail; export PATH="$t/bin:$PATH"; BACKUP_DIR="$t/backups"; default_backup_keep )
+}
+k_small="$(_keep_with_df 26214400)" # 25 GiB
+k_large="$(_keep_with_df 524288000)" # 500 GiB
+k_unknown="$(_keep_with_df '')"
+if [ "$k_small" = 1 ] && [ "$k_large" = 3 ] && [ "$k_unknown" = 3 ]; then
+ ok "default_backup_keep: 1 snapshot under 50 GiB, 3 above or when unknown"
+else
+ bad "default_backup_keep: small=$k_small large=$k_large unknown=$k_unknown (want 1/3/3)"
+fi
+
+# --------------------------------------------------------------------------
+# T10j — plan 85 B2: on a blue/green install the active slot stays on disk as
+# the rollback, so backup_current must not write a serverkit-tree-* copy of
+# it (a second full copy of the same code per update). A plain-directory
+# install still gets its tree backup.
+# --------------------------------------------------------------------------
+t="$WORK/t10j"
+_tree_backups_after_backup_current() { # $1 = bluegreen|plain
+ rm -rf "$t"; mkdir -p "$t/opt/serverkit-a/backend" "$t/backups"
+ if [ "$1" = bluegreen ]; then
+ ln -s "$t/opt/serverkit-a" "$t/opt/serverkit"
+ else
+ mv "$t/opt/serverkit-a" "$t/opt/serverkit"
+ fi
+ (
+ set -Eeuo pipefail
+ DRY_RUN=0; INSTALL_DIR="$t/opt/serverkit"; BACKUP_DIR="$t/backups"
+ SERVERKIT_BACKUP_RETENTION=3
+ backup_current
+ ) >/dev/null 2>&1 || true
+ find "$t/backups" -mindepth 1 -maxdepth 1 -name 'serverkit-tree-*' | wc -l
+}
+trees_bg="$(_tree_backups_after_backup_current bluegreen)"
+trees_plain="$(_tree_backups_after_backup_current plain)"
+if [ "$trees_bg" -eq 0 ] && [ "$trees_plain" -eq 1 ]; then
+ ok "backup_current skips the tree copy on blue/green, keeps it for a plain install"
+else
+ bad "tree backups: bluegreen=$trees_bg plain=$trees_plain (want 0/1)"
+fi
+
# --------------------------------------------------------------------------
# T11 — zero-downtime regression: reload_nginx_graceful must RELOAD a running
# nginx and must NEVER stop it. Host nginx fronts every managed app, so a stop
diff --git a/scripts/update.sh b/scripts/update.sh
index cee3c85e..29543854 100644
--- a/scripts/update.sh
+++ b/scripts/update.sh
@@ -1113,7 +1113,12 @@ backup_current() {
local tree_backup
tree_backup="$BACKUP_DIR/serverkit-tree-$(date +%Y%m%d-%H%M%S)"
- if [ -d "$active" ]; then
+ if [ -L "$INSTALL_DIR" ] && [ -d "$active" ]; then
+ # Blue/green: the update installs into the OTHER slot, so this one
+ # stays on disk as the rollback. A tree copy of it was a second full
+ # copy of the same code (~1 GB per update on a small VPS).
+ info "Install tree kept in place as the rollback slot: $active"
+ elif [ -d "$active" ]; then
# The SQLite database is NOT part of the tree snapshot: it was just
# written as the pre-upgrade copy above, and rollback restores from
# that copy, never from the tree. Copying it here too doubled every
@@ -1684,9 +1689,26 @@ backups_size_kb() {
du -sk "$BACKUP_DIR" 2>/dev/null | awk '{print $1}' || echo 0
}
+# How many upgrade snapshots to keep when SERVERKIT_BACKUP_RETENTION is unset:
+# 1 on a filesystem under 50 GiB, else 3. Each snapshot is a full database
+# copy, and on a 25 GB VPS three of them compete with the apps for the disk.
+# An unreadable size keeps the old default.
+default_backup_keep() {
+ local total_kb
+ total_kb="$(df -Pk "$BACKUP_DIR" 2>/dev/null | awk 'NR==2 {print $2}' || true)"
+ if [ -z "$total_kb" ]; then
+ total_kb="$(df -Pk / 2>/dev/null | awk 'NR==2 {print $2}' || true)"
+ fi
+ if [ -n "$total_kb" ] && [ "$total_kb" -lt $((50 * 1024 * 1024)) ] 2>/dev/null; then
+ echo 1
+ else
+ echo 3
+ fi
+}
+
# Trim every backup kind to the retention cap. Safe to call repeatedly.
trim_backups() {
- local keep="${1:-${SERVERKIT_BACKUP_RETENTION:-3}}"
+ local keep="${1:-${SERVERKIT_BACKUP_RETENTION:-$(default_backup_keep)}}"
prune_old_backups 'serverkit-tree-*' "$keep"
prune_old_backups 'serverkit-pre-upgrade-*.db' "$keep"
prune_old_backups 'serverkit-pre-upgrade-*.dump' "$keep"