Updated backup commands

main
Anthony 2025-01-07 01:22:25 +08:00
parent 6ca34bbe18
commit eaad9a63f2
3 changed files with 47 additions and 73 deletions

View File

@ -2,10 +2,7 @@
# Exit on error and handle errors with a trap # Exit on error and handle errors with a trap
set -e set -e
trap 'echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: An error occurred during the backup process." | tee -a "$LOG_FILE"' ERR trap 'log_message "ERROR: An error occurred during the backup process."' ERR
# Load the backup logic functions
source /home/jelastic/mb-backups/backup-logic.sh
# Function: Log messages # Function: Log messages
log_message() { log_message() {
@ -22,7 +19,7 @@ validate_password() {
fi fi
local stored_password local stored_password
stored_password=$(cat "$password_file") stored_password=$(<"$password_file")
if [ "$stored_password" != "$password" ]; then if [ "$stored_password" != "$password" ]; then
log_message "ERROR: Password mismatch. Aborting backup." log_message "ERROR: Password mismatch. Aborting backup."
exit 1 exit 1
@ -31,13 +28,13 @@ validate_password() {
# Check for required arguments # Check for required arguments
if [ $# -ne 2 ]; then if [ $# -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>" echo "Usage: $0 <RESTIC_PASSWORD> <CUSTOM_LABEL>"
exit 1 exit 1
fi fi
# Assign arguments to variables # Assign arguments to variables
RESTIC_PASSWORD="$1" RESTIC_PASSWORD="$1"
ADDITIONAL_TAG="$2" CUSTOM_LABEL="$2"
# Configuration # Configuration
APP_PATH="/var/www/webroot/ROOT" APP_PATH="/var/www/webroot/ROOT"
@ -45,6 +42,7 @@ backupPath="/mnt/backups"
password_file="/etc/restic-password" password_file="/etc/restic-password"
LOG_DIR="/home/jelastic/mb-backups/logs" LOG_DIR="/home/jelastic/mb-backups/logs"
LOG_FILE="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log" LOG_FILE="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log"
STATIC_TAG="core_files" # Static tag for this backup type
excludePaths=( excludePaths=(
"$APP_PATH/wp-content/uploads" "$APP_PATH/wp-content/uploads"
) )
@ -59,14 +57,8 @@ export RESTIC_PASSWORD
# Validate the password # Validate the password
validate_password "$RESTIC_PASSWORD" validate_password "$RESTIC_PASSWORD"
# Check repository accessibility and integrity # Logging start
if ! check_backup_repo; then log_message "Starting Core Files Backup with tags: $STATIC_TAG, $CUSTOM_LABEL"
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 # Build exclude options
excludeOptions="" excludeOptions=""
@ -75,12 +67,12 @@ for path in "${excludePaths[@]}"; do
done done
# Perform backup # Perform backup
if restic backup $excludeOptions "$APP_PATH" --tag core_files --tag "$ADDITIONAL_TAG"; then if restic backup $excludeOptions "$APP_PATH" --tag "$STATIC_TAG" --tag "$CUSTOM_LABEL"; then
log_message "Core files backup completed successfully." log_message "Core files backup completed successfully."
else else
log_message "ERROR: Core files backup failed." log_message "ERROR: Core files backup failed."
exit 1 exit 1
fi fi
# Finish logging # Logging end
log_message "Backup process finished." log_message "Backup process finished."

View File

@ -1,8 +1,5 @@
#!/bin/bash #!/bin/bash
# Load backup logic functions
source /home/jelastic/mb-backups/backup-logic.sh
# Validate input parameters # Validate input parameters
if [ "$#" -ne 2 ]; then if [ "$#" -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>" echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>"
@ -11,65 +8,54 @@ fi
# Assign command line arguments to variables # Assign command line arguments to variables
RESTIC_PASSWORD="$1" RESTIC_PASSWORD="$1"
ADDITIONAL_TAG="$2" CUSTOM_TAG="$2"
# Configuration # Configuration
APP_PATH='/var/www/webroot/ROOT' APP_PATH='/var/www/webroot/ROOT'
WP_CONFIG="${APP_PATH}/wp-config.php" WP_CONFIG="${APP_PATH}/wp-config.php"
backupPath='/mnt/backups' backupPath='/mnt/backups'
password_file="/etc/restic-password" password_file="/etc/restic-password"
LOG_DIR="/home/jelastic/mb-backups/logs" LOG_DIR="/home/litespeed/mb-backups/logs"
LOG_FILE="${LOG_DIR}/backup_database_$(date +'%Y-%m-%d').log" LOG_FILE="${LOG_DIR}/backup_database_$(date +'%Y-%m-%d').log"
# Extract database credentials from wp-config.php # Extract database credentials from wp-config.php
DB_NAME=$(grep "define( 'DB_NAME'" "$WP_CONFIG" | cut -d "'" -f 4) DB_NAME=$(grep "define('DB_NAME'" "$WP_CONFIG" | cut -d "'" -f 4)
DB_USER=$(grep "define( 'DB_USER'" "$WP_CONFIG" | cut -d "'" -f 4) DB_USER=$(grep "define('DB_USER'" "$WP_CONFIG" | cut -d "'" -f 4)
DB_PASSWORD=$(grep "define( 'DB_PASSWORD'" "$WP_CONFIG" | cut -d "'" -f 4) DB_PASSWORD=$(grep "define('DB_PASSWORD'" "$WP_CONFIG" | cut -d "'" -f 4)
DB_HOST=$(grep "define('DB_HOST'" "$WP_CONFIG" | cut -d "'" -f 4)
# Backup user and password (optional) DB_PORT=3306 # Default MySQL port
BACKUP_USER='wp_backup'
BACKUP_PASSWORD='gaQjveXl24Xo66w'
# Ensure log directory exists # Ensure log directory exists
mkdir -p "$LOG_DIR" mkdir -p "$LOG_DIR"
# Set Restic and MySQL environment variables # Set Restic and MySQL environment variables
export RESTIC_REPOSITORY="$backupPath" export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD # Updated to use the command line argument export RESTIC_PASSWORD
export MYSQL_PWD=$BACKUP_PASSWORD # Use the backup user's password for mysqldump export MYSQL_PWD="$DB_PASSWORD"
# Logging start # Logging start
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Database Backup with additional tag: $ADDITIONAL_TAG" | tee -a "$LOG_FILE" echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Database Backup for $DB_NAME with tags: wordpress_db, $CUSTOM_TAG" | tee -a "$LOG_FILE"
# Verify that the password file exists and matches the supplied password # Verify that the password file exists
if [ ! -f "$password_file" ]; then if [ ! -f "$password_file" ]; then
echo "ERROR: Password file not found at $password_file" | tee -a "$LOG_FILE" echo "ERROR: Password file not found at $password_file" | tee -a "$LOG_FILE"
exit 1 exit 1
fi fi
# Load and verify password from the file
stored_password=$(cat "$password_file")
if [ "$stored_password" != "$RESTIC_PASSWORD" ]; then
echo "ERROR: Password mismatch. Aborting backup." | tee -a "$LOG_FILE"
exit 1
fi
# Verify backup path exists # Verify backup path exists
if [ ! -d "$backupPath" ]; then if [ ! -d "$backupPath" ]; then
echo "ERROR: Backup path $backupPath does not exist." | tee -a "$LOG_FILE" echo "ERROR: Backup path $backupPath does not exist." | tee -a "$LOG_FILE"
exit 1 exit 1
fi fi
# Check repository accessibility and integrity # Perform database backup with both static and custom tags
check_backup_repo BACKUP_TAGS="wordpress_db,$CUSTOM_TAG"
if [ $? -ne 0 ]; then DUMP_FILE="${DB_NAME}_$(date +'%Y-%m-%d_%H-%M-%S').sql"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Backup repository check failed. Aborting backup." | tee -a "$LOG_FILE"
exit 1
fi
# Perform database backup with additional tag if mysqldump -h "$DB_HOST" -P "$DB_PORT" -u "$DB_USER" "$DB_NAME" > "/tmp/$DUMP_FILE" && \
if mysqldump -u "$BACKUP_USER" "$DB_NAME" | restic backup --stdin --stdin-filename "${DB_NAME}.sql" --tag wordpress_db --tag "$ADDITIONAL_TAG"; then restic backup --stdin --stdin-filename "$DUMP_FILE" --tag "$BACKUP_TAGS"; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Database backup completed successfully with tags: wordpress_db, $ADDITIONAL_TAG." | tee -a "$LOG_FILE" echo "[$(date +'%Y-%m-%d %H:%M:%S')] Database backup completed successfully with tags: $BACKUP_TAGS." | tee -a "$LOG_FILE"
rm -f "/tmp/$DUMP_FILE"
else else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Database backup failed." | tee -a "$LOG_FILE" echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Database backup failed." | tee -a "$LOG_FILE"
exit 1 exit 1

View File

@ -1,17 +1,17 @@
#!/bin/bash #!/bin/bash
# Load backup logic functions # Exit on error
source /home/jelastic/mb-backups/backup-logic.sh set -e
# Validate input parameters # Validate input parameters
if [ "$#" -ne 2 ]; then if [ "$#" -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>" echo "Usage: $0 <RESTIC_PASSWORD> <CUSTOM_TAG>"
exit 1 exit 1
fi fi
# Assign command line arguments to variables # Assign arguments to variables
RESTIC_PASSWORD="$1" RESTIC_PASSWORD="$1"
ADDITIONAL_TAG="$2" CUSTOM_TAG="$2"
# Configuration # Configuration
APP_PATH='/var/www/webroot/ROOT' APP_PATH='/var/www/webroot/ROOT'
@ -28,38 +28,34 @@ mkdir -p "$LOG_DIR"
export RESTIC_REPOSITORY="$backupPath" export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD export RESTIC_PASSWORD
# Logging start # Start logging
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Media and Themes Backup" | tee -a "$LOG_FILE" echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Media Backup with tags: media_themes, $CUSTOM_TAG" | tee -a "$LOG_FILE"
# Verify that the password file exists and matches the supplied password # Verify password file exists
if [ ! -f "$password_file" ]; then if [ ! -f "$password_file" ]; then
echo "ERROR: Password file not found at $password_file" | tee -a "$LOG_FILE" echo "ERROR: Password file not found at $password_file" | tee -a "$LOG_FILE"
exit 1 exit 1
fi fi
# Load and verify password from the file # Verify repository access
stored_password=$(cat "$password_file") if ! restic snapshots > /dev/null 2>&1; then
if [ "$stored_password" != "$RESTIC_PASSWORD" ]; then echo "ERROR: Unable to access the restic repository. Aborting backup." | tee -a "$LOG_FILE"
echo "ERROR: Password mismatch. Aborting backup." | tee -a "$LOG_FILE"
exit 1 exit 1
fi fi
# Check repository accessibility and integrity # Perform the backup
check_backup_repo
if [ $? -ne 0 ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Backup repository check failed. Aborting backup." | tee -a "$LOG_FILE"
exit 1
fi
# Perform media backup with additional tag
for path in "${includePaths[@]}"; do for path in "${includePaths[@]}"; do
if restic backup "$path" --tag media_themes --tag "$ADDITIONAL_TAG"; then if restic backup "$path" \
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup completed successfully for $path with tags: media_themes, $ADDITIONAL_TAG." | tee -a "$LOG_FILE" --tag media_themes \
--tag "$CUSTOM_TAG" \
--force \
--option b2.connections=4; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup completed successfully for $path with tags: media_themes, $CUSTOM_TAG." | tee -a "$LOG_FILE"
else else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Backup failed for $path." | tee -a "$LOG_FILE" echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Backup failed for $path." | tee -a "$LOG_FILE"
exit 1 exit 1
fi fi
done done
# Logging end # End logging
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Media and Themes Backup process finished." | tee -a "$LOG_FILE" echo "[$(date +'%Y-%m-%d %H:%M:%S')] Media Backup process finished." | tee -a "$LOG_FILE"