mb-backup-manager/scripts/imports/backup_all.sh

127 lines
3.3 KiB
Bash
Raw Normal View History

2024-09-18 16:53:20 +00:00
#!/bin/bash
2024-11-12 18:15:11 +00:00
# Enable error handling
set -e
2025-01-06 15:18:14 +00:00
# Define log file path
LOG_DIR="/home/jelastic/mb-backups/logs"
mkdir -p "$LOG_DIR"
# Properly escape the trap command
2025-01-06 15:33:56 +00:00
trap 'echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup failed" >> "$LOG_DIR/backup_error.log"' ERR
2024-11-12 18:15:11 +00:00
# Load the backup logic functions
source /home/jelastic/mb-backups/backup-logic.sh
2024-09-18 16:53:20 +00:00
# Configuration
2024-11-12 18:15:11 +00:00
password_file="/etc/restic-password"
2025-01-06 15:33:56 +00:00
APP_PATH="/var/www/webroot/ROOT"
2024-09-18 16:53:20 +00:00
WP_CONFIG="${APP_PATH}/wp-config.php"
2025-01-06 15:33:56 +00:00
backupPath="/mnt/backups"
2024-11-12 18:15:11 +00:00
TEMP_DIR="/tmp/wp_backup_tmp"
MAX_BACKUP_SIZE=$((10 * 1024 * 1024 * 1024)) # 10GB
2025-01-06 15:33:56 +00:00
# Function: Ensure required commands are available
validate_dependencies() {
for cmd in restic mysqldump jq; do
if ! command -v "$cmd" &>/dev/null; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Required command '$cmd' not found" | tee -a "$LOG_DIR/backup_error.log"
exit 1
fi
done
}
2024-11-12 18:15:11 +00:00
2025-01-06 15:33:56 +00:00
# Function: Initialize logging
log_message() {
local log_file="$1"
local message="$2"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "$log_file"
2024-11-12 18:15:11 +00:00
}
2025-01-06 15:33:56 +00:00
# Function: Check if backup size exceeds limit
2024-11-12 18:15:11 +00:00
check_backup_size() {
local path="$1"
2025-01-06 15:33:56 +00:00
local size
size=$(du -sb "$path" | cut -f1)
2024-11-12 18:15:11 +00:00
if [ "$size" -gt "$MAX_BACKUP_SIZE" ]; then
2025-01-06 15:33:56 +00:00
log_message "$LOG_DIR/backup_warning.log" "Warning: Backup size exceeds 10GB for $path"
2024-11-12 18:15:11 +00:00
return 1
fi
return 0
}
2025-01-06 15:33:56 +00:00
# Function: Verify backup
2024-11-12 18:15:11 +00:00
verify_backup() {
local tag="$1"
2025-01-06 15:33:56 +00:00
local latest_snapshot
latest_snapshot=$(restic snapshots --latest 1 --tag "$tag" --json | jq -r '.[0].id')
2024-11-12 18:15:11 +00:00
if [ -n "$latest_snapshot" ]; then
restic check --read-data "$latest_snapshot"
return $?
fi
return 1
}
2025-01-06 15:33:56 +00:00
# Function: Initialize backup
initialize_backup() {
if [ ! -f "$password_file" ]; then
log_message "$LOG_DIR/backup_error.log" "ERROR: Password file not found at $password_file"
2025-01-06 15:13:20 +00:00
exit 1
fi
2025-01-06 15:33:56 +00:00
export RESTIC_PASSWORD=$(cat "$password_file")
export RESTIC_REPOSITORY="$backupPath"
2025-01-06 15:13:20 +00:00
}
2025-01-06 15:33:56 +00:00
# Function: Backup core files
2024-09-18 16:53:20 +00:00
backup_core_files() {
local log_file="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log"
2025-01-06 15:33:56 +00:00
log_message "$log_file" "Starting Core Files Backup"
2024-11-12 18:15:11 +00:00
check_backup_size "$APP_PATH" || return 1
2024-09-18 16:53:20 +00:00
local excludePaths=("$APP_PATH/wp-content/uploads")
local excludeOptions=""
for path in "${excludePaths[@]}"; do
excludeOptions+="--exclude $path "
done
if restic backup $excludeOptions "$APP_PATH" --tag core_files --tag full_backup; then
2024-11-12 18:15:11 +00:00
verify_backup "core_files" || return 1
2025-01-06 15:33:56 +00:00
log_message "$log_file" "Core files backup completed successfully"
2024-11-12 18:15:11 +00:00
return 0
2024-09-18 16:53:20 +00:00
fi
2024-11-12 18:15:11 +00:00
return 1
2024-09-18 16:53:20 +00:00
}
2024-11-12 18:15:11 +00:00
# Main execution
main() {
2025-01-06 15:33:56 +00:00
validate_dependencies
initialize_backup
2024-11-12 18:15:11 +00:00
local start_time=$(date +%s)
local backup_date=$(date +'%Y-%m-%d_%H-%M-%S')
local main_log="${LOG_DIR}/full_backup_${backup_date}.log"
2025-01-06 15:33:56 +00:00
log_message "$main_log" "Starting full backup process"
2024-11-12 18:15:11 +00:00
backup_core_files || exit 1
backup_media_themes || exit 1
backup_database || exit 1
cleanup_old_backups
local end_time=$(date +%s)
local duration=$((end_time - start_time))
2025-01-06 15:33:56 +00:00
log_message "$main_log" "Full backup completed in $duration seconds"
2024-11-12 18:15:11 +00:00
}
2024-09-18 16:53:20 +00:00
2025-01-06 15:13:20 +00:00
# Argument handling
if [ "$1" == "backup_now" ]; then
main
else
echo "Usage: $0 backup_now"
exit 1
fi