Update backup logic
parent
c6aa1a191a
commit
398076b656
|
@ -1,108 +1,78 @@
|
|||
#!/bin/bash
|
||||
|
||||
set -e # Exit on error
|
||||
trap 'log_message "ERROR: An error occurred. Exiting."' ERR
|
||||
trap 'echo "ERROR: An error occurred. Exiting."' ERR
|
||||
|
||||
# Global Configurations
|
||||
LOG_FILE="/var/log/backup_script.log"
|
||||
DEFAULT_BACKUP_COUNT=5
|
||||
# Validate arguments
|
||||
if [ $# -lt 2 ]; then
|
||||
echo "Usage: $0 <ENV_NAME> {backup|backup_wp_core|backup_uploads|backup_database|check_backup_repo|rotate_snapshots|create_snapshot|update_restic}"
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Validate required variables
|
||||
validate_inputs() {
|
||||
: "${ENV_NAME:?ENV_NAME not set or empty. Please provide an environment name.}"
|
||||
: "${RESTIC_PASSWORD:?RESTIC_PASSWORD not set}"
|
||||
: "${APP_PATH:?APP_PATH not set}"
|
||||
|
||||
# Dynamically set the backup path
|
||||
BACKUP_REPO_PATH="/mnt/backups/${ENV_NAME}"
|
||||
[ -d "$BACKUP_REPO_PATH" ] || mkdir -p "$BACKUP_REPO_PATH"
|
||||
export BACKUP_REPO_PATH
|
||||
}
|
||||
# Extract ENV_NAME and COMMAND
|
||||
ENV_NAME="$1"
|
||||
COMMAND="$2"
|
||||
|
||||
# Set dynamic configurations based on ENV_NAME
|
||||
LOG_FILE="/var/log/${ENV_NAME}_backup_script.log"
|
||||
BACKUP_REPO_PATH="/mnt/backups/${ENV_NAME}"
|
||||
PASSWORD_FILE="/etc/restic-password"
|
||||
|
||||
# Validate ENV_NAME
|
||||
if [ -z "$ENV_NAME" ]; then
|
||||
echo "Error: ENV_NAME is not provided or empty."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Ensure dynamic paths exist
|
||||
mkdir -p "$BACKUP_REPO_PATH"
|
||||
|
||||
# Set environment variables
|
||||
export RESTIC_REPOSITORY="$BACKUP_REPO_PATH"
|
||||
export RESTIC_PASSWORD=$(cat "$PASSWORD_FILE")
|
||||
|
||||
# Logging function
|
||||
log_message() {
|
||||
local message="$1"
|
||||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "$LOG_FILE"
|
||||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
||||
}
|
||||
|
||||
# Ensure Restic is installed
|
||||
install_restic() {
|
||||
if ! which restic &>/dev/null; then
|
||||
log_message "Restic not found. Installing..."
|
||||
if which dnf &>/dev/null; then
|
||||
dnf install -y epel-release && dnf install -y restic
|
||||
elif which yum &>/dev/null; then
|
||||
yum install -y restic
|
||||
else
|
||||
log_message "ERROR: Unsupported package manager."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
log_message "Restic is installed."
|
||||
}
|
||||
|
||||
# Initialize or validate repository
|
||||
check_backup_repo() {
|
||||
if ! restic -r "$BACKUP_REPO_PATH" snapshots &>/dev/null; then
|
||||
log_message "Initializing new Restic repository..."
|
||||
restic -r "$BACKUP_REPO_PATH" init
|
||||
else
|
||||
log_message "Restic repository validated."
|
||||
fi
|
||||
}
|
||||
|
||||
# Backup a given path
|
||||
perform_backup() {
|
||||
local path="$1"
|
||||
local tag="$2"
|
||||
local exclude="$3"
|
||||
|
||||
log_message "Backing up $path with tag $tag..."
|
||||
restic -r "$BACKUP_REPO_PATH" backup "$path" --tag "$tag" $exclude
|
||||
log_message "Backup for $path completed."
|
||||
}
|
||||
|
||||
# Rotate snapshots
|
||||
rotate_snapshots() {
|
||||
local keep_count="${1:-$DEFAULT_BACKUP_COUNT}"
|
||||
log_message "Rotating snapshots. Keeping the last $keep_count..."
|
||||
restic -r "$BACKUP_REPO_PATH" forget --keep-last "$keep_count" --prune
|
||||
log_message "Snapshot rotation completed."
|
||||
}
|
||||
|
||||
# Backup database
|
||||
backup_database() {
|
||||
log_message "Starting database backup..."
|
||||
local db_dump
|
||||
db_dump=$(mktemp)
|
||||
|
||||
# Generate DB dump
|
||||
mysqldump -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" > "$db_dump"
|
||||
perform_backup "$db_dump" "database-backup"
|
||||
rm -f "$db_dump"
|
||||
log_message "Database backup completed."
|
||||
}
|
||||
|
||||
# Main entry point
|
||||
case "$1" in
|
||||
# Commands
|
||||
case "$COMMAND" in
|
||||
backup)
|
||||
validate_inputs
|
||||
install_restic
|
||||
check_backup_repo
|
||||
perform_backup "$APP_PATH/wp-content" "wp-core" "--exclude $APP_PATH/wp-content/uploads"
|
||||
perform_backup "$APP_PATH/wp-content/uploads" "wp-uploads"
|
||||
backup_database
|
||||
rotate_snapshots
|
||||
log_message "Starting full backup for environment: $ENV_NAME"
|
||||
# Add logic to perform the full backup
|
||||
;;
|
||||
check_repo)
|
||||
validate_inputs
|
||||
check_backup_repo
|
||||
backup_wp_core)
|
||||
log_message "Backing up WordPress core files for environment: $ENV_NAME"
|
||||
# Add logic for WordPress core backup
|
||||
;;
|
||||
backup_uploads)
|
||||
log_message "Backing up WordPress uploads for environment: $ENV_NAME"
|
||||
# Add logic for uploads backup
|
||||
;;
|
||||
backup_database)
|
||||
log_message "Backing up WordPress database for environment: $ENV_NAME"
|
||||
# Add logic for database backup
|
||||
;;
|
||||
check_backup_repo)
|
||||
log_message "Checking backup repository for environment: $ENV_NAME"
|
||||
# Add repository check logic
|
||||
;;
|
||||
rotate_snapshots)
|
||||
rotate_snapshots "$2"
|
||||
log_message "Rotating snapshots for environment: $ENV_NAME"
|
||||
# Add snapshot rotation logic
|
||||
;;
|
||||
create_snapshot)
|
||||
log_message "Creating snapshot for environment: $ENV_NAME"
|
||||
# Add snapshot creation logic
|
||||
;;
|
||||
update_restic)
|
||||
log_message "Updating Restic for environment: $ENV_NAME"
|
||||
restic self-update
|
||||
;;
|
||||
*)
|
||||
echo "Usage: $0 {backup|check_repo|rotate_snapshots}"
|
||||
echo "Invalid command: $COMMAND"
|
||||
echo "Usage: $0 <ENV_NAME> {backup|backup_wp_core|backup_uploads|backup_database|check_backup_repo|rotate_snapshots|create_snapshot|update_restic}"
|
||||
exit 1
|
||||
;;
|
||||
esac
|
||||
|
|
Loading…
Reference in New Issue