Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions lib/pasarguard-backup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1535,7 +1535,7 @@ backup_command() {
fi
done <<<"$mariadb_databases"

if [ "$configured_db_found" = true ] && docker exec -e MYSQL_PWD="$MYSQL_ROOT_PASSWORD" "$container_name" mariadb-dump -u root --all-databases --ignore-database=mysql --ignore-database=performance_schema --ignore-database=information_schema --ignore-database=sys --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
if [ "$configured_db_found" = true ] && docker exec -e MYSQL_PWD="$MYSQL_ROOT_PASSWORD" "$container_name" mariadb-dump -u root --all-databases --ignore-database=mysql --ignore-database=performance_schema --ignore-database=information_schema --ignore-database=sys --single-transaction --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
colorized_echo green "MariaDB backup completed successfully (all databases)"
else
# Fallback to SQL URL credentials for specific database
Expand All @@ -1548,7 +1548,7 @@ backup_command() {
error_messages+=("MariaDB backup failed - root backup failed and fallback credentials incomplete.")
else
colorized_echo blue "Backing up MariaDB database '$db_name' from container: $container_name (using app user)"
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" mariadb-dump -u "$backup_user" "$db_name" --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" mariadb-dump -u "$backup_user" "$db_name" --single-transaction --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
colorized_echo red "MariaDB dump failed. Check log file for details."
error_messages+=("MariaDB dump failed.")
else
Expand All @@ -1569,7 +1569,7 @@ backup_command() {
error_messages+=("MariaDB database name not found.")
else
colorized_echo blue "Backing up MariaDB database '$db_name' from container: $container_name (using app user)"
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" mariadb-dump -u "$backup_user" "$db_name" --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" mariadb-dump -u "$backup_user" "$db_name" --single-transaction --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
colorized_echo red "MariaDB dump failed. Check log file for details."
error_messages+=("MariaDB dump failed.")
else
Expand Down Expand Up @@ -1643,14 +1643,14 @@ backup_command() {
error_messages+=("MySQL backup failed - no databases found and fallback credentials incomplete.")
else
colorized_echo blue "Backing up $db_type_name database '$db_name' from container: $container_name (using app user)"
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" "$dump_cmd" -u "$backup_user" "$db_name" --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" "$dump_cmd" -u "$backup_user" "$db_name" --single-transaction --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
colorized_echo red "$db_type_name dump failed. Check log file for details."
error_messages+=("$db_type_name dump failed.")
else
colorized_echo green "$db_type_name backup completed successfully"
fi
fi
elif ! docker exec -e MYSQL_PWD="$MYSQL_ROOT_PASSWORD" "$container_name" "$dump_cmd" -u root --databases "${database_list[@]}" --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
elif ! docker exec -e MYSQL_PWD="$MYSQL_ROOT_PASSWORD" "$container_name" "$dump_cmd" -u root --databases "${database_list[@]}" --single-transaction --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
# Root backup failed, fallback to SQL URL credentials
colorized_echo yellow "Root backup failed, falling back to app user for specific database"
local backup_user="${db_user:-${DB_USER:-}}"
Expand All @@ -1661,7 +1661,7 @@ backup_command() {
error_messages+=("MySQL backup failed - root backup failed and fallback credentials incomplete.")
else
colorized_echo blue "Backing up $db_type_name database '$db_name' from container: $container_name (using app user)"
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" "$dump_cmd" -u "$backup_user" "$db_name" --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" "$dump_cmd" -u "$backup_user" "$db_name" --single-transaction --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
colorized_echo red "$db_type_name dump failed. Check log file for details."
error_messages+=("$db_type_name dump failed.")
else
Expand Down Expand Up @@ -1690,7 +1690,7 @@ backup_command() {
error_messages+=("MySQL database name not found.")
else
colorized_echo blue "Backing up $db_type_name database '$db_name' from container: $container_name (using app user)"
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" "$dump_cmd" -u "$backup_user" "$db_name" --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
if ! docker exec -e MYSQL_PWD="$backup_password" "$container_name" "$dump_cmd" -u "$backup_user" "$db_name" --single-transaction --events --triggers >"$temp_dir/db_backup.sql" 2>>"$log_file"; then
colorized_echo red "$db_type_name dump failed. Check log file for details."
error_messages+=("$db_type_name dump failed.")
else
Expand Down
1 change: 1 addition & 0 deletions tests/run_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ SUITES=(
"unit_restore_archive_safety.sh"
"unit_restore_recovery.sh"
"unit_backup_metadata.sh"
"unit_backup_dump_flags.sh"
"unit_compose_templates.sh"
"test_script_update_safety.sh"
)
Expand Down
93 changes: 93 additions & 0 deletions tests/unit_backup_dump_flags.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
#!/usr/bin/env bash
# =============================================================================
# unit_backup_dump_flags.sh - SQL dump invocations used by `pasarguard backup`
# must never lock the tables: every $dump_cmd exec carries --single-transaction.
# =============================================================================
set -euo pipefail

ROOT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." && pwd)"

WORK_DIR="$(mktemp -d)"
trap 'rm -rf "$WORK_DIR"' EXIT

export APP_TMP_DIR="$WORK_DIR/tmp"
export APP_NAME="pasarguard-dump-flags-unit"
export APP_DIR="$WORK_DIR/app"
export DATA_DIR="$WORK_DIR/data"
mkdir -p "$APP_TMP_DIR"

# A fake docker CLI: records every exec call so the test can assert on the
# arguments the dump was invoked with. Compose lists the mysql service.
FAKE_DOCKER_DIR="$WORK_DIR/fake-docker"
export FAKE_DOCKER_DIR
mkdir -p "$WORK_DIR/bin" "$FAKE_DOCKER_DIR"
: >"$FAKE_DOCKER_DIR/calls.log"
cat >"$WORK_DIR/bin/docker" <<'FAKE'
#!/usr/bin/env bash
d="${FAKE_DOCKER_DIR:?}"
printf '%s\n' "$*" >>"$d/calls.log"
case "$*" in
compose*" config --services"*) printf 'pasarguard\n' ;;
compose*" ps -q "*|"compose ps -q "*) printf 'cid\n' ;;
compose*" ps -a -q "*) printf 'cid-%s\n' "${@: -1}" ;;
"inspect --format {{.Image}} cid-"*) printf 'sha256:%s\n' "$(printf '1%.0s' {1..64})" ;;
"image inspect --format"*) printf 'example/%s@sha256:%s\n' "svc" "$(printf '2%.0s' {1..64})" ;;
ps*) printf 'cid\n' ;;
exec*) exit 0 ;;
*) exit 1 ;;
esac
FAKE
chmod 755 "$WORK_DIR/bin/docker"
export PATH="$WORK_DIR/bin:$PATH"

curl() { echo ""; return 0; }
export -f curl

export PASARGUARD_SOURCE_ONLY="true"
# shellcheck source=pasarguard.sh
source "$ROOT_DIR/pasarguard.sh"
set +e
set -uo pipefail
# shellcheck disable=SC2034 # read by the dump helpers under test
COMPOSE="docker compose"

PASS=0
FAIL=0
pass() { echo "✓ $1"; PASS=$((PASS + 1)); }
fail() { echo "✗ $1"; FAIL=$((FAIL + 1)); }
assert_true() { local l="$1"; shift; if "$@"; then pass "$l"; else fail "$l"; fi; }
assert_eq() {
local actual="$1" expected="$2" label="$3"
if [ "$actual" = "$expected" ]; then pass "$label"; else fail "$label (expected='$expected' got='$actual')"; fi
}

echo "=== unit_backup_dump_flags.sh ==="

# -----------------------------------------------------------------------
# Every mysqldump / mariadb-dump exec site carries --single-transaction
# -----------------------------------------------------------------------
# Static check first: the flag appears next to every dump exec ("$dump_cmd"
# and the direct mariadb-dump calls), so a new call site cannot silently
# reintroduce table locks.
dump_sites="$(grep -nE 'docker exec .*("\$dump_cmd"|mariadb-dump) -u' "$ROOT_DIR/lib/pasarguard-backup.sh")"
total_sites="$(printf '%s\n' "$dump_sites" | grep -c .)"
flagged_sites="$(printf '%s\n' "$dump_sites" | grep -c -- '--single-transaction')"
assert_eq "$total_sites" "7" "dump flags: found all 7 mysqldump/mariadb-dump exec sites"
assert_eq "$flagged_sites" "$total_sites" "dump flags: all $total_sites dump exec sites carry --single-transaction"

# Dynamic check: run the MySQL backup path with the fake docker and confirm
# the recorded exec arguments really contain the flag.
stage="$WORK_DIR/run"
mkdir -p "$stage" "$APP_DIR"
printf 'SQLALCHEMY_DATABASE_URL=mysql+pymysql://pasarguard:pw@127.0.0.1:3306/pasarguard\n' >"$APP_DIR/.env"
printf 'MYSQL_ROOT_PASSWORD=rootpw\n' >>"$APP_DIR/.env"
printf 'SQLALCHEMY_DATABASE_URL=mysql+pymysql://pasarguard:pw@127.0.0.1:3306/pasarguard\n' >"$stage/.env"
printf 'MYSQL_ROOT_PASSWORD=rootpw\n' >>"$stage/.env"
: >"$FAKE_DOCKER_DIR/calls.log"
mkdir -p "$APP_DIR"
(backup_command backup "$stage") >"$WORK_DIR/run.out" 2>"$WORK_DIR/run.err"
assert_true "dump flags: recorded exec args contain --single-transaction" grep -q -- '--single-transaction' "$FAKE_DOCKER_DIR/calls.log"

echo ""
echo "Results: $PASS passed, $FAIL failed"
[ "$FAIL" -eq 0 ] || exit 1