From b4a6b8d4d3cb8747259e7313ff9dc9e3f8a3fd37 Mon Sep 17 00:00:00 2001 From: knee-cola Date: Sun, 24 Aug 2025 08:49:21 +0000 Subject: [PATCH] restore: add interactive pre-backup prompt with automation flags --- restore.sh | 67 +++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 56 insertions(+), 11 deletions(-) diff --git a/restore.sh b/restore.sh index f65caae..eef355c 100755 --- a/restore.sh +++ b/restore.sh @@ -4,19 +4,44 @@ set -euo pipefail # Configuration MONGO_SERVICE="utility-bills-tracker_mongo" -# Usage: ./restore.sh -# Example: ./restore.sh 2025-08-24-14-30 +# Parse command line options +SKIP_PRE_BACKUP=false +NO_PRE_BACKUP=false -if [ $# -ne 1 ]; then - echo "Usage: $0 " +while [[ $# -gt 0 ]]; do + case $1 in + --skip-pre-backup) + SKIP_PRE_BACKUP=true + shift + ;; + --no-pre-backup) + NO_PRE_BACKUP=true + shift + ;; + *) + TIMESTAMP="$1" + shift + ;; + esac +done + +# Usage: ./restore.sh [options] +# Example: ./restore.sh 2025-08-24-14-30 +# Options: --skip-pre-backup (skip interactive prompt, create backup) +# --no-pre-backup (skip interactive prompt, no backup) + +if [ -z "${TIMESTAMP:-}" ]; then + echo "Usage: $0 [options] " echo "Example: $0 2025-08-24-14-30" echo "" + echo "Options:" + echo " --skip-pre-backup Skip interactive prompt, create pre-restore backup" + echo " --no-pre-backup Skip interactive prompt, no pre-restore backup" + echo "" echo "Available backups:" ls -1t backups/mongo-volume-backup-*.tar.gz 2>/dev/null | sed 's/.*mongo-volume-backup-\(.*\)\.tar\.gz/ \1/' || echo " No backups found" exit 1 fi - -TIMESTAMP="$1" BACKUP_DIR="${BACKUP_DIR:-backups}" BACKUP_FILE="$BACKUP_DIR/mongo-volume-backup-$TIMESTAMP.tar.gz" @@ -42,10 +67,28 @@ fi echo "Scaling down mongo service..." docker service scale "$MONGO_SERVICE"=0 -# Backup current volume (just in case) -SAFETY_BACKUP="$BACKUP_DIR/mongo-volume-pre-restore-$(date +%Y-%m-%d-%H-%M).tar.gz" -echo "Creating safety backup: $SAFETY_BACKUP" -sudo tar -czf "$SAFETY_BACKUP" mongo-volume 2>/dev/null || true +# Handle pre-restore backup +CREATE_PRE_BACKUP=true +if [ "$NO_PRE_BACKUP" = true ]; then + CREATE_PRE_BACKUP=false +elif [ "$SKIP_PRE_BACKUP" = false ]; then + # Ask user interactively + read -p "Create pre-restore safety backup? (Y/n): " -n 1 -r + echo + if [[ $REPLY =~ ^[Nn]$ ]]; then + CREATE_PRE_BACKUP=false + fi +fi + +# Create pre-restore backup if requested +if [ "$CREATE_PRE_BACKUP" = true ]; then + SAFETY_BACKUP="$BACKUP_DIR/mongo-volume-pre-restore-$(date +%Y-%m-%d-%H-%M).tar.gz" + echo "Creating safety backup: $SAFETY_BACKUP" + sudo tar -czf "$SAFETY_BACKUP" mongo-volume 2>/dev/null || true +else + echo "Skipping pre-restore backup" + SAFETY_BACKUP="" +fi # Remove current volume echo "Removing current mongo-volume..." @@ -64,4 +107,6 @@ echo "Scaling mongo service back up..." docker service scale "$MONGO_SERVICE"=1 echo "Restore completed successfully!" -echo "Safety backup saved as: $SAFETY_BACKUP" +if [ -n "$SAFETY_BACKUP" ]; then + echo "Safety backup saved as: $SAFETY_BACKUP" +fi