# OpenWebUI Configuration
OPENWEBUI_DOMAIN=http://127.0.0.1:8080
OPENWEBUI_API_KEY=sk-dc2c745c8cd94faea80a5f3d9b348086 # OpenWebUI API key for fetching model list
# Access Control
ACCESS_TOKEN=STshentong # Used for Monitor page login
API_KEY=STshentong0722 # Used for authentication when sending requests to Monitor
# Price Configuration (Optional, $/million tokens)
INIT_BALANCE=5 # Initial balance for users, optional
# PostgreSQL Database Configuration (Optional, configure these if using external database)
# POSTGRES_HOST=172.21.0.2
# POSTGRES_PORT=5432
# POSTGRES_USER=postgres
# POSTGRES_PASSWORD=openwebui
# POSTGRES_DATABASE=openwebui_monitor
#!/bin/bash
set -e # If any command fails, immediately exit the script
# --- Configuration (Specific to this target server) ---
POSTGRESQL_DBNAME="openwebui"
POSTGRESQL_USERNAME="st"
# !!! Ensure this password is correct for the 'st' DATABASE user !!!
POSTGRESQL_PASSWORD="STshentong"
# --- Connection Info (Running locally) ---
TARGET_HOST="127.0.0.1"
# !!! Port for the PostgreSQL instance on THIS server !!!
TARGET_PORT="5432"
# --- Backup File Path (Located on THIS server) ---
# !!! IMPORTANT: Update this path to your actual backup file !!!
# Example: BACKUP_FILE_PATH="/data/backup/nochat/openwebui_exclude_chat_file_20231027_100000.dump"
# You might want to make this an argument to the script, e.g., BACKUP_FILE_PATH="$1"
BACKUP_FILE_PATH="/data/openwebui_exclude_chat_file_credit_credit_log_20250623_103946.dump" # <<<--- !!! UPDATE THIS LINE !!!
# --- pg_restore Configuration ---
# Adjusted for an 8-core server, you can tweak this
PG_RESTORE_JOBS=4
# --- Restore Logic ---
echo "--- Starting local restore process on ${TARGET_HOST}:${TARGET_PORT} ---"
echo "Restoring database: ${POSTGRESQL_DBNAME}"
echo "Running script as OS user: $(whoami)"
echo "Connecting to DB as PostgreSQL user: ${POSTGRESQL_USERNAME}"
echo "Using backup file: ${BACKUP_FILE_PATH}"
# Check if backup file exists
if [ ! -f "${BACKUP_FILE_PATH}" ]; then
echo "ERROR: Backup file not found at ${BACKUP_FILE_PATH}"
echo "Please ensure the BACKUP_FILE_PATH variable is set correctly in the script."
exit 1
fi
# --- Attempt to terminate existing connections ---
# This is crucial to avoid "database is being accessed by other users" errors during restore,
# especially when --clean is used.
echo "[${TARGET_HOST}:${TARGET_PORT}] Attempting to terminate existing connections to database ${POSTGRESQL_DBNAME}..."
TERMINATE_SQL="SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${POSTGRESQL_DBNAME}' AND pid <> pg_backend_pid();"
export PGPASSWORD="${POSTGRESQL_PASSWORD}"
# Connect to 'postgres' or another maintenance DB to terminate connections to the target DB
if ! psql \
--host "${TARGET_HOST}" \
--port "${TARGET_PORT}" \
--username "${POSTGRESQL_USERNAME}" \
--dbname "postgres" \
-tAc "${TERMINATE_SQL}" > /dev/null; then
echo "[${TARGET_HOST}:${TARGET_PORT}] WARNING: Failed to execute terminate command. This might be due to:"
echo " - No active connections to '${POSTGRESQL_DBNAME}'."
echo " - Insufficient database permissions for user '${POSTGRESQL_USERNAME}' to terminate backends."
echo " - Failure to connect to the 'postgres' database."
echo "Continuing with restore, but it might fail if connections persist."
else
echo "[${TARGET_HOST}:${TARGET_PORT}] Terminate command sent successfully. Waiting a few seconds for connections to close..."
sleep 5 # Give some time for connections to actually terminate
fi
unset PGPASSWORD
# --- IMPORTANT MODIFICATION: ---
# We DO NOT drop or create the database here.
# We are restoring into an EXISTING database and want to preserve tables
# ('chat', 'file') that are NOT in the backup.
# The target database POSTGRESQL_DBNAME must already exist.
# If it doesn't exist, pg_restore will fail. You would need to create it manually
# or add a 'createdb --if-not-exists' equivalent logic if that's a desired scenario.
echo "[${TARGET_HOST}:${TARGET_PORT}] Ensuring database '${POSTGRESQL_DBNAME}' exists (not creating or dropping it)."
echo "The restore will proceed into the existing '${POSTGRESQL_DBNAME}' database."
echo "Tables 'chat' and 'file' (if they exist) will NOT be touched by this restore."
echo "Other tables present in the backup file will be dropped (if they exist) and then recreated."
# --- Execute Restore ---
echo "[${TARGET_HOST}:${TARGET_PORT}] Starting database restore from local file using ${PG_RESTORE_JOBS} parallel jobs..."
echo "Using --clean option to drop and recreate objects FROM THE BACKUP FILE."
export PGPASSWORD="${POSTGRESQL_PASSWORD}"
# Using 'time' to measure duration of the restore operation
time pg_restore \
--host "${TARGET_HOST}" \
--port "${TARGET_PORT}" \
--username "${POSTGRESQL_USERNAME}" \
--dbname "${POSTGRESQL_DBNAME}" \
--jobs "${PG_RESTORE_JOBS}" \
--clean \
--if-exists \
--verbose \
"${BACKUP_FILE_PATH}"
PGRESTORE_EXIT_CODE=$?
unset PGPASSWORD
if [ ${PGRESTORE_EXIT_CODE} -eq 0 ]; then
echo "[${TARGET_HOST}:${TARGET_PORT}] Database restore completed successfully!"
echo "Tables from backup were restored. Tables 'chat' and 'file' were not affected by the restore process."
else
echo "[${TARGET_HOST}:${TARGET_PORT}] ERROR: Database restore failed! (pg_restore exit code: ${PGRESTORE_EXIT_CODE})"
exit 1
fi
echo "--- Restore script execution finished ---"
exit 0