#!/bin/bash set -e echo "=========================================" echo "Web 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 ""