Files
selfhost-cloud-compose-stack/scripts/backup/restore-odoo.sh
2025-11-25 12:27:53 +03:30

124 lines
3.7 KiB
Bash
Executable File

#!/bin/sh
set -eu
usage() {
cat <<EOF
Usage: $(basename "$0") BACKUP_ID [DB_NAME] [DB_USER]
Restore the standard Odoo volumes and the database from a backup run identified by BACKUP_ID.
BACKUP_ID should match the directory name under ./backups (e.g. 2025-11-17_12-30).
DB_NAME and DB_USER default to values from environment variables ODOO_DB / ODOO_DB_USER,
or fall back to "odoo" / "odoouser" if unset.
EOF
}
if [ "${1:-}" = "--help" ] || [ "${1:-}" = "-h" ]; then
usage
exit 0
fi
if [ $# -lt 1 ] || [ $# -gt 3 ]; then
usage >&2
exit 1
fi
BACKUP_ID=$1
ROOT_DIR=$(cd "$(dirname "$0")/../.." && pwd)
BACKUPS_DIR="$ROOT_DIR/backups/$BACKUP_ID"
load_env_file() {
local file=$1
if [ -f "$file" ]; then
# shellcheck disable=SC1090
set -a
. "$file"
set +a
fi
}
# Load credentials from .env files if present
load_env_file "$ROOT_DIR/.env"
DB_NAME=${ODOO_DB:-odoo}
DB_USER=${ODOO_DB_USER:-odoodbuser}
DB_PASSWORD=${ODOO_DB_PASSWORD:-}
DB_NAME=${2:-$DB_NAME}
DB_USER=${3:-$DB_USER}
if [ ! -d "$BACKUPS_DIR" ]; then
printf 'Backup directory not found: %s\n' "$BACKUPS_DIR" >&2
exit 1
fi
COMPOSE_CMD="docker compose"
cd "$ROOT_DIR"
printf 'Stopping Odoo service before restore...\n'
if ! $COMPOSE_CMD stop odoo >/dev/null 2>&1; then
printf 'Warning: could not stop Odoo service (it may already be stopped).\n' >&2
fi
restore_volume() {
volume=$1
archive_name=$2
host_file="$BACKUPS_DIR/$archive_name"
container_path="/backups/$BACKUP_ID/$archive_name"
if [ ! -f "$host_file" ]; then
printf 'Skipping %s: archive missing (%s)\n' "$volume" "$host_file" >&2
return 0
fi
printf 'Restoring %s from %s\n' "$volume" "$host_file"
$COMPOSE_CMD run --rm restore restore-volume "$volume" "$container_path"
}
restore_volume odoo-config "odoo_config_${BACKUP_ID}.tar.gz"
restore_volume odoo-db-data "odoo_db_data_${BACKUP_ID}.tar.gz"
DB_DUMP_DIR="$BACKUPS_DIR"
DB_DUMP_FILE="$DB_DUMP_DIR/${DB_NAME}_${BACKUP_ID}.sql"
DB_DUMP_GZ="$DB_DUMP_FILE.gz"
if [ -f "$DB_DUMP_GZ" ]; then
DB_SOURCE="$DB_DUMP_GZ"
elif [ -f "$DB_DUMP_FILE" ]; then
DB_SOURCE="$DB_DUMP_FILE"
else
DB_SOURCE=""
fi
if [ -n "$DB_SOURCE" ]; then
if [ -z "$DB_PASSWORD" ]; then
printf 'Database dump found (%s) but ODOO_DB_PASSWORD not set; skipping DB restore.\n' "$DB_SOURCE" >&2
else
printf 'Restoring database %s from %s\n' "$DB_NAME" "$DB_SOURCE"
DROP_FLAG=${ODOO_DROP_EXISTING_DB:-${DROP_EXISTING_DB:-1}}
RESTORE_ENV_ARGS="-e PGPASSWORD=$DB_PASSWORD -e DROP_EXISTING_DB=$DROP_FLAG"
if [ -n "${POSTGRES_ADMIN_USER:-}" ] && [ -n "${POSTGRES_ADMIN_PASSWORD:-}" ]; then
RESTORE_ENV_ARGS="$RESTORE_ENV_ARGS -e POSTGRES_ADMIN_USER=$POSTGRES_ADMIN_USER -e POSTGRES_ADMIN_PASSWORD=$POSTGRES_ADMIN_PASSWORD"
fi
if [ -n "${POSTGRES_ADMIN_DB:-}" ]; then
RESTORE_ENV_ARGS="$RESTORE_ENV_ARGS -e POSTGRES_ADMIN_DB=$POSTGRES_ADMIN_DB"
fi
if [ -n "${ODOO_DB_HOST:-}" ]; then
RESTORE_ENV_ARGS="$RESTORE_ENV_ARGS -e POSTGRES_HOST=$ODOO_DB_HOST"
elif [ -n "${POSTGRES_HOST:-}" ]; then
RESTORE_ENV_ARGS="$RESTORE_ENV_ARGS -e POSTGRES_HOST=$POSTGRES_HOST"
fi
# shellcheck disable=SC2086
$COMPOSE_CMD run --rm $RESTORE_ENV_ARGS restore \
restore-db "/backups/$BACKUP_ID/$(basename "$DB_SOURCE")" "$DB_NAME" "$DB_USER" "$DB_PASSWORD"
fi
else
printf 'No database dump found for %s in %s\n' "$DB_NAME" "$DB_DUMP_DIR" >&2
fi
printf '\nRestore commands completed. Restart the dependent services when ready.\n'
printf 'Starting Odoo service...\n'
if ! $COMPOSE_CMD up -d odoo; then
printf 'Warning: failed to start Odoo service. Please start it manually.\n' >&2
fi