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

79 lines
1.9 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
2025-01-06 17:22:25 +00:00
trap 'log_message "ERROR: An error occurred during the backup process."' ERR
2024-11-12 18:15:11 +00:00
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
2025-01-06 17:22:25 +00:00
stored_password=$(<"$password_file")
2025-01-06 15:33:56 +00:00
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
2025-01-06 17:22:25 +00:00
echo "Usage: $0 <RESTIC_PASSWORD> <CUSTOM_LABEL>"
2024-09-18 16:53:20 +00:00
exit 1
fi
# Assign arguments to variables
RESTIC_PASSWORD="$1"
2025-01-06 17:22:25 +00:00
CUSTOM_LABEL="$2"
2024-09-18 16:53:20 +00:00
# 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"
2025-01-06 17:22:25 +00:00
STATIC_TAG="core_files" # Static tag for this backup type
2024-09-18 16:53:20 +00:00
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
2025-01-06 17:22:25 +00:00
# Logging start
log_message "Starting Core Files Backup with tags: $STATIC_TAG, $CUSTOM_LABEL"
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
2025-01-06 17:22:25 +00:00
if restic backup $excludeOptions "$APP_PATH" --tag "$STATIC_TAG" --tag "$CUSTOM_LABEL"; 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 17:22:25 +00:00
# Logging end
2025-01-06 15:33:56 +00:00
log_message "Backup process finished."