#!/usr/bin/env sh # restore-config.sh # Lista y restaura backups creados por start.sh (config.local.yml.bak.YYYYMMDDHHMMSS) set -e WORKDIR="$(pwd)" BACKUP_GLOB="$WORKDIR/config.local.yml.bak.*" DEST="$WORKDIR/config.local.yml" usage() { cat <] [--yes] Options: --list List available backup files --latest Show the latest backup file (implies --list) --restore FILE Restore the specified backup into config.local.yml --yes Skip confirmation when restoring Examples: $0 --list $0 --latest $0 --restore ./config.local.yml.bak.20251001102251 $0 --restore latest --yes EOF } list_backups() { ls -1 $BACKUP_GLOB 2>/dev/null | sort || true } latest_backup() { # sort by name (timestamp suffix) and pick last ls -1 $BACKUP_GLOB 2>/dev/null | sort | tail -n 1 || true } if [ "$#" -eq 0 ]; then usage exit 0 fi FORCE=0 MODE="" TARGET="" while [ "$#" -gt 0 ]; do case "$1" in --list) MODE=list; shift ;; --latest) MODE=latest; shift ;; --restore) MODE=restore; TARGET="$2"; shift 2 ;; --yes) FORCE=1; shift ;; -h|--help) usage; exit 0 ;; *) echo "Unknown arg: $1" >&2; usage; exit 1 ;; esac done case "$MODE" in list) echo "Backups:"; list_backups; exit 0 ;; latest) echo "Latest backup:"; latest_backup; exit 0 ;; restore) if [ "$TARGET" = "latest" ] || [ -z "$TARGET" ]; then TARGET=$(latest_backup) fi if [ -z "$TARGET" ] || [ ! -f "$TARGET" ]; then echo "No backup found to restore: $TARGET" >&2 exit 1 fi echo "About to restore backup: $TARGET -> $DEST" if [ "$FORCE" -ne 1 ]; then printf "Continue and overwrite %s? [y/N]: " "$DEST" read ans || true case "$ans" in y|Y|yes|YES) ;; *) echo "Aborted."; exit 1 ;; esac fi cp "$TARGET" "$DEST" echo "Restored $TARGET -> $DEST" exit 0 ;; *) usage; exit 1 ;; esac