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

87 lines
2.1 KiB
Bash

#!/bin/bash
# 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
# Load the backup logic functions
source /home/jelastic/mb-backups/backup-logic.sh
# 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
}
# 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
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"
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"
# Check repository accessibility and integrity
if ! check_backup_repo; then
log_message "ERROR: Backup repository check failed. Aborting backup."
exit 1
fi
# Start logging
log_message "Starting Core Files Backup with tags: core_files, $ADDITIONAL_TAG"
# Build exclude options
excludeOptions=""
for path in "${excludePaths[@]}"; do
excludeOptions+="--exclude $path "
done
# Perform backup
if restic backup $excludeOptions "$APP_PATH" --tag core_files --tag "$ADDITIONAL_TAG"; then
log_message "Core files backup completed successfully."
else
log_message "ERROR: Core files backup failed."
exit 1
fi
# Finish logging
log_message "Backup process finished."