Files
SkyArtShop/migrate-restore.sh

187 lines
5.2 KiB
Bash
Executable File

#!/bin/bash
# SkyArtShop Migration Restoration Script
# Run this on the TARGET server
set -e
echo "========================================="
echo " SkyArtShop Migration - Restoration"
echo "========================================="
echo ""
# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Configuration
APP_DIR=/var/www/SkyArtShop
BACKUP_DIR=~/skyartshop-backup
# Check if backup directory exists
if [ ! -d "$BACKUP_DIR" ]; then
echo -e "${RED}✗ Backup directory not found: $BACKUP_DIR${NC}"
echo "Please transfer the backup files first."
exit 1
fi
echo -e "${BLUE}Backup directory found: $BACKUP_DIR${NC}"
echo ""
# Check for required files
echo -e "${YELLOW}[1/8] Checking backup files...${NC}"
MONGO_BACKUP=$(ls $BACKUP_DIR/mongodb-backup-*.tar.gz 2>/dev/null | head -1)
APP_BACKUP=$(ls $BACKUP_DIR/skyartshop-app-*.tar.gz 2>/dev/null | head -1)
if [ -z "$MONGO_BACKUP" ]; then
echo -e "${RED}✗ MongoDB backup not found${NC}"
exit 1
fi
if [ -z "$APP_BACKUP" ]; then
echo -e "${RED}✗ Application backup not found${NC}"
exit 1
fi
echo -e "${GREEN}✓ Found MongoDB backup: $(basename $MONGO_BACKUP)${NC}"
echo -e "${GREEN}✓ Found Application backup: $(basename $APP_BACKUP)${NC}"
echo ""
# Check prerequisites
echo -e "${YELLOW}[2/8] Checking prerequisites...${NC}"
if ! command -v dotnet &> /dev/null; then
echo -e "${RED}✗ .NET SDK not installed${NC}"
echo "Install .NET 8.0 first:"
echo " wget https://dot.net/v1/dotnet-install.sh"
echo " bash dotnet-install.sh --channel 8.0"
exit 1
else
echo -e "${GREEN}✓ .NET SDK found: $(dotnet --version)${NC}"
fi
if ! command -v mongod &> /dev/null; then
echo -e "${RED}✗ MongoDB not installed${NC}"
echo "Install MongoDB first:"
echo " See MIGRATION_PACKAGE.md for instructions"
exit 1
else
echo -e "${GREEN}✓ MongoDB found${NC}"
fi
# Check if MongoDB is running
if ! pgrep -x mongod > /dev/null; then
echo -e "${YELLOW}⚠ MongoDB not running, starting...${NC}"
sudo systemctl start mongod
sleep 3
fi
echo -e "${GREEN}✓ MongoDB is running${NC}"
echo ""
# Restore MongoDB
echo -e "${YELLOW}[3/8] Restoring MongoDB database...${NC}"
cd $BACKUP_DIR
tar -xzf $MONGO_BACKUP
mongorestore --db=SkyArtShopDB mongodb-dump/SkyArtShopDB/ --drop
rm -rf mongodb-dump/
# Verify restoration
PRODUCT_COUNT=$(mongosh SkyArtShopDB --quiet --eval "db.Products.countDocuments()" 2>/dev/null || echo "0")
MENU_COUNT=$(mongosh SkyArtShopDB --quiet --eval "db.MenuItems.countDocuments()" 2>/dev/null || echo "0")
echo -e "${GREEN}✓ MongoDB restored${NC}"
echo -e " - Products: $PRODUCT_COUNT"
echo -e " - Menu Items: $MENU_COUNT"
echo ""
# Restore application files
echo -e "${YELLOW}[4/8] Restoring application files...${NC}"
sudo mkdir -p /var/www
cd /var/www
sudo tar -xzf $APP_BACKUP
# Set ownership
sudo chown -R $USER:$USER $APP_DIR
echo -e "${GREEN}✓ Application files restored to $APP_DIR${NC}"
echo ""
# Restore NuGet packages
echo -e "${YELLOW}[5/8] Restoring NuGet packages...${NC}"
cd $APP_DIR
dotnet restore
echo -e "${GREEN}✓ NuGet packages restored${NC}"
echo ""
# Build application
echo -e "${YELLOW}[6/8] Building application...${NC}"
dotnet build -c Release
echo -e "${GREEN}✓ Application built${NC}"
echo ""
# Publish application
echo -e "${YELLOW}[7/8] Publishing application...${NC}"
dotnet publish SkyArtShop.csproj -c Release -o publish
echo -e "${GREEN}✓ Application published to $APP_DIR/publish${NC}"
echo ""
# Set permissions
echo -e "${YELLOW}[8/8] Setting permissions...${NC}"
chmod -R 755 $APP_DIR
chmod -R 777 $APP_DIR/wwwroot/uploads
echo -e "${GREEN}✓ Permissions set${NC}"
echo ""
# Test application
echo -e "${YELLOW}Testing application startup...${NC}"
cd $APP_DIR/publish
# Kill any existing instance
pkill -f "dotnet.*SkyArtShop.dll" 2>/dev/null || true
sleep 2
# Start application
nohup dotnet SkyArtShop.dll --urls "http://0.0.0.0:5001" > /tmp/skyartshop.log 2>&1 &
sleep 5
# Test if running
if curl -s http://localhost:5001 > /dev/null; then
echo -e "${GREEN}✓ Application is running on port 5001${NC}"
else
echo -e "${RED}✗ Application failed to start. Check logs:${NC}"
echo " tail -f /tmp/skyartshop.log"
fi
echo ""
# Display summary
echo "========================================="
echo " ✅ MIGRATION COMPLETE"
echo "========================================="
echo ""
echo "Application Details:"
echo " Location: $APP_DIR"
echo " Status: Running on http://localhost:5001"
echo " Database: SkyArtShopDB ($PRODUCT_COUNT products)"
echo ""
echo "Next Steps:"
echo "1. Test the application:"
echo -e " ${YELLOW}curl http://localhost:5001${NC}"
echo ""
echo "2. Configure Nginx (optional):"
echo -e " ${YELLOW}sudo nano /etc/nginx/sites-available/skyartshop${NC}"
echo " (See MIGRATION_PACKAGE.md for config)"
echo ""
echo "3. Create systemd service (optional):"
echo -e " ${YELLOW}sudo nano /etc/systemd/system/skyartshop.service${NC}"
echo " (See MIGRATION_PACKAGE.md for config)"
echo ""
echo "4. Check application logs:"
echo -e " ${YELLOW}tail -f /tmp/skyartshop.log${NC}"
echo ""
echo "Configuration files backed up in:"
echo " $BACKUP_DIR/configs/"
echo ""