deploy
This commit is contained in:
95
scripts/backup.sh
Executable file
95
scripts/backup.sh
Executable file
@@ -0,0 +1,95 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Backup script for Observation App
|
||||
# This script creates backups of database and images
|
||||
|
||||
set -e
|
||||
|
||||
echo "💾 Creating backup of Observation App data..."
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
print_status() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
# Create backup directory with timestamp
|
||||
BACKUP_DIR="backups/$(date +%Y%m%d_%H%M%S)"
|
||||
mkdir -p "$BACKUP_DIR"
|
||||
|
||||
print_status "Backup directory: $BACKUP_DIR"
|
||||
|
||||
# Backup database
|
||||
print_status "Backing up MongoDB database..."
|
||||
docker-compose exec -T observation_db mongodump --uri="mongodb://admin:password123@localhost:27017/beanstalk?authSource=admin" --archive > "$BACKUP_DIR/mongodb_backup.archive"
|
||||
|
||||
# Backup images from public/observations
|
||||
if [ -d "public/observations" ]; then
|
||||
print_status "Backing up images..."
|
||||
cp -r public/observations "$BACKUP_DIR/"
|
||||
print_success "Images backed up"
|
||||
else
|
||||
print_warning "No images directory found"
|
||||
fi
|
||||
|
||||
# Backup configuration files
|
||||
print_status "Backing up configuration..."
|
||||
cp .env "$BACKUP_DIR/" 2>/dev/null || print_warning "No .env file found"
|
||||
cp docker-compose.yml "$BACKUP_DIR/" 2>/dev/null || true
|
||||
|
||||
# Create restore script
|
||||
cat > "$BACKUP_DIR/restore.sh" << 'EOF'
|
||||
#!/bin/bash
|
||||
|
||||
# Restore script for Observation App backup
|
||||
set -e
|
||||
|
||||
echo "🔄 Restoring Observation App from backup..."
|
||||
|
||||
# Colors for output
|
||||
GREEN='\033[0;32m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
print_status() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
# Check if containers are running
|
||||
if ! docker-compose ps | grep -q "Up"; then
|
||||
echo "Please start the application first: docker-compose up -d"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Restore database
|
||||
print_status "Restoring database..."
|
||||
docker-compose exec -T observation_db mongorestore --uri="mongodb://admin:password123@localhost:27017/beanstalk?authSource=admin" --archive < mongodb_backup.archive
|
||||
|
||||
# Restore images
|
||||
if [ -d "observations" ]; then
|
||||
print_status "Restoring images..."
|
||||
cp -r observations/* public/observations/
|
||||
print_success "Images restored"
|
||||
fi
|
||||
|
||||
print_success "Restore completed!"
|
||||
EOF
|
||||
|
||||
chmod +x "$BACKUP_DIR/restore.sh"
|
||||
|
||||
print_success "Backup completed successfully!"
|
||||
echo "Backup location: $BACKUP_DIR"
|
||||
echo "To restore: cd $BACKUP_DIR && ./restore.sh"
|
||||
52
scripts/cleanup.sh
Executable file
52
scripts/cleanup.sh
Executable file
@@ -0,0 +1,52 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Cleanup script for Observation App
|
||||
# This script removes all containers, images, and volumes
|
||||
|
||||
set -e
|
||||
|
||||
echo "🧹 Cleaning up Observation App deployment..."
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m'
|
||||
|
||||
print_status() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
# Stop and remove containers
|
||||
print_status "Stopping and removing containers..."
|
||||
docker-compose down -v 2>/dev/null || true
|
||||
|
||||
# Remove images
|
||||
print_status "Removing application images..."
|
||||
docker rmi observation_import-observation_app 2>/dev/null || true
|
||||
|
||||
# Remove unused networks
|
||||
print_status "Removing unused networks..."
|
||||
docker network prune -f 2>/dev/null || true
|
||||
|
||||
# Remove unused volumes (prompt user)
|
||||
read -p "Remove all unused Docker volumes? This will delete database data! (y/N): " -n 1 -r
|
||||
echo
|
||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||
print_status "Removing unused volumes..."
|
||||
docker volume prune -f
|
||||
print_warning "Database data has been removed!"
|
||||
else
|
||||
print_status "Keeping Docker volumes (database data preserved)"
|
||||
fi
|
||||
|
||||
print_success "Cleanup completed!"
|
||||
130
scripts/deploy.sh
Executable file
130
scripts/deploy.sh
Executable file
@@ -0,0 +1,130 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Deployment script for Observation App
|
||||
# This script handles the complete deployment process
|
||||
|
||||
set -e # Exit on any error
|
||||
|
||||
echo "🚀 Starting Observation App Deployment..."
|
||||
|
||||
# Colors for output
|
||||
RED='\033[0;31m'
|
||||
GREEN='\033[0;32m'
|
||||
YELLOW='\033[1;33m'
|
||||
BLUE='\033[0;34m'
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
# Function to print colored output
|
||||
print_status() {
|
||||
echo -e "${BLUE}[INFO]${NC} $1"
|
||||
}
|
||||
|
||||
print_success() {
|
||||
echo -e "${GREEN}[SUCCESS]${NC} $1"
|
||||
}
|
||||
|
||||
print_warning() {
|
||||
echo -e "${YELLOW}[WARNING]${NC} $1"
|
||||
}
|
||||
|
||||
print_error() {
|
||||
echo -e "${RED}[ERROR]${NC} $1"
|
||||
}
|
||||
|
||||
# Check if .env file exists
|
||||
if [ ! -f .env ]; then
|
||||
print_error ".env file not found!"
|
||||
echo "Please create .env file before deploying."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if public directory exists
|
||||
if [ ! -d public ]; then
|
||||
print_error "public directory not found!"
|
||||
echo "Please ensure public directory exists with required files."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Check if public/db directory exists
|
||||
if [ ! -d public/db ]; then
|
||||
print_warning "public/db directory not found. Creating empty directory..."
|
||||
mkdir -p public/db
|
||||
fi
|
||||
|
||||
# Stop existing containers if running
|
||||
print_status "Stopping existing containers..."
|
||||
docker-compose down 2>/dev/null || true
|
||||
|
||||
# Build and start containers
|
||||
print_status "Building and starting containers..."
|
||||
docker-compose up --build -d
|
||||
|
||||
# Wait for database to be ready
|
||||
print_status "Waiting for database to be ready..."
|
||||
sleep 10
|
||||
|
||||
# Check if database is accessible
|
||||
print_status "Checking database connection..."
|
||||
docker-compose exec observation_db mongosh --eval "db.adminCommand('ping')" > /dev/null 2>&1
|
||||
if [ $? -eq 0 ]; then
|
||||
print_success "Database is ready!"
|
||||
else
|
||||
print_error "Database connection failed!"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Import database if files exist in public/db
|
||||
if [ "$(ls -A public/db/*.json 2>/dev/null)" ]; then
|
||||
print_status "Found database files in public/db. Starting import..."
|
||||
|
||||
# Import each JSON file
|
||||
for file in public/db/*.json; do
|
||||
filename=$(basename "$file" .json)
|
||||
print_status "Importing $filename..."
|
||||
|
||||
# Copy file to container and import
|
||||
docker cp "$file" observation_app:/tmp/"$(basename "$file")"
|
||||
docker-compose exec observation_app curl -X POST -F "file=@/tmp/$(basename "$file")" http://localhost:3000/api/import-json
|
||||
|
||||
# Clean up
|
||||
docker-compose exec observation_app rm "/tmp/$(basename "$file")"
|
||||
|
||||
print_success "Imported $filename"
|
||||
done
|
||||
else
|
||||
print_warning "No database files found in public/db"
|
||||
fi
|
||||
|
||||
# Check application health
|
||||
print_status "Checking application health..."
|
||||
sleep 5
|
||||
if curl -f http://localhost:3000/api/health > /dev/null 2>&1; then
|
||||
print_success "Application is healthy!"
|
||||
else
|
||||
print_warning "Application health check failed, but deployment may still be successful"
|
||||
fi
|
||||
|
||||
# Display deployment information
|
||||
echo ""
|
||||
print_success "🎉 Deployment completed successfully!"
|
||||
echo ""
|
||||
echo "📋 Service Information:"
|
||||
echo " • Application URL: http://localhost:3000"
|
||||
echo " • Database URL: mongodb://admin:password123@localhost:27017"
|
||||
echo " • Database Name: beanstalk"
|
||||
echo ""
|
||||
echo "🔧 Useful Commands:"
|
||||
echo " • View logs: docker-compose logs -f"
|
||||
echo " • Stop services: docker-compose down"
|
||||
echo " • Restart services: docker-compose restart"
|
||||
echo " • Access database: docker-compose exec observation_db mongosh"
|
||||
echo ""
|
||||
echo "📁 Data Locations:"
|
||||
echo " • Images: ./public/observations/ (copied to container)"
|
||||
echo " • Database files: ./public/db/ (imported to MongoDB)"
|
||||
echo " • MongoDB data: Docker volume 'mongo_data'"
|
||||
echo ""
|
||||
|
||||
# Show container status
|
||||
print_status "Container Status:"
|
||||
docker-compose ps
|
||||
18
scripts/init-mongo.js
Normal file
18
scripts/init-mongo.js
Normal file
@@ -0,0 +1,18 @@
|
||||
// MongoDB initialization script
|
||||
// This script runs when the container starts for the first time
|
||||
|
||||
db = db.getSiblingDB('beanstalk');
|
||||
|
||||
// Create application user
|
||||
db.createUser({
|
||||
user: 'app_user',
|
||||
pwd: 'app_password',
|
||||
roles: [
|
||||
{
|
||||
role: 'readWrite',
|
||||
db: 'beanstalk'
|
||||
}
|
||||
]
|
||||
});
|
||||
|
||||
print('Database initialization completed');
|
||||
Reference in New Issue
Block a user