Преглед изворни кода

feat: add reset and restore scripts with automatic backup

Implement comprehensive data management tools for production deployments:
- reset.sh: Creates timestamped backup before removing all user data
- restore.sh: Interactive restoration from available backups
- Backups include database, .env config, and service logs
- Stored in /var/backups/webshop-scraper/ with proper permissions
- Updated README with usage documentation

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Fszontagh пре 8 месеци
родитељ
комит
6e097373c3
3 измењених фајлова са 423 додато и 0 уклоњено
  1. 19 0
      README.md
  2. 232 0
      scripts/reset.sh
  3. 172 0
      scripts/restore.sh

+ 19 - 0
README.md

@@ -64,6 +64,25 @@ npm start
 sudo ./scripts/install.sh
 sudo ./scripts/install.sh
 ```
 ```
 
 
+### Reset and Restore
+
+Reset the system to a clean state (creates backup first):
+```bash
+sudo ./scripts/reset.sh
+```
+
+Restore from a previous backup:
+```bash
+sudo ./scripts/restore.sh
+```
+
+**What gets backed up:**
+- SQLite database (data/shops.db)
+- Environment configuration (.env)
+- Service logs from journalctl
+
+Backups are stored in `/var/backups/webshop-scraper/` with timestamps.
+
 ## Vector Search & MCP Integration
 ## Vector Search & MCP Integration
 
 
 The system includes optional Qdrant vector search capabilities for semantic search of scraped content:
 The system includes optional Qdrant vector search capabilities for semantic search of scraped content:

+ 232 - 0
scripts/reset.sh

@@ -0,0 +1,232 @@
+#!/bin/bash
+
+set -e
+
+echo "========================================="
+echo "Webshop Scraper Reset Script"
+echo "========================================="
+echo ""
+echo "WARNING: This will:"
+echo "  1. Create a backup of all data"
+echo "  2. Stop the systemd service"
+echo "  3. Remove all user/variable data"
+echo "  4. Restart the systemd service"
+echo ""
+
+# Check if running as root
+if [ "$EUID" -ne 0 ]; then
+    echo "Error: Please run as root (use sudo)"
+    exit 1
+fi
+
+# Get the actual user who ran sudo
+ACTUAL_USER=${SUDO_USER:-$USER}
+
+# Configuration
+INSTALL_DIR="/opt/webshop-scraper"
+SERVICE_NAME="webshop-scraper"
+BACKUP_DIR="/var/backups/webshop-scraper"
+TIMESTAMP=$(date +"%Y%m%d_%H%M%S")
+BACKUP_FILE="$BACKUP_DIR/webshop-scraper-backup-$TIMESTAMP.tar.gz"
+
+# Check if service is installed
+if [ ! -f "/etc/systemd/system/$SERVICE_NAME.service" ]; then
+    echo "Error: Service $SERVICE_NAME is not installed"
+    echo "Run ./scripts/install.sh first to install the service"
+    exit 1
+fi
+
+# Check if installation directory exists
+if [ ! -d "$INSTALL_DIR" ]; then
+    echo "Error: Installation directory $INSTALL_DIR does not exist"
+    exit 1
+fi
+
+# Confirm reset
+echo "Are you sure you want to reset? This will remove all data! (yes/no)"
+read -r CONFIRM
+
+if [ "$CONFIRM" != "yes" ]; then
+    echo "Reset cancelled."
+    exit 0
+fi
+
+echo ""
+echo "========================================="
+echo "Step 1: Creating Backup"
+echo "========================================="
+echo ""
+
+# Create backup directory
+mkdir -p "$BACKUP_DIR"
+
+# Get service logs before stopping
+echo "Collecting service logs..."
+TEMP_LOG_FILE="/tmp/webshop-scraper-logs-$TIMESTAMP.log"
+if journalctl -u "$SERVICE_NAME" --no-pager > "$TEMP_LOG_FILE" 2>/dev/null; then
+    echo "✓ Service logs collected"
+else
+    echo "⚠ Warning: Could not collect service logs"
+    TEMP_LOG_FILE=""
+fi
+
+# Create list of files to backup
+BACKUP_ITEMS=""
+
+# Check and add data directory
+if [ -d "$INSTALL_DIR/data" ]; then
+    BACKUP_ITEMS="$BACKUP_ITEMS data"
+    echo "✓ Found data directory ($(du -sh $INSTALL_DIR/data 2>/dev/null | cut -f1))"
+fi
+
+# Check and add .env file
+if [ -f "$INSTALL_DIR/.env" ]; then
+    BACKUP_ITEMS="$BACKUP_ITEMS .env"
+    echo "✓ Found .env file"
+fi
+
+# Add logs if collected
+if [ -n "$TEMP_LOG_FILE" ] && [ -f "$TEMP_LOG_FILE" ]; then
+    BACKUP_ITEMS="$BACKUP_ITEMS -C /tmp $(basename $TEMP_LOG_FILE)"
+fi
+
+if [ -z "$BACKUP_ITEMS" ]; then
+    echo "⚠ Warning: No data found to backup"
+else
+    echo ""
+    echo "Creating backup archive: $BACKUP_FILE"
+
+    # Change to installation directory for tar
+    cd "$INSTALL_DIR"
+
+    # Create backup with progress
+    if [ -n "$TEMP_LOG_FILE" ] && [ -f "$TEMP_LOG_FILE" ]; then
+        # Backup with logs
+        tar -czf "$BACKUP_FILE" $BACKUP_ITEMS 2>/dev/null
+    else
+        # Backup without logs
+        tar -czf "$BACKUP_FILE" $BACKUP_ITEMS 2>/dev/null
+    fi
+
+    # Clean up temp log file
+    [ -n "$TEMP_LOG_FILE" ] && [ -f "$TEMP_LOG_FILE" ] && rm -f "$TEMP_LOG_FILE"
+
+    # Set ownership
+    chown $ACTUAL_USER:$ACTUAL_USER "$BACKUP_FILE"
+    chmod 600 "$BACKUP_FILE"
+
+    BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
+    echo "✓ Backup created: $BACKUP_FILE ($BACKUP_SIZE)"
+fi
+
+echo ""
+echo "========================================="
+echo "Step 2: Stopping Service"
+echo "========================================="
+echo ""
+
+# Stop service if running
+if systemctl is-active --quiet "$SERVICE_NAME"; then
+    echo "Stopping service $SERVICE_NAME..."
+    systemctl stop "$SERVICE_NAME"
+    echo "✓ Service stopped"
+
+    # Wait a moment to ensure service is fully stopped
+    sleep 2
+else
+    echo "Service is already stopped"
+fi
+
+echo ""
+echo "========================================="
+echo "Step 3: Removing Data"
+echo "========================================="
+echo ""
+
+# Remove data directory
+if [ -d "$INSTALL_DIR/data" ]; then
+    echo "Removing data directory..."
+    rm -rf "$INSTALL_DIR/data"
+    echo "✓ Data directory removed"
+fi
+
+# Recreate empty data directory
+mkdir -p "$INSTALL_DIR/data"
+chown $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR/data"
+echo "✓ Created fresh data directory"
+
+# Remove .env file (will be recreated by service)
+if [ -f "$INSTALL_DIR/.env" ]; then
+    echo "Removing .env file..."
+    rm -f "$INSTALL_DIR/.env"
+    echo "✓ .env file removed"
+    echo ""
+    echo "⚠ WARNING: You need to restore or create a new .env file before starting the service!"
+    echo "  You can:"
+    echo "    1. Copy from your project directory: sudo cp /path/to/project/.env $INSTALL_DIR/.env"
+    echo "    2. Extract from backup: tar -xzf $BACKUP_FILE -C $INSTALL_DIR .env"
+    echo "    3. Create manually: sudo nano $INSTALL_DIR/.env"
+fi
+
+echo ""
+echo "========================================="
+echo "Step 4: Restarting Service"
+echo "========================================="
+echo ""
+
+# Check if .env exists before restarting
+if [ ! -f "$INSTALL_DIR/.env" ]; then
+    echo "⚠ Cannot start service: .env file is missing"
+    echo ""
+    echo "Please restore or create .env file first, then manually start the service:"
+    echo "  sudo systemctl start $SERVICE_NAME"
+else
+    echo "Starting service $SERVICE_NAME..."
+    systemctl start "$SERVICE_NAME"
+
+    # Wait a moment and check status
+    sleep 2
+
+    if systemctl is-active --quiet "$SERVICE_NAME"; then
+        echo "✓ Service started successfully"
+    else
+        echo "⚠ Warning: Service failed to start"
+        echo ""
+        echo "Check the status with:"
+        echo "  sudo systemctl status $SERVICE_NAME"
+        echo "  sudo journalctl -u $SERVICE_NAME -n 50"
+    fi
+fi
+
+echo ""
+echo "========================================="
+echo "Reset Complete!"
+echo "========================================="
+echo ""
+
+if [ -f "$BACKUP_FILE" ]; then
+    echo "Backup Information:"
+    echo "  Location: $BACKUP_FILE"
+    echo "  Size: $BACKUP_SIZE"
+    echo "  Owner: $ACTUAL_USER"
+    echo ""
+    echo "To restore from backup:"
+    echo "  1. Stop service: sudo systemctl stop $SERVICE_NAME"
+    echo "  2. Extract backup: sudo tar -xzf $BACKUP_FILE -C $INSTALL_DIR"
+    echo "  3. Set permissions: sudo chown -R $ACTUAL_USER:$ACTUAL_USER $INSTALL_DIR"
+    echo "  4. Restart service: sudo systemctl start $SERVICE_NAME"
+    echo ""
+    echo "All backups are stored in: $BACKUP_DIR"
+    ls -lh "$BACKUP_DIR" 2>/dev/null || true
+else
+    echo "⚠ No backup was created (no data found)"
+fi
+
+echo ""
+echo "Service Management:"
+echo "  Start:   sudo systemctl start $SERVICE_NAME"
+echo "  Stop:    sudo systemctl stop $SERVICE_NAME"
+echo "  Restart: sudo systemctl restart $SERVICE_NAME"
+echo "  Status:  sudo systemctl status $SERVICE_NAME"
+echo "  Logs:    sudo journalctl -u $SERVICE_NAME -f"
+echo ""

+ 172 - 0
scripts/restore.sh

@@ -0,0 +1,172 @@
+#!/bin/bash
+
+set -e
+
+echo "========================================="
+echo "Webshop Scraper Restore Script"
+echo "========================================="
+echo ""
+
+# Check if running as root
+if [ "$EUID" -ne 0 ]; then
+    echo "Error: Please run as root (use sudo)"
+    exit 1
+fi
+
+# Get the actual user who ran sudo
+ACTUAL_USER=${SUDO_USER:-$USER}
+
+# Configuration
+INSTALL_DIR="/opt/webshop-scraper"
+SERVICE_NAME="webshop-scraper"
+BACKUP_DIR="/var/backups/webshop-scraper"
+
+# Check if installation directory exists
+if [ ! -d "$INSTALL_DIR" ]; then
+    echo "Error: Installation directory $INSTALL_DIR does not exist"
+    echo "Run ./scripts/install.sh first to install the service"
+    exit 1
+fi
+
+# List available backups
+echo "Available backups:"
+echo ""
+
+if [ ! -d "$BACKUP_DIR" ] || [ -z "$(ls -A $BACKUP_DIR/*.tar.gz 2>/dev/null)" ]; then
+    echo "No backups found in $BACKUP_DIR"
+    exit 1
+fi
+
+# List backups with numbers
+BACKUPS=($(ls -t "$BACKUP_DIR"/*.tar.gz))
+for i in "${!BACKUPS[@]}"; do
+    BACKUP_FILE="${BACKUPS[$i]}"
+    BACKUP_SIZE=$(du -h "$BACKUP_FILE" | cut -f1)
+    BACKUP_DATE=$(stat -c %y "$BACKUP_FILE" | cut -d'.' -f1)
+    echo "  [$((i+1))] $(basename $BACKUP_FILE)"
+    echo "      Size: $BACKUP_SIZE | Date: $BACKUP_DATE"
+done
+
+echo ""
+echo "Enter the number of the backup to restore (or 'q' to quit):"
+read -r SELECTION
+
+if [ "$SELECTION" = "q" ] || [ "$SELECTION" = "Q" ]; then
+    echo "Restore cancelled."
+    exit 0
+fi
+
+# Validate selection
+if ! [[ "$SELECTION" =~ ^[0-9]+$ ]] || [ "$SELECTION" -lt 1 ] || [ "$SELECTION" -gt "${#BACKUPS[@]}" ]; then
+    echo "Error: Invalid selection"
+    exit 1
+fi
+
+BACKUP_FILE="${BACKUPS[$((SELECTION-1))]}"
+
+echo ""
+echo "Selected backup: $(basename $BACKUP_FILE)"
+echo ""
+echo "WARNING: This will:"
+echo "  1. Stop the systemd service"
+echo "  2. Restore data from backup (existing data will be overwritten)"
+echo "  3. Restart the systemd service"
+echo ""
+echo "Are you sure you want to continue? (yes/no)"
+read -r CONFIRM
+
+if [ "$CONFIRM" != "yes" ]; then
+    echo "Restore cancelled."
+    exit 0
+fi
+
+echo ""
+echo "========================================="
+echo "Step 1: Stopping Service"
+echo "========================================="
+echo ""
+
+# Stop service if running
+if systemctl is-active --quiet "$SERVICE_NAME"; then
+    echo "Stopping service $SERVICE_NAME..."
+    systemctl stop "$SERVICE_NAME"
+    echo "✓ Service stopped"
+    sleep 2
+else
+    echo "Service is already stopped"
+fi
+
+echo ""
+echo "========================================="
+echo "Step 2: Restoring Backup"
+echo "========================================="
+echo ""
+
+# List contents of backup
+echo "Backup contents:"
+tar -tzf "$BACKUP_FILE" | head -20
+echo ""
+
+# Extract backup
+echo "Extracting backup to $INSTALL_DIR..."
+cd "$INSTALL_DIR"
+
+# Extract with overwrite
+tar -xzf "$BACKUP_FILE" --overwrite 2>/dev/null || {
+    echo "Error: Failed to extract backup"
+    exit 1
+}
+
+echo "✓ Backup extracted successfully"
+
+# Set correct permissions
+echo "Setting permissions..."
+chown -R $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR/data" 2>/dev/null || true
+chown $ACTUAL_USER:$ACTUAL_USER "$INSTALL_DIR/.env" 2>/dev/null || true
+chmod 600 "$INSTALL_DIR/.env" 2>/dev/null || true
+
+echo "✓ Permissions set"
+
+echo ""
+echo "========================================="
+echo "Step 3: Starting Service"
+echo "========================================="
+echo ""
+
+# Check if .env exists
+if [ ! -f "$INSTALL_DIR/.env" ]; then
+    echo "⚠ Warning: .env file not found in backup"
+    echo "You need to create or restore .env file manually before starting the service"
+    echo ""
+    echo "After creating .env, start the service with:"
+    echo "  sudo systemctl start $SERVICE_NAME"
+else
+    echo "Starting service $SERVICE_NAME..."
+    systemctl start "$SERVICE_NAME"
+
+    # Wait and check status
+    sleep 2
+
+    if systemctl is-active --quiet "$SERVICE_NAME"; then
+        echo "✓ Service started successfully"
+    else
+        echo "⚠ Warning: Service failed to start"
+        echo ""
+        echo "Check the status with:"
+        echo "  sudo systemctl status $SERVICE_NAME"
+        echo "  sudo journalctl -u $SERVICE_NAME -n 50"
+    fi
+fi
+
+echo ""
+echo "========================================="
+echo "Restore Complete!"
+echo "========================================="
+echo ""
+echo "Restored from: $(basename $BACKUP_FILE)"
+echo ""
+echo "Service Management:"
+echo "  Status:  sudo systemctl status $SERVICE_NAME"
+echo "  Logs:    sudo journalctl -u $SERVICE_NAME -f"
+echo "  Restart: sudo systemctl restart $SERVICE_NAME"
+echo ""