108 lines
2.9 KiB
Bash
108 lines
2.9 KiB
Bash
#!/bin/bash
|
|
|
|
set -e # Exit on error
|
|
trap 'echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: An error occurred. Exiting." | tee -a "$LOG_FILE"' ERR
|
|
|
|
# Global Configurations
|
|
LOG_FILE="/var/log/backup_script.log"
|
|
BACKUP_REPO_PATH="/mnt/backups/${ENV_NAME}"
|
|
PASSWORD_FILE="/etc/restic-password"
|
|
RESTIC_CONFIG_FILE="/etc/restic-config"
|
|
DEFAULT_BACKUP_COUNT=5
|
|
|
|
# Logging function
|
|
log_message() {
|
|
local message="$1"
|
|
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "$LOG_FILE"
|
|
}
|
|
|
|
# Validate required variables
|
|
validate_inputs() {
|
|
: "${RESTIC_PASSWORD:?RESTIC_PASSWORD not set}"
|
|
: "${BACKUP_REPO_PATH:?BACKUP_REPO_PATH not set}"
|
|
}
|
|
|
|
# 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() {
|
|
[ -d "$BACKUP_REPO_PATH" ] || mkdir -p "$BACKUP_REPO_PATH"
|
|
|
|
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
|
|
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
|
|
;;
|
|
check_repo)
|
|
validate_inputs
|
|
check_backup_repo
|
|
;;
|
|
rotate_snapshots)
|
|
rotate_snapshots "$2"
|
|
;;
|
|
*)
|
|
echo "Usage: $0 {backup|check_repo|rotate_snapshots}"
|
|
exit 1
|
|
;;
|
|
esac
|