Last active
February 13, 2025 20:29
-
-
Save grahama1970/901d994cd04bbda8b0336fe0c4f479db to your computer and use it in GitHub Desktop.
Arango Dump and Restore bash scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# arangodump_db.sh | |
# This script dumps an ArangoDB database from a Docker container with a low memory batch size. | |
# | |
# Usage: ./arangodump_db.sh | |
# Make sure arangodump is in your PATH. | |
# This script dumps an ArangoDB database from a Docker container with a low memory batch size. | |
# The dump path follows the structure: backups/<database_name>/<timestamp>/arangodump | |
# | |
# Usage: ./arangodump_db.sh | |
# Make sure arangodump is in your PATH. | |
# --- Configuration --- | |
CONTAINER_NAME="arangodb" # Update with your container name | |
ARANGO_USER="root" | |
ARANGO_PASS="openSesame" | |
DB_NAME="verifaix" | |
HOST_DUMP_DIR="./arangodb_dumps" # New host directory for all dumps | |
CONTAINER_DUMP_DIR="/tmp/arangodump" # Temporary container directory | |
# Set a low batch size to limit memory usage (adjust as needed) | |
BATCH_SIZE=100 | |
# Create timestamped dump directory | |
TIMESTAMP=$(date +%Y%m%d-%H%M%S) | |
DUMP_DIR="${HOST_DUMP_DIR}/${DB_NAME}_${TIMESTAMP}" | |
mkdir -p "${DUMP_DIR}" | |
echo "Dumping ArangoDB database '${DB_NAME}' from container ${CONTAINER_NAME}..." | |
echo "Using batch size: ${BATCH_SIZE}" | |
echo "Output directory: ${DUMP_DIR}" | |
# Execute dump inside container and copy results out | |
docker exec "${CONTAINER_NAME}" \ | |
arangodump \ | |
--server.endpoint "tcp://localhost:8529" \ | |
--server.username "${ARANGO_USER}" \ | |
--server.password "${ARANGO_PASS}" \ | |
--server.database "${DB_NAME}" \ | |
--output-directory "${CONTAINER_DUMP_DIR}" \ | |
--batch-size "${BATCH_SIZE}" \ | |
--overwrite true | |
# Check if dump succeeded before copying | |
if [ $? -eq 0 ]; then | |
echo "Copying dump from container..." | |
docker cp "${CONTAINER_NAME}:${CONTAINER_DUMP_DIR}" "${DUMP_DIR}" | |
docker exec "${CONTAINER_NAME}" rm -rf "${CONTAINER_DUMP_DIR}" | |
echo "Database dump completed successfully." | |
else | |
echo "Database dump failed." >&2 | |
exit 1 | |
fi |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# arangorestore_db.sh | |
# The restore path follows the structure: backups/<database_name>/<timestamp>/arangodump | |
# --- Logging Function --- | |
log() { | |
printf "[%s] %s\n" "$(date +%T)" "$*" >&2 | |
} | |
# --- Configuration --- | |
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" # Get script's directory | |
DB_NAME="fraud_detection" | |
TARGET_DB=${DB_NAME} | |
DUMPS_ROOT="backups/fraud_detection" | |
CONTAINER_NAME="arangodb" | |
ARANGO_USER="root" | |
ARANGO_PASS="openSesame" | |
CONTAINER_TMP_DIR="/tmp/arangorestore" | |
# --- Arguments --- | |
# First find latest directory, then validate it | |
LATEST_DUMP=$(find "${DUMPS_ROOT}" -mindepth 1 -maxdepth 1 -type d | sort -r | head -1) | |
# --- Validation --- | |
log "π Checking dumps in: ${DUMPS_ROOT}" | |
if [ ! -d "${LATEST_DUMP}" ]; then | |
log "β No dumps found in: ${DUMPS_ROOT}" | |
exit 1 | |
fi | |
if [ ! -d "${LATEST_DUMP}/arangodump" ]; then | |
log "β Invalid dump structure in ${LATEST_DUMP}" | |
log " Missing: ${LATEST_DUMP}/arangodump subdirectory" | |
log " Directory contents:" | |
ls -l "${LATEST_DUMP}" | sed 's/^/ /' >&2 | |
exit 1 | |
fi | |
log "β Selected dump: ${LATEST_DUMP}" | |
# Validate dump structure | |
if [ ! -f "${LATEST_DUMP}/arangodump/ENCRYPTION" ]; then | |
echo "ERROR: Not a valid ArangoDB dump directory" >&2 | |
exit 1 | |
fi | |
log "π Starting restore to database '${TARGET_DB}'" | |
log "π Using dump: ${LATEST_DUMP} (exists: $(test -d "$LATEST_DUMP" && echo β || echo β))" | |
# Copy to container | |
log "π¦ Copying dump to container: ${CONTAINER_TMP_DIR}..." | |
docker cp "${LATEST_DUMP}" "${CONTAINER_NAME}:${CONTAINER_TMP_DIR}" || { | |
log "β Failed to copy dump files"; exit 1; | |
} | |
# Prepare restore command | |
RESTORE_PATH="${CONTAINER_TMP_DIR}/$(basename "${LATEST_DUMP}")/arangodump" | |
RESTORE_CMD="arangorestore \ | |
--server.endpoint tcp://localhost:8529 \ | |
--server.username ${ARANGO_USER} \ | |
--server.password ${ARANGO_PASS} \ | |
--server.database ${TARGET_DB} \ | |
--input-directory ${RESTORE_PATH} \ | |
--overwrite true \ | |
--create-database true" | |
# Execute restore | |
log "π Executing restore from: ${RESTORE_PATH}" | |
if docker exec "${CONTAINER_NAME}" ${RESTORE_CMD}; then | |
log "π§Ή Cleanup: Removing temp files" | |
docker exec "${CONTAINER_NAME}" rm -rf "${CONTAINER_TMP_DIR}" | |
log "β Restore successful" | |
else | |
log "π₯ Restore failed!" | |
log "β οΈ Temp files preserved at: ${CONTAINER_TMP_DIR}" | |
exit 1 | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment