Files
digital-garage-sale/update.sh
2025-04-29 14:13:26 -07:00

91 lines
2.5 KiB
Bash
Executable File

#!/bin/bash
# Update script for Hex's Garage Sale Catalog
# This script updates an existing deployment with the latest code
set -e # Exit immediately if a command exits with a non-zero status
# Color codes for better readability
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color
# Function to print colored messages
print_message() {
echo -e "${GREEN}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
# Check if running as root
if [ "$(id -u)" != "0" ]; then
print_error "This script must be run as root"
exit 1
fi
# Get deployment configuration
APP_DIR=${1:-/var/www/garagesale}
BACKUP_DIR="/var/backups/garagesale/$(date +%Y%m%d_%H%M%S)"
print_message "Beginning update process..."
# Create backup directory
mkdir -p "$BACKUP_DIR"
# Backup the current database and uploads
print_message "Creating backup..."
if [ -f "$APP_DIR/garage_sale.db" ]; then
cp "$APP_DIR/garage_sale.db" "$BACKUP_DIR/"
print_message "SQLite database backed up to $BACKUP_DIR/garage_sale.db"
fi
# Backup uploads directory
if [ -d "$APP_DIR/app/static/uploads" ]; then
cp -r "$APP_DIR/app/static/uploads" "$BACKUP_DIR/"
print_message "Uploads directory backed up to $BACKUP_DIR/uploads"
fi
# Copy environment file
if [ -f "$APP_DIR/.env" ]; then
cp "$APP_DIR/.env" "$BACKUP_DIR/"
print_message "Environment file backed up to $BACKUP_DIR/.env"
fi
# Copy new code
print_message "Updating application code..."
rsync -av --exclude 'venv' --exclude '.env' --exclude 'garage_sale.db' \
--exclude 'app/static/uploads' "$(dirname "$0")/" "$APP_DIR/"
# Set correct ownership and permissions
print_message "Setting correct permissions..."
chown -R garagesale:www-data "$APP_DIR"
chmod -R 755 "$APP_DIR"
chmod -R 775 "$APP_DIR/app/static/uploads"
# Install or update dependencies
print_message "Updating Python dependencies..."
cd "$APP_DIR"
su -c "source venv/bin/activate && pip install -r requirements.txt" garagesale
# Run database migrations if needed
print_message "Running database updates..."
cd "$APP_DIR"
su -c "source venv/bin/activate && python update_database.py" garagesale
# Restart services
print_message "Restarting services..."
systemctl restart garagesale
systemctl restart nginx
print_message "Update completed successfully!"
print_message "Backup created at: $BACKUP_DIR"
print_message "If you encounter any issues, you can restore from the backup."
exit 0