|
| 1 | +#!/bin/bash |
| 2 | +echo "$PWD" |
| 3 | +source ../../base-docker-wsl/conf.env |
| 4 | +# Load Environment Variables for DGraph Alpha and Zero Nodes |
| 5 | +DGRAPH_ALPHA_HOSTS="${DGRAPH_HOSTS:-localhost}" |
| 6 | +DGRAPH_ALPHA_PORTS="${DGRAPH_PORTS:-8080}" |
| 7 | +DGRAPH_ZERO_HOSTS="${DGRAPH_ZERO_HOSTS:-localhost}" |
| 8 | +DGRAPH_ZERO_PORTS="${DGRAPH_ZERO_PORTS:-5080}" |
| 9 | + |
| 10 | +# Load Environment Variables for Data Directories |
| 11 | +DATA_DGRAPH_ZERO_01_DIR="${DATA_DGRAPH_ZERO_01_DIR}" |
| 12 | +DATA_DGRAPH_ALPHA_01_DIR="${DATA_DGRAPH_ALPHA_01_DIR}" |
| 13 | +DATA_DGRAPH_ALPHA_02_DIR="${DATA_DGRAPH_ALPHA_02_DIR:-}" # Optional |
| 14 | +DATA_DGRAPH_ALPHA_03_DIR="${DATA_DGRAPH_ALPHA_03_DIR:-}" # Optional |
| 15 | +BACKUP_PATH="${DGRAPH_BACKUP_DIRECTORY}/$(date +%Y%m%d_%H%M%S)" |
| 16 | +REMOTE_SERVER="${DGRAPH_BACKUP_REMOTE_SERVER}" |
| 17 | +REMOTE_PATH="${DGRAPH_BACKUP_REMOTE_PATH}" |
| 18 | + |
| 19 | +# Create Backup Directory if it doesn't exist |
| 20 | +[ ! -d "$BACKUP_PATH" ] && mkdir -p "$BACKUP_PATH" |
| 21 | + |
| 22 | +LOG_FILE="${BACKUP_PATH}/dgraph_backup_$(date +%Y%m%d_%H%M%S).log" |
| 23 | + |
| 24 | +# Function to Backup DGraph Alpha and Zero Nodes |
| 25 | +backup_dgraph_node() { |
| 26 | + local host=$1 |
| 27 | + local port=$2 |
| 28 | + |
| 29 | + echo "$(date) - Starting backup for DGraph node at ${host}:${port}" >> "${LOG_FILE}" |
| 30 | + # Replace with actual backup command for the node. Example: |
| 31 | + # curl "http://${host}:${port}/admin/backup" |
| 32 | + echo "$(date) - Backup completed for DGraph node at ${host}:${port}" >> "${LOG_FILE}" |
| 33 | +} |
| 34 | + |
| 35 | +# Function to Backup DGraph Directory |
| 36 | +backup_dgraph_dir() { |
| 37 | + local dir=$1 |
| 38 | + local dir_name=$(basename "$dir") |
| 39 | + echo "Starting Dgraph database Backup..." |
| 40 | + echo "$(date) - Starting backup for DGraph directory at $dir" >> "${LOG_FILE}" |
| 41 | + tar -czvf "${BACKUP_PATH}/${dir_name}_$(date +%Y%m%d_%H%M%S).tar.gz" -C "$dir" . |
| 42 | + echo "$(date) - Backup completed for DGraph directory at $dir" >> "${LOG_FILE}" |
| 43 | + echo "Database Dgraph Backup completed." |
| 44 | +} |
| 45 | + |
| 46 | + |
| 47 | +# Backup DGraph Nodes (Alphas and Zeros) |
| 48 | +IFS=',' read -r -a alpha_hosts <<< "$DGRAPH_ALPHA_HOSTS" |
| 49 | +IFS=',' read -r -a alpha_ports <<< "$DGRAPH_ALPHA_PORTS" |
| 50 | +IFS=',' read -r -a zero_hosts <<< "$DGRAPH_ZERO_HOSTS" |
| 51 | +IFS=',' read -r -a zero_ports <<< "$DGRAPH_ZERO_PORTS" |
| 52 | + |
| 53 | +for i in "${!alpha_hosts[@]}"; do |
| 54 | + backup_dgraph_node "${alpha_hosts[i]}" "${alpha_ports[i]}" |
| 55 | +done |
| 56 | + |
| 57 | +for i in "${!zero_hosts[@]}"; do |
| 58 | + backup_dgraph_node "${zero_hosts[i]}" "${zero_ports[i]}" |
| 59 | +done |
| 60 | + |
| 61 | +# Backup DGraph Directories |
| 62 | +[ -d "$DATA_DGRAPH_ZERO_01_DIR" ] && backup_dgraph_dir "$DATA_DGRAPH_ZERO_01_DIR" |
| 63 | +[ -d "$DATA_DGRAPH_ALPHA_01_DIR" ] && backup_dgraph_dir "$DATA_DGRAPH_ALPHA_01_DIR" |
| 64 | +[ -d "$DATA_DGRAPH_ALPHA_02_DIR" ] && backup_dgraph_dir "$DATA_DGRAPH_ALPHA_02_DIR" |
| 65 | +[ -d "$DATA_DGRAPH_ALPHA_03_DIR" ] && backup_dgraph_dir "$DATA_DGRAPH_ALPHA_03_DIR" |
| 66 | + |
| 67 | +# Function to Copy Backup to Remote Server |
| 68 | +copy_to_remote() { |
| 69 | + if [ -n "${REMOTE_SERVER}" ] && [ -n "${REMOTE_PATH}" ]; then |
| 70 | + echo "$(date) - Starting remote transfer" >> "${LOG_FILE}" |
| 71 | + scp "${BACKUP_PATH}/*_$(date +%Y%m%d).tar.gz" ${REMOTE_SERVER}:${REMOTE_PATH} |
| 72 | + echo "$(date) - Remote transfer completed" >> "${LOG_FILE}" |
| 73 | + else |
| 74 | + echo "$(date) - Remote server details not set. Skipping remote transfer." >> "${LOG_FILE}" |
| 75 | + fi |
| 76 | +} |
| 77 | + |
| 78 | +# Main Execution |
| 79 | +copy_to_remote |
| 80 | +chmod -R 777 "$BACKUP_PATH" |
0 commit comments