chabge database dump to use docker exec

This commit is contained in:
2026-10-23 16:36:46 +03:00
parent 8cb32b799f
commit 26ff030a44
3 changed files with 264 additions and 108 deletions
+131 -49
View File
@@ -188,30 +188,58 @@ validate_database() {
fi
if [[ "${DB_TYPE}" == "postgresql" ]]; then
if ! command -v pg_dump >/dev/null 2>&1; then
die "pg_dump not found. Install postgresql-client."
fi
export PGPASSWORD="${DB_PASS}"
if ! pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" --no-owner --no-privileges --clean --if-exists >/dev/null 2>&1; then
if command -v pg_dump >/dev/null 2>&1; then
export PGPASSWORD="${DB_PASS}"
if ! pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" --no-owner --no-privileges --clean --if-exists >/dev/null 2>&1; then
unset PGPASSWORD
die "Cannot connect to PostgreSQL at ${DB_HOST}:${DB_PORT} as ${DB_USER}. Check credentials and ensure database is accessible from host."
fi
unset PGPASSWORD
die "Cannot connect to PostgreSQL at ${DB_HOST}:${DB_PORT} as ${DB_USER}. Check credentials and ensure database is accessible from host."
else
# No host tools, try docker exec
info "Host pg_dump not found, trying docker exec..."
if ! command -v docker >/dev/null 2>&1; then
die "Neither pg_dump nor docker found. Cannot dump database."
fi
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(postgres|nextcloud.*db)' | head -1 || true)
if [[ -z "${db_container}" ]]; then
die "No PostgreSQL container found. Cannot dump database."
fi
info "Using database container: ${db_container}"
if ! docker exec -e PGPASSWORD="${DB_PASS}" "${db_container}" pg_isready -U "${DB_USER}" -d "${DB_NAME}" >/dev/null 2>&1; then
die "Cannot connect to PostgreSQL in container ${db_container}. Check credentials."
fi
fi
unset PGPASSWORD
else
local dump_cmd=""
if command -v mariadb-dump >/dev/null 2>&1; then
dump_cmd="mariadb-dump"
elif command -v mysqldump >/dev/null 2>&1; then
dump_cmd="mysqldump"
else
die "Neither mariadb-dump nor mysqldump found. Install mariadb-client or mysql-client."
fi
export MYSQL_PWD="${DB_PASS}"
if ! ${dump_cmd} -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -e "SELECT 1" >/dev/null 2>&1; then
if [[ -n "${dump_cmd}" ]]; then
export MYSQL_PWD="${DB_PASS}"
if ! ${dump_cmd} -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -e "SELECT 1" >/dev/null 2>&1; then
unset MYSQL_PWD
die "Cannot connect to MariaDB/MySQL at ${DB_HOST}:${DB_PORT} as ${DB_USER}. Check credentials."
fi
unset MYSQL_PWD
die "Cannot connect to MariaDB/MySQL at ${DB_HOST}:${DB_PORT} as ${DB_USER}. Check credentials."
else
# No host tools, try docker exec
info "Host mariadb-dump/mysqldump not found, trying docker exec..."
if ! command -v docker >/dev/null 2>&1; then
die "Neither mariadb-dump/mysqldump nor docker found. Cannot dump database."
fi
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|nextcloud.*db)' | head -1 || true)
if [[ -z "${db_container}" ]]; then
die "No MariaDB/MySQL container found. Cannot dump database."
fi
info "Using database container: ${db_container}"
if ! docker exec "${db_container}" mariadb -u "${DB_USER}" -p"${DB_PASS}" -e "SELECT 1" >/dev/null 2>&1; then
die "Cannot connect to MariaDB/MySQL in container ${db_container}. Check credentials."
fi
fi
unset MYSQL_PWD
fi
success "Database connection validated"
}
@@ -310,47 +338,85 @@ backup_database() {
mkdir -p "${BACKUP_DIR}/database"
if [[ "${DB_TYPE}" == "postgresql" ]]; then
info "Using pg_dump for database dump"
export PGPASSWORD="${DB_PASS}"
if ! pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" \
--no-owner --no-privileges --clean --if-exists \
> "${db_dump}" 2>>"${LOG_FILE}"; then
if command -v pg_dump >/dev/null 2>&1; then
info "Using pg_dump for database dump"
export PGPASSWORD="${DB_PASS}"
if ! pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" \
--no-owner --no-privileges --clean --if-exists \
> "${db_dump}" 2>>"${LOG_FILE}"; then
unset PGPASSWORD
die "PostgreSQL dump failed"
fi
unset PGPASSWORD
die "PostgreSQL dump failed"
else
# Use docker exec
info "Host pg_dump not found, using docker exec..."
if ! command -v docker >/dev/null 2>&1; then
die "Neither pg_dump nor docker found. Cannot dump database."
fi
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(postgres|nextcloud.*db)' | head -1 || true)
if [[ -z "${db_container}" ]]; then
die "No PostgreSQL container found. Cannot dump database."
fi
info "Using docker exec on container: ${db_container}"
info "Dumping database ${DB_NAME}..."
if ! docker exec -e PGPASSWORD="${DB_PASS}" "${db_container}" pg_dump -U "${DB_USER}" -d "${DB_NAME}" \
--no-owner --no-privileges --clean --if-exists \
> "${db_dump}" 2>>"${LOG_FILE}"; then
die "PostgreSQL dump failed via docker exec."
fi
fi
unset PGPASSWORD
else
local dump_cmd=""
if command -v mariadb-dump >/dev/null 2>&1; then
dump_cmd="mariadb-dump"
elif command -v mysqldump >/dev/null 2>&1; then
dump_cmd="mysqldump"
else
die "Neither mariadb-dump nor mysqldump found. Install mariadb-client or mysql-client."
fi
info "Using ${dump_cmd} for database dump"
local conn_params=(
"-h" "${DB_HOST}"
"-P" "${DB_PORT}"
"-u" "${DB_USER}"
"--single-transaction"
"--routines"
"--triggers"
"--events"
"--hex-blob"
"--complete-insert"
"--extended-insert"
"--quick"
"--lock-tables=false"
)
export MYSQL_PWD="${DB_PASS}"
if ! ${dump_cmd} "${conn_params[@]}" "${DB_NAME}" > "${db_dump}" 2>>"${LOG_FILE}"; then
if [[ -n "${dump_cmd}" ]]; then
info "Using ${dump_cmd} for database dump"
local conn_params=(
"-h" "${DB_HOST}"
"-P" "${DB_PORT}"
"-u" "${DB_USER}"
"--single-transaction"
"--routines"
"--triggers"
"--events"
"--hex-blob"
"--complete-insert"
"--extended-insert"
"--quick"
"--lock-tables=false"
)
export MYSQL_PWD="${DB_PASS}"
if ! ${dump_cmd} "${conn_params[@]}" "${DB_NAME}" > "${db_dump}" 2>>"${LOG_FILE}"; then
unset MYSQL_PWD
die "MariaDB/MySQL dump failed"
fi
unset MYSQL_PWD
die "MariaDB/MySQL dump failed"
else
# Use docker exec
info "Host mariadb-dump/mysqldump not found, using docker exec..."
if ! command -v docker >/dev/null 2>&1; then
die "Neither mariadb-dump/mysqldump nor docker found. Cannot dump database."
fi
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|nextcloud.*db)' | head -1 || true)
if [[ -z "${db_container}" ]]; then
die "No MariaDB/MySQL container found. Cannot dump database."
fi
info "Using docker exec on container: ${db_container}"
info "Dumping database ${DB_NAME}..."
if ! docker exec "${db_container}" mariadb-dump -u "${DB_USER}" -p"${DB_PASS}" \
--single-transaction --routines --triggers --events --hex-blob \
--complete-insert --extended-insert --quick --lock-tables=false \
"${DB_NAME}" > "${db_dump}" 2>>"${LOG_FILE}"; then
die "MariaDB/MySQL dump failed via docker exec."
fi
fi
unset MYSQL_PWD
fi
info "Compressing database dump..."
@@ -362,13 +428,29 @@ backup_database() {
# Schema-only dump
if [[ "${DB_TYPE}" == "postgresql" ]]; then
export PGPASSWORD="${DB_PASS}"
pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" --no-owner --no-privileges --clean --if-exists --schema-only | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
unset PGPASSWORD
if command -v pg_dump >/dev/null 2>&1; then
export PGPASSWORD="${DB_PASS}"
pg_dump -h "${DB_HOST}" -p "${DB_PORT}" -U "${DB_USER}" -d "${DB_NAME}" --no-owner --no-privileges --clean --if-exists --schema-only | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
unset PGPASSWORD
else
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(postgres|nextcloud.*db)' | head -1 || true)
if [[ -n "${db_container}" ]]; then
docker exec -e PGPASSWORD="${DB_PASS}" "${db_container}" pg_dump -U "${DB_USER}" -d "${DB_NAME}" --no-owner --no-privileges --clean --if-exists --schema-only | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
fi
fi
else
export MYSQL_PWD="${DB_PASS}"
${dump_cmd} "${conn_params[@]}" --no-data "${DB_NAME}" | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
unset MYSQL_PWD
if [[ -n "${dump_cmd}" ]]; then
export MYSQL_PWD="${DB_PASS}"
${dump_cmd} "${conn_params[@]}" --no-data "${DB_NAME}" | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
unset MYSQL_PWD
else
local db_container=$(docker ps --format '{{.Names}}' | grep -iE '(mariadb|mysql|nextcloud.*db)' | head -1 || true)
if [[ -n "${db_container}" ]]; then
docker exec "${db_container}" mariadb-dump -u "${DB_USER}" -p"${DB_PASS}" \
--no-data --single-transaction --routines --triggers --events \
"${DB_NAME}" | gzip -${COMPRESSION_LEVEL} > "${BACKUP_DIR}/database/nextcloud_schema_${TIMESTAMP}.sql.gz"
fi
fi
fi
}