diff --git a/scripts/db/backup-client/docker-compose.yml b/scripts/db/backup-client/docker-compose.yml new file mode 100644 index 0000000..eceb0d9 --- /dev/null +++ b/scripts/db/backup-client/docker-compose.yml @@ -0,0 +1,17 @@ +services: + backup: + image: postgres:17.7-alpine + environment: + PGHOST: ${DB_HOST} + PGPORT: ${DB_PORT} + PGDATABASE: ${DB_NAME} + PGUSER: ${DB_USER} + PGPASSWORD: ${DB_PASSWORD} + entrypoint: ["pg_dump"] + command: + - "--format=custom" + - "--no-owner" + - "--no-privileges" + - "--file=/backup/current.dump" + volumes: + - ${BACKUP_DIR}:/backup diff --git a/scripts/db/synology_db_backup.sh b/scripts/db/synology_db_backup.sh index e2ad73f..914010c 100755 --- a/scripts/db/synology_db_backup.sh +++ b/scripts/db/synology_db_backup.sh @@ -9,6 +9,8 @@ BACKUP_DIR="${BACKUP_DIR:-/volume1/naurua_db_backups/dev}" FILE_PREFIX="${FILE_PREFIX:-erpnaurua_dev}" PG_DUMP_BIN="${PG_DUMP_BIN:-$(command -v pg_dump || true)}" PSQL_BIN="${PSQL_BIN:-$(command -v psql || true)}" +DOCKER_BIN="${DOCKER_BIN:-$(command -v docker || true)}" +COMPOSE_FILE="${COMPOSE_FILE:-${ROOT_DIR}/scripts/db/backup-client/docker-compose.yml}" if [[ ! -f "${ENV_FILE}" ]]; then echo "Missing environment file: ${ENV_FILE}" >&2 @@ -27,22 +29,12 @@ for required_var in DB_HOST DB_PORT DB_NAME DB_USER DB_PASSWORD; do fi done -if [[ -z "${PG_DUMP_BIN}" || ! -x "${PG_DUMP_BIN}" ]]; then - echo "Compatible pg_dump is required. Set PG_DUMP_BIN to a PostgreSQL client binary." >&2 +if [[ -z "${DOCKER_BIN}" || ! -x "${DOCKER_BIN}" ]]; then + echo "Docker is required for the PostgreSQL client container." >&2 exit 1 fi -if [[ -z "${PSQL_BIN}" || ! -x "${PSQL_BIN}" ]]; then - echo "psql is required." >&2 - exit 1 -fi - -export PGPASSWORD="${DB_PASSWORD}" -server_version_num="$("${PSQL_BIN}" -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" -X -Atqc 'SHOW server_version_num')" -client_version="$("${PG_DUMP_BIN}" --version | sed -E 's/.*PostgreSQL\)?[[:space:]]+([0-9]+).*/\1/')" -server_major="${server_version_num:0:${#server_version_num}-4}" - -if [[ -z "${server_major}" || -z "${client_version}" || "${server_major}" != "${client_version}" ]]; then - echo "pg_dump major version mismatch: server=${server_major:-unknown}, client=${client_version:-unknown}" >&2 +if [[ ! -f "${COMPOSE_FILE}" ]]; then + echo "Missing backup compose file: ${COMPOSE_FILE}" >&2 exit 1 fi @@ -54,8 +46,15 @@ temporary_file="${output_file}.tmp" cleanup() { rm -f "${temporary_file}"; } trap cleanup EXIT -"${PG_DUMP_BIN}" -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" \ - --format=custom --no-owner --no-privileges > "${temporary_file}" +export BACKUP_DIR +"${DOCKER_BIN}" compose --file "${COMPOSE_FILE}" run --rm --no-deps -T backup + +if [[ ! -s "${BACKUP_DIR}/current.dump" ]]; then + echo "Backup failed: container dump file is empty." >&2 + exit 1 +fi + +mv -f "${BACKUP_DIR}/current.dump" "${temporary_file}" if [[ ! -s "${temporary_file}" ]]; then echo "Backup failed: dump file is empty." >&2