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

View File

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

View File

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