95 lines
2.5 KiB
Bash
95 lines
2.5 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
REPO_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
|
|
TARGET_SCRIPT="${REPO_ROOT}/scripts/backup-equipment.sh"
|
|
TEMP_DIR=""
|
|
|
|
assert_exists() {
|
|
local path="$1"
|
|
if [ ! -e "${path}" ]; then
|
|
echo "Expected path to exist: ${path}" >&2
|
|
return 1
|
|
fi
|
|
}
|
|
|
|
main() {
|
|
TEMP_DIR="$(mktemp -d)"
|
|
trap 'rm -rf "${TEMP_DIR}"' EXIT
|
|
|
|
local project_dir="${TEMP_DIR}/project"
|
|
local backup_root="${TEMP_DIR}/backups"
|
|
local fake_bin="${TEMP_DIR}/bin"
|
|
|
|
mkdir -p "${project_dir}/frontend/public/branding" "${backup_root}" "${fake_bin}"
|
|
|
|
printf 'version: "3"\n' > "${project_dir}/docker-compose.yml"
|
|
printf '#!/bin/bash\nexit 0\n' > "${project_dir}/deploy.sh"
|
|
printf 'FROM scratch\n' > "${project_dir}/Dockerfile"
|
|
printf 'brand\n' > "${project_dir}/frontend/public/branding/logo.txt"
|
|
|
|
mkdir -p \
|
|
"${backup_root}/source" \
|
|
"${backup_root}/manual_hotfix_20260326_195455" \
|
|
"${backup_root}/manual_hotfix_20260326_2035_authfix" \
|
|
"${backup_root}/20260326_141543" \
|
|
"${backup_root}/20260326_194741"
|
|
|
|
cat > "${fake_bin}/docker" <<'EOF'
|
|
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
command="${1:-}"
|
|
shift || true
|
|
|
|
case "${command}" in
|
|
inspect)
|
|
exit 0
|
|
;;
|
|
exec)
|
|
if printf '%s\n' "$*" | grep -q "mysqldump"; then
|
|
printf 'fake-sql-dump'
|
|
else
|
|
printf ''
|
|
fi
|
|
;;
|
|
*)
|
|
echo "Unexpected docker command: ${command} $*" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
EOF
|
|
chmod +x "${fake_bin}/docker"
|
|
|
|
PATH="${fake_bin}:${PATH}" \
|
|
PROJECT_DIR="${project_dir}" \
|
|
BACKUP_ROOT="${backup_root}" \
|
|
KEEP_BACKUPS=3 \
|
|
DB_CONTAINER="equipment-mysql" \
|
|
BACKEND_CONTAINER="equipment-backend" \
|
|
DB_NAME="equipment_management" \
|
|
DB_USER="root" \
|
|
DB_PASSWORD="secret" \
|
|
UPLOADS_PATH="/app/uploads" \
|
|
bash "${TARGET_SCRIPT}" >/dev/null
|
|
|
|
mapfile -t timestamp_backups < <(find "${backup_root}" -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | grep -E '^[0-9]{8}_[0-9]{6}$' | sort)
|
|
if [ "${#timestamp_backups[@]}" -eq 0 ]; then
|
|
echo "Expected at least one timestamp backup to remain after pruning" >&2
|
|
return 1
|
|
fi
|
|
local latest_index=$(( ${#timestamp_backups[@]} - 1 ))
|
|
local latest_backup="${backup_root}/${timestamp_backups[${latest_index}]}"
|
|
|
|
assert_exists "${latest_backup}"
|
|
assert_exists "${backup_root}/20260326_141543"
|
|
assert_exists "${backup_root}/20260326_194741"
|
|
assert_exists "${backup_root}/source"
|
|
assert_exists "${backup_root}/manual_hotfix_20260326_195455"
|
|
assert_exists "${backup_root}/manual_hotfix_20260326_2035_authfix"
|
|
}
|
|
|
|
main "$@"
|