Added Shell Scripts

main
Anthony 2024-09-19 00:53:20 +08:00
parent b6aab4e747
commit 15087e1bdf
9 changed files with 530 additions and 86 deletions

View File

@ -13,37 +13,99 @@ targetNodes:
- llsmp - llsmp
globals: globals:
scriptSufix: mb-backup-manager
envName: "${env.name}" envName: "${env.name}"
log_file: "/var/log/backup_addon.log"
onInstall: onInstall:
- checkApplication
- checkAddons - checkAddons
- installRestic - installRestic
settings:
backupSettings:
submitUnchanged: true
fields:
- name: blabel
caption: Backup Label
type: string
restoreSettings:
submitUnchanged: true
fields:
- name: snapshotId
caption: Snapshot ID
type: string
buttons: buttons:
- caption: Backup Now - caption: Backup Now
action: backup action: backupnow
loadingText: Backing up... loadingText: Backing up...
confirmText: Do you want to initiate the backup process? confirmText: Do you want to initiate the backup process?
successText: The backup process has been finished successfully. successText: The backup process has been finished successfully.
menu: menu:
- caption: Core File Backup
confirmText: Backup Core Files? confirmText: Backup Core Files?
loadingText: Backing up Core Files on your site loadingText: Backing up Core Files...
action: backupcore action: backupcore
caption: Backup Core Only successText: Core Files Backup Completed Successfully
successText: Backup Core Files Completed Successfully settings: backupSettings
title: Backup Core Files title: Backup Core Files
submitButtonText: Backup Now submitButtonText: Backup Now
- caption: Media Backup
confirmText: Backup Media Files?
loadingText: Backing up Media Files...
action: backupmedia
successText: Media Files Backup Completed Successfully
settings: backupSettings
title: Backup Media Files
submitButtonText: Backup Now
- caption: Database Backup
confirmText: Backup Database?
loadingText: Backing up Database...
action: backupdb
successText: Database Backup Completed Successfully
settings: backupSettings
title: Backup Database
submitButtonText: Backup Now
- caption: Restore
confirmText: Restore from Snapshot?
loadingText: Restoring...
action: restore
successText: Restore Completed Successfully
settings: restoreSettings
title: Restore from Snapshot
submitButtonText: Restore Now
- caption: View Full Backups
action: viewFullBackups
confirmText: Are you sure you want to view full backups?
successText: Full backups listed successfully.
- caption: View Core Backups
action: viewCoreBackups
confirmText: Are you sure you want to view core backups?
successText: Core backups listed successfully.
- caption: View Media Backups
action: viewMediaBackups
confirmText: Are you sure you want to view media backups?
successText: Media backups listed successfully.
- caption: View Database Backups
action: viewDatabaseBackups
confirmText: Are you sure you want to view database backups?
successText: Database backups listed successfully.
- caption: Auto Backup Sched
action: checkBackupSched
confirmText: Check the schedules for automated backups?
successText: Backup Scheds.
onUninstall: onUninstall:
- callScript: uninstall
- removeScript - removeScript
onBeforeDelete: onBeforeDelete:
- callScript: uninstall
- removeScript - removeScript
onAfterRedeployContainer[cp]: onAfterRedeployContainer[cp]:
@ -58,18 +120,85 @@ onAfterClone:
backupCount: "5" backupCount: "5"
actions: actions:
checkApplication:
- cmd[${nodes.cp.master.id}]: |-
[ -f /root/check_app.sh ] && rm -f /root/check_app.sh;
wget -O /root/check_app.sh ${baseUrl}/scripts/check_app.sh;
bash /root/check_app.sh;
user: root
- if (response.out == "Non-supported"):
- stopEvent:
type: warning
message: Deployed application is not supported by Backup add-on.
backupnow: backupnow:
message: BACKUP ALL DONE - cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/backup_all.sh "${globals.envName}"
- return:
type: info
message: "${response.out}"
backupcore:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/backup_core_files.sh "${globals.envName}" "${settings.blabel}"
- return:
type: info
message: "${response.out}"
backupmedia:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/backup_media.sh "${globals.envName}" "${settings.blabel}"
- return:
type: info
message: "${response.out}"
backupdb:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/backup_database.sh "${globals.envName}" "${settings.blabel}"
- return:
type: info
message: "${response.out}"
restore:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/restore_backup_direct.sh "${settings.snapshotId}" "${globals.envName}"
- return:
type: info
message: "${response.out}"
viewFullBackups:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/view_snapshots.sh main_backup
- return:
type: info
message: "${response.out}"
viewCoreBackups:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/view_snapshots.sh core_files
- return:
type: info
message: "${response.out}"
viewMediaBackups:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/view_snapshots.sh media_themes
- return:
type: info
message: "${response.out}"
viewDatabaseBackups:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/view_snapshots.sh wordpress_db
- return:
type: info
message: "${response.out}"
checkBackupSched:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/check_sched.sh
- return:
type: info
message: "${response.out}"
checkAddons: checkAddons:
- script: |- - script: |-
@ -95,7 +224,7 @@ actions:
- stopEvent: - stopEvent:
type: warning type: warning
message: Database backup add-on is already installed on ${env.name}. Backup addon installation is not possible. message: Database backup add-on is already installed on ${env.name}. Backup addon installation is not possible.
installRestic: installRestic:
cmd [cp]: |- cmd [cp]: |-
if which dnf >/dev/null 2>&1; then if which dnf >/dev/null 2>&1; then
@ -115,68 +244,4 @@ actions:
copytruncate copytruncate
}" > /etc/logrotate.d/backup-addon }" > /etc/logrotate.d/backup-addon
fi fi
user: root user: root
installScript:
- removeScript
- script: ${baseUrl}/scripts/create-backup-main-script.js?_r=${fn.random}
params:
scriptName: ${env.envName}-${globals.scriptSufix}
baseUrl: ${baseUrl}
cronTime: '0 * * * *'
backupCount: '5'
userId: ${env.uid}
callScript:
script: |-
var resp = api.dev.scripting.Eval(appid, session, '${env.envName}-${globals.scriptSufix}', {action:"${this}"});
if (resp.result === 1702 && "${this}" == "uninstall") {
return { result: 0, out: "script not found" };
} else {
return resp.response || resp;
}
removeScript:
script: |-
var resp = api.dev.scripting.GetScript(appid, session, '${env.envName}-${globals.scriptSufix}');
if (resp.result === 0) {
var resp = api.dev.scripting.DeleteScript(appid, session, '${env.envName}-${globals.scriptSufix}');
return resp.response || resp;
}
return { result: 0 };
backup:
- callScript: backup
backupcore:
- cmd[cp]: |-
# Ensure the /mbmanager directory exists
mkdir -p /mbmanager
# Download the backup-logic.sh script if it doesn't already exist
if [ ! -f /mbmanager/backup-logic.sh ]; then
echo "Downloading backup-logic.sh script..."
wget -O /mbmanager/backup-logic.sh '${baseUrl}/scripts/backup-logic.sh' || curl -o /mbmanager/backup-logic.sh '${baseUrl}/scripts/backup-logic.sh'
fi
# Make the script executable
chmod +x /mbmanager/backup-logic.sh
# Export necessary environment variables
export ENV_NAME='${globals.envName}'
export BACKUP_LOG_FILE='${globals.log_file}'
export BACKUP_TYPE='manual'
export APP_PATH='/var/www/webroot/ROOT'
export RESTIC_PASSWORD='your_restic_repository_password' # Replace with your actual password
export backupPath='/mnt/backups'
# Execute the backup_wp_core function from the script
/bin/bash /mbmanager/backup-logic.sh backup_wp_core
user: root
restore:
- cmd[cp]: |-
echo "${settings.backupedEnvName}" > /root/.backupedenv
echo "${settings.backupDir}" > /root/.backupid
user: root
- callScript: restore

View File

@ -0,0 +1,76 @@
#!/bin/bash
# Check if Restic password was supplied
if [ "$#" -ne 1 ]; then
echo "Usage: $0 RESTIC_PASSWORD"
exit 1
fi
# Configuration
RESTIC_PASSWORD="$1"
APP_PATH='/var/www/webroot/ROOT'
WP_CONFIG="${APP_PATH}/wp-config.php"
backupPath='/mnt/backups'
LOG_DIR="/home/jelastic/mb-backups/logs"
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'
export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Backup functions
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"
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
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Core files backup completed successfully." | tee -a "$log_file"
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Core files backup failed." | tee -a "$log_file"
fi
}
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
if restic backup "$path" --tag media_themes --tag full_backup; then
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"
fi
done
}
backup_database() {
local log_file="${LOG_DIR}/backup_database_$(date +'%Y-%m-%d').log"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Database Backup" | tee -a "$log_file"
# Correctly pass the password to mysqldump
if mysqldump -u "$BACKUP_USER" --password="$BACKUP_PASSWORD" "$DB_NAME" | restic backup --stdin --tag wordpress_db --tag full_backup; then
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Database backup completed successfully." | tee -a "$log_file"
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Database backup failed." | tee -a "$log_file"
fi
}
# Execute backup functions
backup_core_files
backup_media_themes
backup_database

View File

@ -0,0 +1,47 @@
#!/bin/bash
# Check for required arguments
if [ $# -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>"
exit 1
fi
# Assign arguments to variables
RESTIC_PASSWORD="$1"
ADDITIONAL_TAG="$2"
# Configuration
APP_PATH='/var/www/webroot/ROOT'
backupPath='/mnt/backups'
LOG_DIR="/home/jelastic/mb-backups/logs"
LOG_FILE="${LOG_DIR}/backup_core_files_$(date +'%Y-%m-%d').log"
excludePaths=(
"$APP_PATH/wp-content/uploads"
)
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Set Restic environment variables
export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD
# Logging start
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Core Files Backup with tags: core_files, $ADDITIONAL_TAG" | tee -a "$LOG_FILE"
# Building exclude options
excludeOptions=""
for path in "${excludePaths[@]}"; do
excludeOptions+="--exclude $path "
done
# Perform backup with both tags
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"
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Core files backup failed." | tee -a "$LOG_FILE"
fi
# Logging end
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Backup process finished." | tee -a "$LOG_FILE"

View File

@ -0,0 +1,46 @@
#!/bin/bash
# Validate input parameters
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>"
exit 1
fi
# Assign command line arguments to variables
RESTIC_PASSWORD="$1"
ADDITIONAL_TAG="$2"
# Configuration
APP_PATH='/var/www/webroot/ROOT'
WP_CONFIG="${APP_PATH}/wp-config.php"
backupPath='/mnt/backups'
LOG_DIR="/home/jelastic/mb-backups/logs"
LOG_FILE="${LOG_DIR}/backup_database_$(date +'%Y-%m-%d').log"
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
BACKUP_USER='wp_backup'
BACKUP_PASSWORD='gaQjveXl24Xo66w'
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Set Restic 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
# Logging start
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Database Backup with additional tag: $ADDITIONAL_TAG" | tee -a "$LOG_FILE"
# Perform backup with additional tag
if mysqldump -u "$BACKUP_USER" "$DB_NAME" | restic backup --stdin --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"
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Database backup failed." | tee -a "$LOG_FILE"
fi
# Logging end
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Database Backup process finished." | tee -a "$LOG_FILE"

View File

@ -0,0 +1,40 @@
#!/bin/bash
# Check for required arguments
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <RESTIC_PASSWORD> <ADDITIONAL_TAG>"
exit 1
fi
# Assign arguments to variables
RESTIC_PASSWORD="$1"
ADDITIONAL_TAG="$2"
# Configuration
APP_PATH='/var/www/webroot/ROOT'
backupPath='/mnt/backups'
LOG_DIR="/home/jelastic/mb-backups/logs/manual/media"
LOG_FILE="${LOG_DIR}/backup_media_$(date +'%Y-%m-%d').log"
includePaths=("$APP_PATH/wp-content/uploads")
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Set Restic environment variables
export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD
# Logging start
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Starting Media Backup" | tee -a "$LOG_FILE"
# Perform backup with additional tag
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"
else
echo "[$(date +'%Y-%m-%d %H:%M:%S')] ERROR: Backup failed for $path." | tee -a "$LOG_FILE"
fi
done
# Logging end
echo "[$(date +'%Y-%m-%d %H:%M:%S')] Media and Themes Backup process finished." | tee -a "$LOG_FILE"

View File

@ -0,0 +1,16 @@
#!/bin/bash
# Script to check for scheduled cron jobs for /home/litespeed/mb-backups/backup_all.sh
# Define the script path to check in cron jobs
SCRIPT_PATH="/home/litespeed/mb-backups/backup_all.sh"
# Use crontab -l to list all cron jobs, then grep to check for the specific script path
CRON_JOB=$(crontab -l | grep -F "$SCRIPT_PATH")
if [ -z "$CRON_JOB" ]; then
echo "Automated Backups is not enabled."
else
echo "$CRON_JOB" | awk '{print "Schedule: " $1, $2, $3, $4, $5}'
# Note: This script stops here, providing the schedule. Computing the next run time in a human-readable format is not straightforward in bash without external tools.
fi

View File

@ -0,0 +1,62 @@
#!/bin/bash
# Configuration
BACKUP_SCRIPT="/home/litespeed/mb-backups/backup_all.sh"
LOG_DIR="/home/litespeed/mb-backups/logs/auto"
ACTION_LOG_FILE="${LOG_DIR}/schedule_actions.log"
BACKUP_LOG_PREFIX="${LOG_DIR}/backup_"
# Ensure the log directory exists
mkdir -p "$LOG_DIR"
log_action() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> "$ACTION_LOG_FILE"
}
# Function to add or update the cron job with dynamic logging
add_update_cron_job() {
# Verify Restic password is provided
if [ -z "$3" ]; then
echo "Restic password is required."
log_action "Attempted to add/update a schedule without providing a Restic password."
exit 1
fi
# Prepare the cron command to include dynamic date in the backup log filename
CMD="RESTIC_PASSWORD=\"$3\" $BACKUP_SCRIPT > \"${BACKUP_LOG_PREFIX}\$(date +\\%Y-\\%m-\\%d_\\%H-\\%M-\\%S).log\" 2>&1"
# Add or update the cron job
(crontab -l | grep -v "$BACKUP_SCRIPT" 2>/dev/null; echo "$2 $CMD") | crontab -
local update_msg="Backup schedule updated to: $2"
echo "$update_msg"
log_action "$update_msg"
}
# Function to remove the cron job
remove_cron_job() {
crontab -l | grep -v "$BACKUP_SCRIPT" | crontab -
local remove_msg="Backup schedule removed."
echo "$remove_msg"
log_action "$remove_msg"
}
# Main logic to add, update, or remove the cron job based on user input
case $1 in
add|update)
if [ "$#" -ne 3 ]; then
echo "Usage for add/update: $0 {add|update} 'schedule' 'restic_password'"
echo "Example: $0 add '0 1 * * *' 'secret_password'"
log_action "Incorrect usage for add/update. Correct format not followed."
else
add_update_cron_job "$@"
fi
;;
remove)
remove_cron_job
;;
*)
echo "Invalid action: $1. Use add, update, or remove."
log_action "Invalid action attempted: $1"
exit 1
;;
esac

View File

@ -0,0 +1,62 @@
#!/bin/bash
# Validate input parameters
if [ "$#" -ne 2 ]; then
echo "Usage: $0 <snapshot_id> <restic_password>"
exit 1
fi
# Assign input to variables
SNAPSHOT_ID=$1
RESTIC_PASSWORD=$2
RESTIC_REPOSITORY="/mnt/backups"
LOG_DIR="/home/litespeed/mb-backups/logs/restore"
WP_CONFIG='/var/www/webroot/ROOT/wp-config.php'
# Set up the environment
export RESTIC_PASSWORD
mkdir -p "$LOG_DIR" # Ensure the log directory exists
LOG_FILE="${LOG_DIR}/restore_$(date +'%Y-%m-%d_%H-%M-%S').log"
# Extracting 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)
# Logging function
log() {
echo "$1" | tee -a "$LOG_FILE"
}
log "Starting restoration for snapshot $SNAPSHOT_ID..."
# Attempt to extract the path and tags for the specified snapshot ID
SNAPSHOT_DATA=$(restic -r "$RESTIC_REPOSITORY" snapshots --json | jq -r ".[] | select(.short_id == \"$SNAPSHOT_ID\")")
SNAPSHOT_PATH=$(echo "$SNAPSHOT_DATA" | jq -r ".paths[]")
SNAPSHOT_TAGS=$(echo "$SNAPSHOT_DATA" | jq -r ".tags[]")
echo "Snapshot data: Path=$SNAPSHOT_PATH, Tags=$SNAPSHOT_TAGS" | tee -a "$LOG_FILE"
if [[ "$SNAPSHOT_TAGS" == *"wordpress_db"* ]] && [[ "$SNAPSHOT_PATH" == "/stdin" ]]; then
log "Detected database backup. Starting restoration process for database $DB_NAME..."
if restic -r "$RESTIC_REPOSITORY" dump "$SNAPSHOT_ID" stdin | mysql -u "$DB_USER" -p"$DB_PASSWORD" "$DB_NAME"; then
log "Restoration of snapshot $SNAPSHOT_ID with TAG: wordpress_db completed successfully."
else
log "Failed to restore database from Snapshot ID: $SNAPSHOT_ID. Please check the log file for details: $LOG_FILE"
exit 1
fi
else
if [ -z "$SNAPSHOT_PATH" ] || [ "$SNAPSHOT_PATH" = "null" ]; then
log "Failed to find a valid path for snapshot ID $SNAPSHOT_ID. Check the snapshot ID and try again."
exit 1
fi
log "Restoring snapshot $SNAPSHOT_ID to $SNAPSHOT_PATH..."
if restic -r "$RESTIC_REPOSITORY" restore "$SNAPSHOT_ID" --target "$SNAPSHOT_PATH"; then
log "Restoration of snapshot $SNAPSHOT_ID to $SNAPSHOT_PATH completed successfully."
else
log "Failed to restore Snapshot ID: $SNAPSHOT_ID to PATH: $SNAPSHOT_PATH. Please check the log file for details: $LOG_FILE"
exit 1
fi
fi

View File

@ -0,0 +1,30 @@
#!/bin/bash
# Display usage if no tag is provided
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <tag>"
echo "Available tags: main_backup, wordpress_db, core_files, media_themes, full_backup"
exit 1
fi
TAG=$1
backupPath='/mnt/backups'
# Embedded Restic repository password
RESTIC_PASSWORD="tonysite1" # Replace this with your actual password
export RESTIC_REPOSITORY="$backupPath"
export RESTIC_PASSWORD
# Validate the provided tag against known tags
case "$TAG" in
main_backup|wordpress_db|core_files|media_themes|full_backup)
# Use the --json flag to output the snapshots in JSON format
restic -r $RESTIC_REPOSITORY snapshots --tag "$TAG" --json
;;
*)
echo "Error: Unknown tag '$TAG'."
echo "Available tags: main_backup, wordpress_db, core_files, media_themes, full_backup"
exit 1
;;
esac