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

95 lines
2.5 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"
}
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"
BACKUP_PATH="/mnt/backups"
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
EXCLUDE_PATHS=(
2024-09-18 16:53:20 +00:00
"$APP_PATH/wp-content/uploads"
)
LOCK_FILE="/tmp/restic_global.lock"
2024-09-18 16:53:20 +00:00
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Validate password file and read password
if [ ! -f "$PASSWORD_FILE" ]; then
log_message "ERROR: Password file not found at $PASSWORD_FILE."
exit 1
fi
export RESTIC_PASSWORD=$(cat "$PASSWORD_FILE")
# Verify repository access
log_message "Verifying repository access..."
if ! restic -r "$BACKUP_PATH" snapshots > /dev/null 2>&1; then
log_message "ERROR: Unable to access the Restic repository. Aborting backup."
exit 1
fi
# Acquire a global lock to serialize Restic operations
log_message "Acquiring global lock for Restic operations..."
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
log_message "Another Restic operation is running. Exiting."
exit 1
fi
log_message "Global lock acquired."
2024-09-18 16:53:20 +00:00
# Check and remove stale locks
log_message "Checking for stale locks in the repository..."
if restic -r "$BACKUP_PATH" list locks | grep -q "lock"; then
log_message "Stale locks detected. Unlocking the repository..."
restic -r "$BACKUP_PATH" unlock
log_message "Repository unlocked successfully."
else
log_message "No stale locks found."
fi
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
EXCLUDE_OPTIONS=""
for path in "${EXCLUDE_PATHS[@]}"; do
EXCLUDE_OPTIONS+="--exclude $path "
2024-09-18 16:53:20 +00:00
done
2025-01-06 15:33:56 +00:00
# Perform backup
if restic -r "$BACKUP_PATH" backup $EXCLUDE_OPTIONS "$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
# Release global lock
exec 9>&-
2025-01-06 17:22:25 +00:00
# Logging end
log_message "Backup process finished successfully."