thryyyyy's picture
restore latest backup files
ffd30b5
raw
history blame
3.99 kB
#!/usr/bin/env bash
# Ensure we're in the right directory and set up paths
SCRIPT_DIR=$(dirname "$(readlink -f "${BASH_SOURCE[0]}")")
cd "$SCRIPT_DIR" || exit 1
export DATA_DIR="/app/backend/data"
export PYTHONPATH="/app/backend:${PYTHONPATH}"
# Validate required environment variables
for var in "BACKUP_PASSPHRASE" "HF_TOKEN" "SPACE_ID"; do
if [ -z "${!var}" ]; then
echo "Error: $var is not set. Required for database backup/restore."
exit 1
fi
done
# Restore database from backup
echo "Restoring database from backup..."
python "$SCRIPT_DIR/scripts/restore.py"
restore_status=$?
if [ $restore_status -ne 0 ]; then
echo "Warning: Database restore failed. Starting with empty database."
fi
# Handle WebUI secret key
KEY_FILE="$SCRIPT_DIR/.webui_secret_key"
PORT="${PORT:-8080}"
HOST="${HOST:-0.0.0.0}"
if test "$WEBUI_SECRET_KEY $WEBUI_JWT_SECRET_KEY" = " "; then
if ! [ -e "$KEY_FILE" ]; then
head -c 12 /dev/random | base64 > "$KEY_FILE" || exit 1
fi
WEBUI_SECRET_KEY=$(cat "$KEY_FILE") || exit 1
fi
# Start Ollama if enabled
if [[ "${USE_OLLAMA_DOCKER,,}" == "true" ]]; then
ollama serve &
fi
# Configure CUDA if enabled
if [[ "${USE_CUDA_DOCKER,,}" == "true" ]]; then
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/lib/python3.11/site-packages/torch/lib:/usr/local/lib/python3.11/site-packages/nvidia/cudnn/lib"
fi
# Handle HuggingFace Space deployment
if [ -n "$SPACE_ID" ]; then
echo "Configuring HuggingFace Space deployment"
if [ -n "$ADMIN_USER_EMAIL" ] && [ -n "$ADMIN_USER_PASSWORD" ]; then
# Start webui temporarily to create admin user
WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY" uvicorn open_webui.main:app --host "$HOST" --port "$PORT" --forwarded-allow-ips '*' &
webui_pid=$!
# Wait for the server to be ready
while ! curl -s http://localhost:8080/health > /dev/null; do
echo "Waiting for WebUI to start..."
sleep 1
done
# Create the admin user
echo "Creating admin user: ${ADMIN_USER_EMAIL}"
curl \
-X POST "http://localhost:8080/api/v1/auths/signup" \
-H "accept: application/json" \
-H "Content-Type: application/json" \
-d "{ \"email\": \"${ADMIN_USER_EMAIL}\", \"password\": \"${ADMIN_USER_PASSWORD}\", \"name\": \"Admin\" }"
# Stop the temporary webui server
echo "Stopping temporary WebUI server..."
kill $webui_pid
wait $webui_pid 2>/dev/null # Wait for it to fully shut down
fi
# Set the public URL for Space deployments
export WEBUI_URL=${SPACE_HOST}
fi
# Start the main web server in the background
echo "Starting main WebUI server..."
WEBUI_SECRET_KEY="$WEBUI_SECRET_KEY" uvicorn open_webui.main:app \
--host "$HOST" --port "$PORT" --forwarded-allow-ips '*' \
--workers "${UVICORN_WORKERS:-1}" &
WEBUI_PID=$!
# Configure and start backup scheduler in the background
BACKUP_INITIAL_WAIT="${BACKUP_INITIAL_WAIT:-100}"
BACKUP_INTERVAL="${BACKUP_INTERVAL:-300}"
(
echo "Starting backup scheduler (Initial wait: ${BACKUP_INITIAL_WAIT}s, Interval: ${BACKUP_INTERVAL}s)"
sleep "$BACKUP_INITIAL_WAIT"
while true; do
echo "Running scheduled backup"
# Pass relevant env vars to backup script if needed
BACKUP_THRESHOLD_MINUTES="${BACKUP_THRESHOLD_MINUTES:-120}" \
python "$SCRIPT_DIR/scripts/backup.py"
if [ $? -ne 0 ]; then
echo "Warning: Backup failed"
fi
sleep "$BACKUP_INTERVAL"
done
) &
# Wait for the main web server process to exit
# This keeps the script alive while the server runs and allows graceful shutdown
echo "Web server started (PID: ${WEBUI_PID}). Waiting for it to exit..."
wait $WEBUI_PID
echo "Web server process ${WEBUI_PID} exited."
# Optional: Add cleanup here if needed after the web server stops.
# The background backup loop will likely be terminated when the main script exits.