#!/bin/bash # Enable error handling set -e # Define log file path LOG_DIR="/home/jelastic/mb-backups/logs" mkdir -p "$LOG_DIR" # Properly escape the trap command trap 'echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup failed" >> "$LOG_DIR/backup_error.log"' ERR # Load the backup logic functions source /home/jelastic/mb-backups/backup-logic.sh # Configuration password_file="/etc/restic-password" APP_PATH="/var/www/webroot/ROOT" WP_CONFIG="${APP_PATH}/wp-config.php" backupPath="/mnt/backups" TEMP_DIR="/tmp/wp_backup_tmp" MAX_BACKUP_SIZE=$((10 * 1024 * 1024 * 1024)) # 10GB # 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 } # 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" } # Function: Check if backup size exceeds limit check_backup_size() { local path="$1" local size size=$(du -sb "$path" | cut -f1) if [ "$size" -gt "$MAX_BACKUP_SIZE" ]; then log_message "$LOG_DIR/backup_warning.log" "Warning: Backup size exceeds 10GB for $path" return 1 fi return 0 } # Function: Verify backup verify_backup() { local tag="$1" local latest_snapshot latest_snapshot=$(restic snapshots --latest 1 --tag "$tag" --json | jq -r '.[0].id') if [ -n "$latest_snapshot" ]; then restic check --read-data "$latest_snapshot" return $? fi return 1 } # 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" exit 1 fi export RESTIC_PASSWORD=$(cat "$password_file") export RESTIC_REPOSITORY="$backupPath" } # Function: Backup core files backup_core_files() { local log_file="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log" log_message "$log_file" "Starting Core Files Backup" check_backup_size "$APP_PATH" || return 1 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 verify_backup "core_files" || return 1 log_message "$log_file" "Core files backup completed successfully" return 0 fi return 1 } # Main execution main() { validate_dependencies initialize_backup 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" log_message "$main_log" "Starting full backup process" 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)) log_message "$main_log" "Full backup completed in $duration seconds" } # Argument handling if [ "$1" == "backup_now" ]; then main else echo "Usage: $0 backup_now" exit 1 fi