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

87 lines
2.1 KiB
Bash
Raw Normal View History

2024-09-18 16:53:20 +00:00
#!/bin/bash
2025-01-06 15:33:56 +00:00
# Exit on error and handle errors with a trap
set -e
trap 'echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: An error occurred during the backup process." | tee -a "$LOG_FILE"' ERR
2024-11-12 18:15:11 +00:00
# Load the backup logic functions
source /home/jelastic/mb-backups/backup-logic.sh
2025-01-06 15:33:56 +00:00
# Function: Log messages
log_message() {
local message="$1"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "$LOG_FILE"
}
# Function: Validate the password
validate_password() {
local password="$1"
if [ ! -f "$password_file" ]; then
log_message "ERROR: Password file not found at $password_file"
exit 1
fi
local stored_password
stored_password=$(cat "$password_file")
if [ "$stored_password" != "$password" ]; then
log_message "ERROR: Password mismatch. Aborting backup."
exit 1
fi
}
2024-09-18 16:53:20 +00:00
# Check for required arguments
if [ $# -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>"
exit 1
fi
# Assign arguments to variables
RESTIC_PASSWORD="$1"
ADDITIONAL_TAG="$2"
# Configuration
2025-01-06 15:33:56 +00:00
APP_PATH="/var/www/webroot/ROOT"
backupPath="/mnt/backups"
2024-11-12 18:15:11 +00:00
password_file="/etc/restic-password"
2024-09-18 16:53:20 +00:00
LOG_DIR="/home/jelastic/mb-backups/logs"
LOG_FILE="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log"
excludePaths=(
"$APP_PATH/wp-content/uploads"
)
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Set Restic environment variables
export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD
2025-01-06 15:33:56 +00:00
# Validate the password
validate_password "$RESTIC_PASSWORD"
2024-11-12 18:15:11 +00:00
# Check repository accessibility and integrity
2025-01-06 15:33:56 +00:00
if ! check_backup_repo; then
log_message "ERROR: Backup repository check failed. Aborting backup."
2024-11-12 18:15:11 +00:00
exit 1
fi
2025-01-06 15:33:56 +00:00
# Start logging
log_message "Starting Core Files Backup with tags: core_files, $ADDITIONAL_TAG"
2024-09-18 16:53:20 +00:00
2025-01-06 15:33:56 +00:00
# Build exclude options
2024-09-18 16:53:20 +00:00
excludeOptions=""
for path in "${excludePaths[@]}"; do
excludeOptions+="--exclude $path "
done
2025-01-06 15:33:56 +00:00
# Perform backup
2024-09-18 16:53:20 +00:00
if restic backup $excludeOptions "$APP_PATH" --tag core_files --tag "$ADDITIONAL_TAG"; then
2025-01-06 15:33:56 +00:00
log_message "Core files backup completed successfully."
2024-09-18 16:53:20 +00:00
else
2025-01-06 15:33:56 +00:00
log_message "ERROR: Core files backup failed."
2024-11-12 18:15:11 +00:00
exit 1
2024-09-18 16:53:20 +00:00
fi
2025-01-06 15:33:56 +00:00
# Finish logging
log_message "Backup process finished."