Compare commits

...

2 Commits

Author SHA1 Message Date
Anthony 1291e4a255 improvements 2025-01-06 23:33:56 +08:00
Anthony 716fcce79c Fix trap error 2025-01-06 23:18:14 +08:00
4 changed files with 162 additions and 358 deletions

View File

@ -172,7 +172,7 @@ actions:
backupnow:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/backup_all.sh "backup_now"
commands: bash /home/jelastic/mb-backups/backup-logic.sh "backup"
- return:
type: info
message: "${response.out}"

View File

@ -1,261 +1,107 @@
#!/bin/bash
BASE_URL=$2
BACKUP_TYPE=$3
BACKUP_LOG_FILE=$4
ENV_NAME=$5
BACKUP_COUNT=$6
APP_PATH=$7
USER_SESSION=$8
USER_EMAIL=$9
BACKUP_PATH=${10}
set -e # Exit on error
trap 'echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: An error occurred. Exiting." | tee -a "$LOG_FILE"' ERR
# Ensure restic is installed
# 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
dnf install -y epel-release && dnf install -y restic
elif which yum &>/dev/null; then
yum-config-manager --add-repo https://copr.fedorainfracloud.org/coprs/copart/restic/repo/epel-7/copart-restic-epel-7.repo
yum-config-manager --enable copart-restic
yum -y install restic
yum-config-manager --disable copart-restic
fi
fi
# Function definitions remain the same, but adjustments are made to use $BACKUP_PATH
function update_restic(){
restic self-update 2>&1;
}
function check_backup_repo() {
local backup_repo_path="/mnt/backups/${ENV_NAME}"
local password_file="/etc/restic-password"
local backup_log_file="/var/log/backup_addon.log"
# Ensure the backup repository directory exists
[ -d "${backup_repo_path}" ] || mkdir -p "${backup_repo_path}"
# Generate a new 10-character random password if password file does not exist or is empty
if [ ! -f "$password_file" ] || [ -z "$(cat "$password_file")" ]; then
local new_password=$(head /dev/urandom | tr -dc A-Za-z0-9 | head -c 10)
echo "$new_password" > "$password_file"
echo "$(date) Password file created with a new random password." | tee -a "${backup_log_file}"
fi
# Load the password from the file for use with Restic
export RESTIC_PASSWORD=$(cat "$password_file")
export RESTIC_REPOSITORY="$backup_repo_path"
export FILES_COUNT=$(find "${backup_repo_path}" -mindepth 1 | wc -l)
# Check if the repository is accessible with the current password
if [ "${FILES_COUNT}" -gt 0 ]; then
echo "$(date) ${ENV_NAME} Checking the backup repository integrity and consistency" | tee -a "${backup_log_file}"
if ! GOGC=20 restic -r "${backup_repo_path}" snapshots > /dev/null 2>&1; then
echo "$(date) ${ENV_NAME} ERROR: Repository exists but password does not match." | tee -a "${backup_log_file}"
echo "$(date) Reinitializing the repository with a new password." | tee -a "${backup_log_file}"
rm -rf "${backup_repo_path}"/*
GOGC=20 restic init -r "${backup_repo_path}"
echo "$(date) Repository reinitialized with a new password." | tee -a "${backup_log_file}"
yum install -y restic
else
# Remove stale lock if exists
if [[ $(ls -A "${backup_repo_path}/locks") ]]; then
echo "$(date) ${ENV_NAME} Backup repository has a stale lock, removing" | tee -a "${backup_log_file}"
GOGC=20 restic -r "${backup_repo_path}" unlock
sendEmailNotification
fi
# Run the repository check command
if ! GOGC=20 restic -q -r "${backup_repo_path}" check --read-data-subset=5%; then
echo "Backup repository integrity check failed." | tee -a "${backup_log_file}"
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
echo "$(date) ${ENV_NAME} Initializing new backup repository" | tee -a "${backup_log_file}"
GOGC=20 restic init -r "${backup_repo_path}"
log_message "Restic repository validated."
fi
}
# Backup a given path
perform_backup() {
local path="$1"
local tag="$2"
local exclude="$3"
function sendEmailNotification() {
if [ -e "/usr/lib/jelastic/modules/api.module" ]; then
[ -e "/var/run/jem.pid" ] && return 0;
CURRENT_PLATFORM_MAJOR_VERSION=$(jem api apicall -s --connect-timeout 3 --max-time 15 [API_DOMAIN]/1.0/statistic/system/rest/getversion 2>/dev/null | jq .version | grep -o [0-9.]* | awk -F . '{print $1}')
if [ "${CURRENT_PLATFORM_MAJOR_VERSION}" -ge "7" ]; then
echo "$(date) ${ENV_NAME} Sending e-mail notification about removing the stale lock" | tee -a "$BACKUP_LOG_FILE"
SUBJECT="Stale lock is removed on ${BACKUP_PATH}/${ENV_NAME} backup repo"
BODY="Please pay attention to ${BACKUP_PATH}/${ENV_NAME} backup repo because the stale lock left from previous operation is removed during the integrity check and backup rotation. Manual check of backup repo integrity and consistency is highly desired."
jem api apicall -s --connect-timeout 3 --max-time 15 [API_DOMAIN]/1.0/message/email/rest/send --data-urlencode "session=$USER_SESSION" --data-urlencode "to=$USER_EMAIL" --data-urlencode "subject=$SUBJECT" --data-urlencode "body=$BODY"
if [[ $? != 0 ]]; then
echo "$(date) ${ENV_NAME} Sending of e-mail notification failed" | tee -a "$BACKUP_LOG_FILE"
else
echo "$(date) ${ENV_NAME} E-mail notification is sent successfully" | tee -a "$BACKUP_LOG_FILE"
fi
elif [ -z "${CURRENT_PLATFORM_MAJOR_VERSION}" ]; then
echo "$(date) ${ENV_NAME} Error when checking the platform version" | tee -a "$BACKUP_LOG_FILE"
else
echo "$(date) ${ENV_NAME} Email notification is not sent because this functionality is unavailable for current platform version." | tee -a "$BACKUP_LOG_FILE"
fi
else
echo "$(date) ${ENV_NAME} Email notification is not sent because this functionality is unavailable for current platform version." | tee -a "$BACKUP_LOG_FILE"
fi
log_message "Backing up $path with tag $tag..."
restic -r "$BACKUP_REPO_PATH" backup "$path" --tag "$tag" $exclude
log_message "Backup for $path completed."
}
function rotate_snapshots(){
local backup_repo_path="/mnt/backups/${ENV_NAME}"
echo "$(date) ${ENV_NAME} Rotating snapshots by keeping the last ${BACKUP_COUNT}" | tee -a "${BACKUP_LOG_FILE}"
if [[ $(ls -A "${backup_repo_path}/locks") ]]; then
echo "$(date) ${ENV_NAME} Backup repository has a stale lock, removing" | tee -a "${BACKUP_LOG_FILE}"
GOGC=20 RESTIC_PASSWORD="${ENV_NAME}" restic -r "${backup_repo_path}" unlock
sendEmailNotification
fi
if ! GOGC=20 RESTIC_COMPRESSION=off RESTIC_PACK_SIZE=8 RESTIC_PASSWORD="${ENV_NAME}" restic forget -q -r "${backup_repo_path}" --keep-last "${BACKUP_COUNT}" --prune | tee -a "${BACKUP_LOG_FILE}"; then
echo "Backup rotation failed." | tee -a "${BACKUP_LOG_FILE}"
exit 1
fi
# 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."
}
function create_snapshot(){
local backup_repo_path="/mnt/backups/${ENV_NAME}"
DUMP_NAME=$(date "+%F_%H%M%S_%Z")
echo "$(date) ${ENV_NAME} Begin uploading the ${DUMP_NAME} snapshot to backup storage" | tee -a "${BACKUP_LOG_FILE}"
# Backup database
backup_database() {
log_message "Starting database backup..."
local db_dump
db_dump=$(mktemp)
if ! GOGC=20 RESTIC_COMPRESSION=off RESTIC_PACK_SIZE=8 RESTIC_READ_CONCURRENCY=8 RESTIC_PASSWORD="${ENV_NAME}" restic backup -q -r "${backup_repo_path}" --tag "${DUMP_NAME} ${BACKUP_ADDON_COMMIT_ID} ${BACKUP_TYPE}" "${APP_PATH}" ~/wp_db_backup.sql | tee -a "${BACKUP_LOG_FILE}"; then
echo "Backup snapshot creation failed." | tee -a "${BACKUP_LOG_FILE}"
exit 1
fi
echo "$(date) ${ENV_NAME} End uploading the ${DUMP_NAME} snapshot to backup storage" | tee -a "${BACKUP_LOG_FILE}"
}
function backup_database() {
local backup_repo_path="/mnt/backups/${ENV_NAME}/db"
local log_file="/home/litespeed/mbmanager/logs/${ENV_NAME}_db_backup.log"
# Ensure the backup and log directories exist
mkdir -p "${backup_repo_path}"
mkdir -p "$(dirname "${log_file}")"
echo "$(date) ${ENV_NAME} Starting database backup..." | tee -a "${log_file}"
# Extract database credentials from wp-config.php
DB_NAME=$(grep DB_NAME /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4)
DB_USER=$(grep DB_USER /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4)
DB_PASSWORD=$(grep DB_PASSWORD /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4)
DB_HOST=$(grep DB_HOST /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4 | awk -F':' '{print $1}')
DB_PORT=$(grep DB_HOST /var/www/webroot/ROOT/wp-config.php | cut -d "'" -f 4 | awk -F':' '{print $2}' | sed 's/)//')
DB_PORT=${DB_PORT:-3306}
# Determine the correct --column-statistics option based on MySQL or MariaDB version
local column_statistics_option=""
SERVER_VERSION=$(mysql -h "${DB_HOST}" -u "${DB_USER}" -p"${DB_PASSWORD}" -e 'SELECT VERSION();' -s -N)
if [[ "${SERVER_VERSION}" =~ ^5\.7|^8\.0|MariaDB ]]; then
column_statistics_option="--column-statistics=0"
fi
# Create a database dump
local dump_file="${backup_repo_path}/${ENV_NAME}_$(date "+%Y-%m-%d_%H-%M-%S").sql"
mysqldump -h "${DB_HOST}" -P "${DB_PORT}" -u "${DB_USER}" -p"${DB_PASSWORD}" ${column_statistics_option} --force --single-transaction --quote-names --opt "${DB_NAME}" > "${dump_file}"
if [ $? -ne 0 ]; then
echo "$(date) ${ENV_NAME} Database backup process failed." | tee -a "${log_file}"
exit 1
fi
# Backup the dump file using Restic
restic -r "${backup_repo_path}" backup "${dump_file}" --tag "db" --host "${ENV_NAME}"
if [ $? -ne 0 ]; then
echo "$(date) ${ENV_NAME} Restic backup process failed." | tee -a "${log_file}"
exit 1
fi
echo "$(date) ${ENV_NAME} Database backup completed successfully." | tee -a "${log_file}"
# Optionally, remove the dump file after successful backup
rm -f "${dump_file}"
}
function backup_wp_core() {
echo "Starting backup of WordPress core files excluding uploads directory..."
# Define the path to the WordPress installation and the backup tag
local wp_path="$APP_PATH"
local backup_tag="wp-core-$(date "+%F_%H%M%S_%Z")"
# Exclude the uploads directory using the --exclude option
GOGC=20 RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "$backupPath" backup \
--tag "$backup_tag" \
--exclude="$wp_path/wp-content/uploads" \
"$wp_path"
echo "WordPress core files backup completed."
}
function backup_uploads() {
echo "Starting backup of WordPress uploads directory..."
# Define the path to the uploads directory and the backup tag
local uploads_path="$APP_PATH/wp-content/uploads"
local backup_tag="wp-uploads-$(date "+%F_%H%M%S_%Z")"
# Perform the backup
GOGC=20 RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "$backupPath" backup \
--tag "$backup_tag" \
"$uploads_path"
echo "WordPress uploads directory backup completed."
}
function backup_database() {
echo "Starting backup of WordPress database..."
# Define the backup tag and the path for the temporary SQL dump
local backup_tag="wp-db-$(date "+%F_%H%M%S_%Z")"
local sql_dump_path="/tmp/wp_db_backup.sql"
# Create a database dump
mysqldump -h "$DB_HOST" -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME" > "$sql_dump_path"
# Perform the backup
GOGC=20 RESTIC_PASSWORD="$RESTIC_PASSWORD" restic -r "$backupPath" backup \
--tag "$backup_tag" \
"$sql_dump_path"
# Remove the temporary SQL dump file
rm -f "$sql_dump_path"
echo "WordPress database backup completed."
# 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)
backup_wp_core
backup_uploads
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
;;
backup_wp_core)
backup_wp_core
;;
backup_uploads)
backup_uploads
;;
backup_database)
backup_database
;;
check_backup_repo)
check_repo)
validate_inputs
check_backup_repo
;;
rotate_snapshots)
rotate_snapshots
;;
create_snapshot)
create_snapshot
;;
update_restic)
update_restic
rotate_snapshots "$2"
;;
*)
echo "Usage: $0 {backup|backup_wp_core|backup_uploads|backup_database|check_backup_repo|rotate_snapshots|create_snapshot|update_restic}"
exit 2
echo "Usage: $0 {backup|check_repo|rotate_snapshots}"
exit 1
;;
esac
exit $?

View File

@ -2,6 +2,12 @@
# Enable error handling
set -e
# 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
@ -9,50 +15,47 @@ source /home/jelastic/mb-backups/backup-logic.sh
# Configuration
password_file="/etc/restic-password"
APP_PATH='/var/www/webroot/ROOT'
APP_PATH="/var/www/webroot/ROOT"
WP_CONFIG="${APP_PATH}/wp-config.php"
backupPath='/mnt/backups'
LOG_DIR="/home/jelastic/mb-backups/logs"
backupPath="/mnt/backups"
TEMP_DIR="/tmp/wp_backup_tmp"
MAX_BACKUP_SIZE=$((10 * 1024 * 1024 * 1024)) # 10GB
# Database configuration
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='wp_backup'
BACKUP_PASSWORD='gaQjveXl24Xo66w'
# Ensure directories exist
mkdir -p "$LOG_DIR"
mkdir -p "$TEMP_DIR"
# Cleanup function
cleanup() {
local exit_code=$?
rm -rf "${TEMP_DIR}"/*
if [ $exit_code -ne 0 ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup failed with exit code $exit_code" | tee -a "$LOG_DIR/backup_error.log"
# Function: Ensure required commands are available
validate_dependencies() {
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
}
trap cleanup EXIT
# Check backup size
# Function: Initialize logging
log_message() {
local log_file="$1"
local message="$2"
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=$(du -sb "$path" | cut -f1)
local size
size=$(du -sb "$path" | cut -f1)
if [ "$size" -gt "$MAX_BACKUP_SIZE" ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Warning: Backup size exceeds 10GB for $path" | tee -a "$LOG_DIR/backup_warning.log"
log_message "$LOG_DIR/backup_warning.log" "Warning: Backup size exceeds 10GB for $path"
return 1
fi
return 0
}
# Verify backup
# Function: Verify backup
verify_backup() {
local tag="$1"
local latest_snapshot=$(restic snapshots --latest 1 --tag "$tag" --json | jq -r '.[0].id')
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 $?
@ -60,30 +63,20 @@ verify_backup() {
return 1
}
# Initialize backup
# Function: Initialize backup
initialize_backup() {
if [ ! -f "$password_file" ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Password file not found at $password_file" | tee -a "$LOG_DIR/backup_error.log"
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"
# Check repository
check_backup_repo() {
restic snapshots > /dev/null 2>&1
if [ $? -ne 0 ]; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Backup repository is not accessible" | tee -a "$LOG_DIR/backup_error.log"
exit 1
fi
}
check_backup_repo
# Backup functions
# Function: Backup core files
backup_core_files() {
local log_file="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Core Files Backup" | tee -a "$log_file"
log_message "$log_file" "Starting Core Files Backup"
check_backup_size "$APP_PATH" || return 1
@ -95,72 +88,22 @@ backup_core_files() {
if restic backup $excludeOptions "$APP_PATH" --tag core_files --tag full_backup; then
verify_backup "core_files" || return 1
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Core files backup completed successfully" | tee -a "$log_file"
log_message "$log_file" "Core files backup completed successfully"
return 0
fi
return 1
}
backup_media_themes() {
local log_file="${LOG_DIR}/backup_media_themes_$(date +'%Y-%m-%d').log"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Media and Themes Backup" | tee -a "$log_file"
local includePaths=("$APP_PATH/wp-content/uploads")
for path in "${includePaths[@]}"; do
check_backup_size "$path" || continue
if restic backup "$path" --tag media_themes --tag full_backup; then
verify_backup "media_themes" || return 1
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup completed successfully for $path" | tee -a "$log_file"
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Backup failed for $path" | tee -a "$log_file"
return 1
fi
done
}
backup_database() {
local log_file="${LOG_DIR}/backup_database_$(date +'%Y-%m-%d').log"
local temp_dump="${TEMP_DIR}/wp_backup_${DB_NAME}_$(date +'%Y%m%d').sql"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Database Backup" | tee -a "$log_file"
if ! mysqldump -u "$BACKUP_USER" --password="$BACKUP_PASSWORD" "$DB_NAME" > "$temp_dump" 2>> "$log_file"; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Database dump failed" | tee -a "$log_file"
return 1
fi
check_backup_size "$temp_dump" || return 1
if restic backup "$temp_dump" --tag wordpress_db --tag full_backup; then
verify_backup "wordpress_db" || return 1
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Database backup completed successfully" | tee -a "$log_file"
return 0
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Database backup failed" | tee -a "$log_file"
return 1
fi
}
cleanup_old_backups() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting backup rotation" | tee -a "$LOG_DIR/backup_rotation.log"
restic forget \
--keep-daily 7 \
--keep-weekly 4 \
--keep-monthly 3 \
--prune \
--tag full_backup \
2>> "$LOG_DIR/backup_rotation.log"
}
# Main execution
main() {
validate_dependencies
initialize_backup
local start_time=$(date +%s)
local backup_date=$(date +'%Y-%m-%d_%H-%M-%S')
local main_log="${LOG_DIR}/full_backup_${backup_date}.log"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting full backup process" | tee -a "$main_log"
log_message "$main_log" "Starting full backup process"
backup_core_files || exit 1
backup_media_themes || exit 1
@ -171,7 +114,7 @@ main() {
local end_time=$(date +%s)
local duration=$((end_time - start_time))
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Full backup completed in $duration seconds" | tee -a "$main_log"
log_message "$main_log" "Full backup completed in $duration seconds"
}
# Argument handling

View File

@ -1,8 +1,34 @@
#!/bin/bash
# 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
# Function: Log messages
log_message() {
local message="$1"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "$LOG_FILE"
}
# Function: Validate the password
validate_password() {
local password="$1"
if [ ! -f "$password_file" ]; then
log_message "ERROR: Password file not found at $password_file"
exit 1
fi
local stored_password
stored_password=$(cat "$password_file")
if [ "$stored_password" != "$password" ]; then
log_message "ERROR: Password mismatch. Aborting backup."
exit 1
fi
}
# Check for required arguments
if [ $# -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>"
@ -14,8 +40,8 @@ RESTIC_PASSWORD="$1"
ADDITIONAL_TAG="$2"
# Configuration
APP_PATH='/var/www/webroot/ROOT'
backupPath='/mnt/backups'
APP_PATH="/var/www/webroot/ROOT"
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"
@ -30,42 +56,31 @@ mkdir -p "$LOG_DIR"
export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD
# Verify that the password file exists and matches the supplied password
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
# Validate the password
validate_password "$RESTIC_PASSWORD"
# 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"
if ! check_backup_repo; then
log_message "ERROR: Backup repository check failed. Aborting backup."
exit 1
fi
# Logging start
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Core Files Backup with tags: core_files, $ADDITIONAL_TAG" | tee -a "$LOG_FILE"
# Start logging
log_message "Starting Core Files Backup with tags: core_files, $ADDITIONAL_TAG"
# Building exclude options
# Build exclude options
excludeOptions=""
for path in "${excludePaths[@]}"; do
excludeOptions+="--exclude $path "
done
# Perform backup with both tags
# Perform backup
if restic backup $excludeOptions "$APP_PATH" --tag core_files --tag "$ADDITIONAL_TAG"; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Core files backup completed successfully." | tee -a "$LOG_FILE"
log_message "Core files backup completed successfully."
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Core files backup failed." | tee -a "$LOG_FILE"
log_message "ERROR: Core files backup failed."
exit 1
fi
# Logging end
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup process finished." | tee -a "$LOG_FILE"
# Finish logging
log_message "Backup process finished."