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

79 lines
1.9 KiB
Bash

#!/bin/bash
# Exit on error and handle errors with a trap
set -e
trap 'log_message "ERROR: An error occurred during the backup process."' ERR
# 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=$(<"$password_file")
if [ "$stored_password" != "$password" ]; then
log_message "ERROR: Password mismatch. Aborting backup."
exit 1
fi
}
# Check for required arguments
if [ $# -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <CUSTOM_LABEL>"
exit 1
fi
# Assign arguments to variables
RESTIC_PASSWORD="$1"
CUSTOM_LABEL="$2"
# Configuration
APP_PATH="/var/www/webroot/ROOT"
backupPath="/mnt/backups"
password_file="/etc/restic-password"
LOG_DIR="/home/jelastic/mb-backups/logs"
LOG_FILE="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log"
STATIC_TAG="core_files" # Static tag for this backup type
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
# Validate the password
validate_password "$RESTIC_PASSWORD"
# Logging start
log_message "Starting Core Files Backup with tags: $STATIC_TAG, $CUSTOM_LABEL"
# Build exclude options
excludeOptions=""
for path in "${excludePaths[@]}"; do
excludeOptions+="--exclude $path "
done
# Perform backup
if restic backup $excludeOptions "$APP_PATH" --tag "$STATIC_TAG" --tag "$CUSTOM_LABEL"; then
log_message "Core files backup completed successfully."
else
log_message "ERROR: Core files backup failed."
exit 1
fi
# Logging end
log_message "Backup process finished."