@@ -330,6 +330,65 @@ update_version_file() {
330330 fi
331331}
332332
333+ # ============================================================================
334+ # PRE-UPDATE BACKUP
335+ # ============================================================================
336+ # Creates a timestamped backup of every file that is about to be overwritten.
337+ # Only files that already exist on disk AND appear in the update package are
338+ # backed up – so the snapshot reflects exactly what will change.
339+ #
340+ # Backup location:
341+ # /home/container/.autoupdate_prebackup_<fromVer>_to_<toVer>_<timestamp>/
342+ #
343+ # A human-readable .backup_info file is written into the backup root so you
344+ # can always tell at a glance what the backup contains and when it was made.
345+ # ============================================================================
346+ create_pre_update_backup () {
347+ local extract_dir=" $1 "
348+ local from_version=" $2 "
349+ local to_version=" $3 "
350+
351+ local timestamp
352+ timestamp=$( date +%Y%m%d_%H%M%S)
353+ local backup_dir=" ${CONTAINER_ROOT} /.autoupdate_prebackup_${from_version} _to_${to_version} _${timestamp} "
354+ mkdir -p " $backup_dir "
355+
356+ echo -e " ${CYAN} [AutoUpdate] 💾 Creating pre-update backup...${NC} "
357+ local backed_up=0
358+
359+ # Walk every file present in the extracted update package
360+ while IFS= read -r -d ' ' extracted_file; do
361+ local rel_path=" ${extracted_file# ${extract_dir} / } "
362+ local live_file=" ${CONTAINER_ROOT} /${rel_path} "
363+
364+ # Only back up files that actually exist on disk right now
365+ if [[ -f " $live_file " ]]; then
366+ local target_dir
367+ target_dir=$( dirname " ${backup_dir} /${rel_path} " )
368+ mkdir -p " $target_dir "
369+ cp " $live_file " " ${backup_dir} /${rel_path} "
370+ backed_up=$(( backed_up + 1 ))
371+ fi
372+ done < <( find " $extract_dir " -type f -print0)
373+
374+ if [[ $backed_up -gt 0 ]]; then
375+ # Write human-readable info file
376+ {
377+ echo " Pre-update backup"
378+ echo " From version : $from_version "
379+ echo " To version : $to_version "
380+ echo " Created at : $( date) "
381+ echo " Files backed up: $backed_up "
382+ } > " ${backup_dir} /.backup_info"
383+
384+ echo -e " ${GREEN} [AutoUpdate] ✓ Backed up ${backed_up} file(s) → $( basename " $backup_dir " ) ${NC} "
385+ echo -e " ${CYAN} [AutoUpdate] Backup location: ${backup_dir}${NC} "
386+ else
387+ echo -e " ${YELLOW} [AutoUpdate] No existing files to back up${NC} "
388+ rmdir " $backup_dir " 2> /dev/null || true
389+ fi
390+ }
391+
333392# Function to download and apply diff
334393apply_update () {
335394 local from_version=" $1 "
@@ -456,13 +515,31 @@ apply_update() {
456515 rm -f " $zip_file "
457516 return 1
458517 fi
459-
518+
460519 # Apply updates only to allowed directories and files
461520 local updated_files=0
462521 local allowed_dirs=(" modules" " nginx" " php" )
463522 local allowed_files=(" start-modules.sh" " README.md" " LICENSE" )
523+ # Files that must never be overwritten by updates (relative to CONTAINER_ROOT)
524+ local protected_files=(" nginx/conf.d/default.conf" )
464525 local self_update_required=false
465-
526+
527+ # Remove protected files from the extraction directory before backup and copy.
528+ # This ensures they are neither backed up (they won't change) nor overwritten.
529+ echo -e " ${CYAN} [AutoUpdate] Applying file protection rules...${NC} "
530+ for protected in " ${protected_files[@]} " ; do
531+ local protected_path=" ${extract_dir} /${protected} "
532+ if [[ -f " $protected_path " ]]; then
533+ rm -f " $protected_path "
534+ echo -e " ${YELLOW} [AutoUpdate] 🔒 Protected (skipped): ${protected}${NC} "
535+ fi
536+ done
537+
538+ # Back up every file that exists on disk and is about to be overwritten.
539+ # Runs AFTER protected files are removed so the backup only covers what
540+ # will actually change.
541+ create_pre_update_backup " $extract_dir " " $from_version " " $to_version "
542+
466543 # Check if autoupdate module itself needs updating
467544 if [[ -f " ${extract_dir} /modules/autoupdate/start.sh" ]]; then
468545 echo -e " ${YELLOW} [AutoUpdate] ⚠ Auto-update module itself has updates${NC} "
@@ -631,4 +708,4 @@ main() {
631708main || echo -e " ${YELLOW} [AutoUpdate] Update check completed${NC} "
632709
633710# Clean up temp directory
634- rm -rf " $TEMP_DIR " 2> /dev/null || true
711+ rm -rf " $TEMP_DIR " 2> /dev/null || true
0 commit comments