Ver 1.5 updated backup all logic with current server time and date
parent
64fe7bd34f
commit
3736c02560
|
@ -1,5 +1,26 @@
|
||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
## Version 1.5
|
||||||
|
|
||||||
|
### Added
|
||||||
|
- Simplified `backup_all.sh` by delegating backup tasks to individual scripts (`backup_core_files.sh`, `backup_database.sh`, `backup_media.sh`).
|
||||||
|
- Introduced centralized orchestration of all backups in `backup_all.sh` with a unified `manual-backup` tag for consistency.
|
||||||
|
- Ensured individual scripts can be reused independently or called as part of a larger workflow.
|
||||||
|
|
||||||
|
### Fixed
|
||||||
|
- Corrected `trap` command in `backup_all.sh` to ensure errors are properly logged to `backup_error.log`.
|
||||||
|
- Addressed issues with redundant logic in `backup_all.sh` by consolidating common functionality into the individual backup scripts.
|
||||||
|
- Resolved potential inconsistencies in tag usage by applying a standard `manual-backup` tag with the current server date across all backup scripts.
|
||||||
|
|
||||||
|
### Updated
|
||||||
|
- Refactored `backup_all.sh` to dynamically call individual backup scripts instead of duplicating logic for core, database, and media backups.
|
||||||
|
- Adjusted YAML configuration for `backupnow` to call `backup_all.sh` as the primary entry point for the backup process.
|
||||||
|
|
||||||
|
### Improved
|
||||||
|
- Centralized the backup process to enhance modularity and maintainability by separating core, database, and media backup logic.
|
||||||
|
- Ensured consistent tagging and logging across all backup types for easier monitoring and debugging.
|
||||||
|
- Streamlined the orchestration script (`backup_all.sh`) for improved readability and reduced duplication.
|
||||||
|
|
||||||
## Version 1.4
|
## Version 1.4
|
||||||
|
|
||||||
### Added
|
### Added
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
type: update
|
type: update
|
||||||
jpsVersion: 1.4
|
jpsVersion: 1.5
|
||||||
name: MightyBox WordPress Backup/Restore Addon
|
name: MightyBox WordPress Backup/Restore Addon
|
||||||
id: mb-backup-manager
|
id: mb-backup-manager
|
||||||
description: Custom Backup and Restore Addon for WordPress using Restic. Supports backing up databases, core files, media files, and full backups with scheduling and retention policies.
|
description: Custom Backup and Restore Addon for WordPress using Restic. Supports backing up databases, core files, media files, and full backups with scheduling and retention policies.
|
||||||
|
@ -173,7 +173,7 @@ actions:
|
||||||
backupnow:
|
backupnow:
|
||||||
- cmd[cp]:
|
- cmd[cp]:
|
||||||
user: root
|
user: root
|
||||||
commands: bash /home/jelastic/mb-backups/backup-logic.sh "$(cat /etc/restic-password)" "backup"
|
commands: bash /home/jelastic/mb-backups/backup_all.sh backup_now
|
||||||
- return:
|
- return:
|
||||||
type: info
|
type: info
|
||||||
message: "${response.out}"
|
message: "${response.out}"
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
|
|
||||||
set -e # Exit on error
|
set -e # Exit on error
|
||||||
trap 'echo "ERROR: An error occurred while executing $COMMAND for $ENV_NAME. Exiting."' ERR
|
trap 'log_message "ERROR: An error occurred while executing $COMMAND for $ENV_NAME. Exiting."' ERR
|
||||||
|
|
||||||
# Validate arguments
|
# Validate arguments
|
||||||
if [ $# -lt 2 ]; then
|
if [ $# -lt 2 ]; then
|
||||||
|
@ -17,30 +17,23 @@ COMMAND="$2"
|
||||||
LOG_FILE="/var/log/${ENV_NAME}_backup_script.log"
|
LOG_FILE="/var/log/${ENV_NAME}_backup_script.log"
|
||||||
BACKUP_REPO_PATH="/mnt/backups/${ENV_NAME}"
|
BACKUP_REPO_PATH="/mnt/backups/${ENV_NAME}"
|
||||||
PASSWORD_FILE="/etc/restic-password"
|
PASSWORD_FILE="/etc/restic-password"
|
||||||
|
LOCK_FILE="/tmp/restic_global.lock"
|
||||||
# Validate ENV_NAME
|
|
||||||
if [ -z "$ENV_NAME" ]; then
|
|
||||||
echo "Error: ENV_NAME is not provided or empty."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Ensure dynamic paths exist
|
# Ensure dynamic paths exist
|
||||||
mkdir -p "$BACKUP_REPO_PATH"
|
mkdir -p "$BACKUP_REPO_PATH"
|
||||||
|
|
||||||
# Set environment variables
|
|
||||||
if [ ! -f "$PASSWORD_FILE" ]; then
|
|
||||||
echo "Error: Password file not found at $PASSWORD_FILE."
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
export RESTIC_REPOSITORY="$BACKUP_REPO_PATH"
|
|
||||||
export RESTIC_PASSWORD=$(cat "$PASSWORD_FILE")
|
|
||||||
|
|
||||||
# Logging function
|
# Logging function
|
||||||
log_message() {
|
log_message() {
|
||||||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" | tee -a "$LOG_FILE"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Validate password file and set Restic 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")
|
||||||
|
|
||||||
# Ensure Restic is installed
|
# Ensure Restic is installed
|
||||||
install_restic() {
|
install_restic() {
|
||||||
if ! which restic &>/dev/null; then
|
if ! which restic &>/dev/null; then
|
||||||
|
@ -57,6 +50,34 @@ install_restic() {
|
||||||
log_message "Restic is installed."
|
log_message "Restic is installed."
|
||||||
}
|
}
|
||||||
|
|
||||||
|
# Acquire global lock to serialize operations
|
||||||
|
acquire_lock() {
|
||||||
|
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."
|
||||||
|
}
|
||||||
|
|
||||||
|
# Release global lock
|
||||||
|
release_lock() {
|
||||||
|
exec 9>&-
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check and remove stale locks
|
||||||
|
check_stale_locks() {
|
||||||
|
log_message "Checking for stale locks in the repository..."
|
||||||
|
if restic -r "$BACKUP_REPO_PATH" list locks | grep -q "lock"; then
|
||||||
|
log_message "Stale locks detected. Unlocking the repository..."
|
||||||
|
restic -r "$BACKUP_REPO_PATH" unlock
|
||||||
|
log_message "Repository unlocked successfully."
|
||||||
|
else
|
||||||
|
log_message "No stale locks found."
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
# Initialize or validate repository
|
# Initialize or validate repository
|
||||||
check_backup_repo() {
|
check_backup_repo() {
|
||||||
if ! restic -r "$BACKUP_REPO_PATH" snapshots &>/dev/null; then
|
if ! restic -r "$BACKUP_REPO_PATH" snapshots &>/dev/null; then
|
||||||
|
@ -95,34 +116,54 @@ case "$COMMAND" in
|
||||||
backup)
|
backup)
|
||||||
log_message "Starting full backup for environment: $ENV_NAME"
|
log_message "Starting full backup for environment: $ENV_NAME"
|
||||||
install_restic
|
install_restic
|
||||||
|
acquire_lock
|
||||||
|
check_stale_locks
|
||||||
check_backup_repo
|
check_backup_repo
|
||||||
perform_backup "/var/www/webroot/ROOT/wp-content" "wp-core" "--exclude /var/www/webroot/ROOT/wp-content/uploads"
|
perform_backup "/var/www/webroot/ROOT/wp-content" "wp-core" "--exclude /var/www/webroot/ROOT/wp-content/uploads"
|
||||||
perform_backup "/var/www/webroot/ROOT/wp-content/uploads" "wp-uploads"
|
perform_backup "/var/www/webroot/ROOT/wp-content/uploads" "wp-uploads"
|
||||||
backup_database
|
backup_database
|
||||||
|
release_lock
|
||||||
;;
|
;;
|
||||||
backup_wp_core)
|
backup_wp_core)
|
||||||
log_message "Backing up WordPress core files for environment: $ENV_NAME"
|
log_message "Backing up WordPress core files for environment: $ENV_NAME"
|
||||||
|
acquire_lock
|
||||||
|
check_stale_locks
|
||||||
perform_backup "/var/www/webroot/ROOT/wp-content" "wp-core" "--exclude /var/www/webroot/ROOT/wp-content/uploads"
|
perform_backup "/var/www/webroot/ROOT/wp-content" "wp-core" "--exclude /var/www/webroot/ROOT/wp-content/uploads"
|
||||||
|
release_lock
|
||||||
;;
|
;;
|
||||||
backup_uploads)
|
backup_uploads)
|
||||||
log_message "Backing up WordPress uploads for environment: $ENV_NAME"
|
log_message "Backing up WordPress uploads for environment: $ENV_NAME"
|
||||||
|
acquire_lock
|
||||||
|
check_stale_locks
|
||||||
perform_backup "/var/www/webroot/ROOT/wp-content/uploads" "wp-uploads"
|
perform_backup "/var/www/webroot/ROOT/wp-content/uploads" "wp-uploads"
|
||||||
|
release_lock
|
||||||
;;
|
;;
|
||||||
backup_database)
|
backup_database)
|
||||||
|
acquire_lock
|
||||||
|
check_stale_locks
|
||||||
backup_database
|
backup_database
|
||||||
|
release_lock
|
||||||
;;
|
;;
|
||||||
check_backup_repo)
|
check_backup_repo)
|
||||||
log_message "Checking backup repository for environment: $ENV_NAME"
|
log_message "Checking backup repository for environment: $ENV_NAME"
|
||||||
|
acquire_lock
|
||||||
|
check_stale_locks
|
||||||
check_backup_repo
|
check_backup_repo
|
||||||
|
release_lock
|
||||||
;;
|
;;
|
||||||
rotate_snapshots)
|
rotate_snapshots)
|
||||||
log_message "Rotating snapshots for environment: $ENV_NAME"
|
log_message "Rotating snapshots for environment: $ENV_NAME"
|
||||||
|
acquire_lock
|
||||||
restic -r "$BACKUP_REPO_PATH" forget --keep-last 5 --prune
|
restic -r "$BACKUP_REPO_PATH" forget --keep-last 5 --prune
|
||||||
|
release_lock
|
||||||
log_message "Snapshot rotation completed."
|
log_message "Snapshot rotation completed."
|
||||||
;;
|
;;
|
||||||
create_snapshot)
|
create_snapshot)
|
||||||
log_message "Creating snapshot for environment: $ENV_NAME"
|
log_message "Creating snapshot for environment: $ENV_NAME"
|
||||||
|
acquire_lock
|
||||||
|
check_stale_locks
|
||||||
restic -r "$BACKUP_REPO_PATH" backup --tag "manual-snapshot"
|
restic -r "$BACKUP_REPO_PATH" backup --tag "manual-snapshot"
|
||||||
|
release_lock
|
||||||
log_message "Snapshot creation completed."
|
log_message "Snapshot creation completed."
|
||||||
;;
|
;;
|
||||||
update_restic)
|
update_restic)
|
||||||
|
|
|
@ -2,119 +2,51 @@
|
||||||
|
|
||||||
# Enable error handling
|
# Enable error handling
|
||||||
set -e
|
set -e
|
||||||
|
trap 'echo "[$(date +%Y-%m-%d %H:%M:%S)] Backup failed" >> "$LOG_DIR/backup_error.log"' ERR
|
||||||
# Define log file path
|
|
||||||
LOG_DIR="/home/jelastic/mb-backups/logs"
|
|
||||||
mkdir -p "$LOG_DIR"
|
|
||||||
|
|
||||||
# Properly escape the trap command
|
|
||||||
trap 'echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup failed" >> "$LOG_DIR/backup_error.log"' ERR
|
|
||||||
|
|
||||||
# Load the backup logic functions
|
|
||||||
source /home/jelastic/mb-backups/backup-logic.sh
|
|
||||||
|
|
||||||
# Configuration
|
# Configuration
|
||||||
password_file="/etc/restic-password"
|
LOG_DIR="/home/jelastic/mb-backups/logs"
|
||||||
APP_PATH="/var/www/webroot/ROOT"
|
CORE_BACKUP_SCRIPT="/home/jelastic/mb-backups/backup_core_files.sh"
|
||||||
WP_CONFIG="${APP_PATH}/wp-config.php"
|
DATABASE_BACKUP_SCRIPT="/home/jelastic/mb-backups/backup_database.sh"
|
||||||
backupPath="/mnt/backups"
|
MEDIA_BACKUP_SCRIPT="/home/jelastic/mb-backups/backup_media.sh"
|
||||||
TEMP_DIR="/tmp/wp_backup_tmp"
|
MANUAL_BACKUP_TAG="manual-backup-$(date +'%Y-%m-%d_%H-%M-%S')"
|
||||||
MAX_BACKUP_SIZE=$((10 * 1024 * 1024 * 1024)) # 10GB
|
|
||||||
|
|
||||||
# Function: Ensure required commands are available
|
# Ensure log directory exists
|
||||||
validate_dependencies() {
|
mkdir -p "$LOG_DIR"
|
||||||
for cmd in restic mysqldump jq; do
|
|
||||||
if ! command -v "$cmd" &>/dev/null; then
|
|
||||||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Required command '$cmd' not found" | tee -a "$LOG_DIR/backup_error.log"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function: Initialize logging
|
# Logging function
|
||||||
log_message() {
|
log_message() {
|
||||||
local log_file="$1"
|
local message="$1"
|
||||||
local message="$2"
|
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "${LOG_DIR}/backup_all.log"
|
||||||
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "$log_file"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function: Check if backup size exceeds limit
|
|
||||||
check_backup_size() {
|
|
||||||
local path="$1"
|
|
||||||
local size
|
|
||||||
size=$(du -sb "$path" | cut -f1)
|
|
||||||
|
|
||||||
if [ "$size" -gt "$MAX_BACKUP_SIZE" ]; then
|
|
||||||
log_message "$LOG_DIR/backup_warning.log" "Warning: Backup size exceeds 10GB for $path"
|
|
||||||
return 1
|
|
||||||
fi
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function: Verify backup
|
|
||||||
verify_backup() {
|
|
||||||
local tag="$1"
|
|
||||||
local latest_snapshot
|
|
||||||
latest_snapshot=$(restic snapshots --latest 1 --tag "$tag" --json | jq -r '.[0].id')
|
|
||||||
if [ -n "$latest_snapshot" ]; then
|
|
||||||
restic check --read-data "$latest_snapshot"
|
|
||||||
return $?
|
|
||||||
fi
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function: Initialize backup
|
|
||||||
initialize_backup() {
|
|
||||||
if [ ! -f "$password_file" ]; then
|
|
||||||
log_message "$LOG_DIR/backup_error.log" "ERROR: Password file not found at $password_file"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
export RESTIC_PASSWORD=$(cat "$password_file")
|
|
||||||
export RESTIC_REPOSITORY="$backupPath"
|
|
||||||
}
|
|
||||||
|
|
||||||
# Function: Backup core files
|
|
||||||
backup_core_files() {
|
|
||||||
local log_file="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log"
|
|
||||||
log_message "$log_file" "Starting Core Files Backup"
|
|
||||||
|
|
||||||
check_backup_size "$APP_PATH" || return 1
|
|
||||||
|
|
||||||
local excludePaths=("$APP_PATH/wp-content/uploads")
|
|
||||||
local excludeOptions=""
|
|
||||||
for path in "${excludePaths[@]}"; do
|
|
||||||
excludeOptions+="--exclude $path "
|
|
||||||
done
|
|
||||||
|
|
||||||
if restic backup $excludeOptions "$APP_PATH" --tag core_files --tag full_backup; then
|
|
||||||
verify_backup "core_files" || return 1
|
|
||||||
log_message "$log_file" "Core files backup completed successfully"
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
return 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Main execution
|
# Main execution
|
||||||
main() {
|
main() {
|
||||||
validate_dependencies
|
log_message "Starting full backup process with tag: $MANUAL_BACKUP_TAG"
|
||||||
initialize_backup
|
|
||||||
|
|
||||||
local start_time=$(date +%s)
|
# Run individual backup scripts
|
||||||
local backup_date=$(date +'%Y-%m-%d_%H-%M-%S')
|
if bash "$CORE_BACKUP_SCRIPT" "$(cat /etc/restic-password)" "$MANUAL_BACKUP_TAG"; then
|
||||||
local main_log="${LOG_DIR}/full_backup_${backup_date}.log"
|
log_message "Core files backup completed successfully."
|
||||||
|
else
|
||||||
|
log_message "ERROR: Core files backup failed."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
log_message "$main_log" "Starting full backup process"
|
if bash "$DATABASE_BACKUP_SCRIPT" "$(cat /etc/restic-password)" "$MANUAL_BACKUP_TAG"; then
|
||||||
|
log_message "Database backup completed successfully."
|
||||||
|
else
|
||||||
|
log_message "ERROR: Database backup failed."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
backup_core_files || exit 1
|
if bash "$MEDIA_BACKUP_SCRIPT" "$(cat /etc/restic-password)" "$MANUAL_BACKUP_TAG"; then
|
||||||
backup_media_themes || exit 1
|
log_message "Media files backup completed successfully."
|
||||||
backup_database || exit 1
|
else
|
||||||
|
log_message "ERROR: Media files backup failed."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
cleanup_old_backups
|
log_message "Full backup process completed successfully."
|
||||||
|
|
||||||
local end_time=$(date +%s)
|
|
||||||
local duration=$((end_time - start_time))
|
|
||||||
|
|
||||||
log_message "$main_log" "Full backup completed in $duration seconds"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Argument handling
|
# Argument handling
|
||||||
|
|
Loading…
Reference in New Issue