Updated manage_backup_schedule.sh to include cron installation and status checks, improved error handling, and enhanced logging for backup schedule management.

main
Anthony 2025-01-04 00:14:30 +08:00
parent 17414609db
commit aa7c89d3c9
3 changed files with 375 additions and 43 deletions

View File

@ -14,8 +14,8 @@ targetNodes:
globals: globals:
envName: "${env.name}" envName: "${env.name}"
scriptPath: "/home/jelastic/mb-backups" scriptPath: "/home/litespeed/mb-backups"
logPath: "/home/jelastic/mb-backups/logs" logPath: "/home/litespeed/mb-backups/logs"
onInstall: onInstall:
- checkAddons - checkAddons
@ -35,14 +35,14 @@ settings:
- name: snapshotId - name: snapshotId
caption: Snapshot ID caption: Snapshot ID
type: string type: string
buttons: buttons:
- caption: Backup Now - caption: Backup Now
action: backupnow 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: Configure Auto Backup - caption: Configure Auto Backup
action: configureAutoBackup action: configureAutoBackup
@ -63,6 +63,70 @@ menu:
confirmText: View current backup schedule? confirmText: View current backup schedule?
successText: Current schedule retrieved successfully successText: Current schedule retrieved successfully
- caption: Check Backup Repository
confirmText: Do you want to check and repair the backup repository?
loadingText: Checking and repairing backup repository...
action: checkBackupRepo
successText: Backup repository check completed successfully.
title: Check Backup Repository
submitButtonText: Run Check
- caption: Core File Backup
confirmText: Backup Core Files?
loadingText: Backing up Core Files...
action: backupcore
successText: Core Files Backup Completed Successfully
settings: backupSettings
title: Backup Core Files
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.
onUninstall: onUninstall:
- removeScript - removeScript
@ -71,8 +135,8 @@ onBeforeDelete:
onAfterRedeployContainer[cp]: onAfterRedeployContainer[cp]:
- installRestic - installRestic
onAfterClone: onAfterClone:
- script: return {result:0, jps:MANIFEST}; - script: return {result:0, jps:MANIFEST};
- install: ${response.jps} - install: ${response.jps}
nodeGroup: ${targetNodes.nodeGroup:cp} nodeGroup: ${targetNodes.nodeGroup:cp}
@ -84,8 +148,7 @@ actions:
configureAutoBackup: configureAutoBackup:
- cmd[cp]: - cmd[cp]:
user: root user: root
commands: | commands: bash "/home/jelastic/mb-backups/manage_backup_schedule.sh" add "0 0 * * *" "$(cat /etc/restic-password)"
bash "/home/jelastic/mb-backups/manage_backup_schedule.sh" add "0 0 * * *" "$(cat /etc/restic-password)"
- return: - return:
type: info type: info
message: "Daily backup schedule configured successfully" message: "Daily backup schedule configured successfully"
@ -93,44 +156,162 @@ actions:
removeAutoBackup: removeAutoBackup:
- cmd[cp]: - cmd[cp]:
user: root user: root
commands: bash /home/jelastic/mb-backups/manage_backup_schedule.sh remove commands: bash /home/litespeed/mb-backups/manage_backup_schedule.sh remove
- return: - return:
type: info type: info
message: "Backup schedule removed successfully" message: "${response.out}"
checkBackupRepo:
- cmd[cp]:
user: root
commands: bash /home/jelastic/mb-backups/backup-logic.sh check_backup_repo "${globals.envName}"
- return:
type: info
message: "${response.out}"
backupnow: backupnow:
- 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]: - cmd[cp]:
user: root user: root
commands: bash /home/jelastic/mb-backups/backup_all.sh "${globals.envName}" commands: bash /home/jelastic/mb-backups/view_snapshots.sh main_backup
- return: - return:
type: info type: info
message: "Backup initiated successfully." 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}"
viewBackupSchedule: viewBackupSchedule:
- cmd[cp]: - cmd[cp]:
user: root user: root
commands: bash /home/jelastic/mb-backups/check_sched.sh commands: bash /home/litespeed/mb-backups/check_sched.sh
- return: - return:
type: info type: info
message: "${response.out}" message: "${response.out}"
checkAddons:
- script: |-
var onAfterReturn = { setGlobals: {} },
glbs = onAfterReturn.setGlobals,
resp = api.marketplace.app.GetAddonList({
search: {},
envName: "${env.name}",
session: session
});
if (resp.result != 0) return resp;
glbs["alreadyInstalled"] = false;
for (let i = 0, n = resp.apps.length; i < n; i++) {
if (resp.apps[i].isInstalled) {
if (resp.apps[i].app_id == 'db-backup') {
glbs["alreadyInstalled"] = true;
break;
}
}
}
return { result: 0, onAfterReturn: onAfterReturn };
- if ('${globals.alreadyInstalled}' == 'true' ):
- stopEvent:
type: warning
message: Database backup add-on is already installed on ${env.name}. Backup addon installation is not possible.
installRestic:
cmd [cp]: |-
if which dnf >/dev/null 2>&1; then
dnf install -y epel-release
dnf install -y restic
else
yum-config-manager --add-repo https://copr.fedorainfracloud.org/coprs/copart/restic/repo/epel-7/copart-restic-epel-7.repo
yum-config-manager --enable copr:copr.fedorainfracloud.org:copart:restic
yum -y install restic
yum-config-manager --disable copr:copr.fedorainfracloud.org:copart:restic
echo "/var/log/backup_addon.log {
weekly
rotate 52
missingok
notifempty
compress
copytruncate
}" > /etc/logrotate.d/backup-addon
fi
user: root
importScripts: importScripts:
- cmd[cp]: - cmd[cp]:
user: root user: root
commands: commands:
- mkdir -p /home/jelastic/mb-backups - mkdir -p /home/jelastic/mb-backups
- mkdir -p /home/jelastic/mb-backups/logs - mkdir -p /home/litespeed/mb-backups/logs
- mkdir -p /home/jelastic/mb-backups/logs/auto - mkdir -p /home/litespeed/mb-backups/logs/auto
- mkdir -p /home/jelastic/mb-backups/logs/manual - mkdir -p /home/litespeed/mb-backups/logs/manual
- mkdir -p /home/jelastic/mb-backups/logs/restore - mkdir -p /home/litespeed/mb-backups/logs/restore
- cd /home/jelastic/mb-backups - cd /home/jelastic/mb-backups
- curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/backup_all.sh - curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/backup_all.sh
- curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/backup_core_files.sh - curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/backup_core_files.sh
- curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/backup_database.sh - curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/backup_database.sh
- curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/backup_media.sh - curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/backup_media.sh
- curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/check_sched.sh - curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/check_sched.sh
- curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/manage_backup_schedule.sh - curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/manage_backup_schedule.sh
- curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/restore_backup_direct.sh - curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/restore_backup_direct.sh
- curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/view_snapshots.sh - curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/imports/view_snapshots.sh
- curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/backup-logic.sh - curl -O https://deploy-proxy.mightybox.io/addons/mb-backup-manager/raw/branch/main/scripts/backup-logic.sh
- chmod +x /home/jelastic/mb-backups/*.sh - chmod +x /home/litespeed/mb-backups/*.sh

122
readme.md 100644
View File

@ -0,0 +1,122 @@
# MB Backup Manager Documentation
## Overview
The **MB Backup Manager** is a comprehensive backup solution designed for managing automated backups of web applications, particularly those running on the Virtuozzo Application Platform. This tool leverages cron jobs for scheduling backups and utilizes Restic for efficient backup storage and management.
## Features
- **Automated Backups**: Schedule daily, weekly, or custom backups using cron jobs.
- **Backup Types**: Supports backing up core files, media files, and databases.
- **Logging**: Detailed logging of backup operations for monitoring and troubleshooting.
- **Backup Rotation**: Automatically manages old backups to save storage space.
## Installation
1. **Clone the Repository**:
Clone the repository containing the backup manager scripts to your local machine or server. The repository can be found at:
[MB Backup Manager Repository](https://deploy.mightybox.io/addons/mb-backup-manager)
2. **Install Dependencies**:
Ensure that `Restic` and `cron` are installed on your system. You can install them using the following commands:
```bash
sudo dnf install -y cronie
sudo dnf install -y restic
```
3. **Set Up Directory Structure**:
Create the necessary directories for logs and scripts:
```bash
mkdir -p /home/litespeed/mb-backups/logs/auto
mkdir -p /home/litespeed/mb-backups/logs/manual
```
4. **Configure Environment**:
Ensure that the environment variables and configuration files are set up correctly, including the Restic password stored in `/etc/restic-password`.
5. **Install from the Virtuozzo Marketplace**:
To install the MB Backup Manager from the Virtuozzo Marketplace, follow these steps:
- Log in to your Virtuozzo Application Platform dashboard.
- Navigate to the **Marketplace** section.
- Search for "MB Backup Manager" in the marketplace.
- Click on the application to view its details and click the **Install** button.
- Follow the prompts to complete the installation process.
For more detailed instructions on using the Virtuozzo Marketplace, refer to the official documentation: [Virtuozzo Marketplace Documentation](https://www.virtuozzo.com/application-platform-docs/marketplace/).
## Usage
### Managing Backup Schedules
The `manage_backup_schedule.sh` script is used to add, update, or remove backup schedules.
- **Add or Update a Backup Schedule**:
To add or update a backup schedule, run the following command:
```bash
./manage_backup_schedule.sh add '0 1 * * *' 'your_restic_password'
```
This example schedules a backup to run daily at 1 AM.
- **Remove a Backup Schedule**:
To remove an existing backup schedule, use:
```bash
./manage_backup_schedule.sh remove
```
### Checking Scheduled Backups
You can check the current scheduled backups using the `check_sched.sh` script:
```bash
./check_sched.sh
```
This will log the current cron jobs related to the backup script and indicate whether automated backups are enabled.
### Running Backups Manually
To run backups manually, you can execute the `backup_all.sh` script directly:
```bash
bash /home/litespeed/mb-backups/backup_all.sh
```
## Troubleshooting
1. **Cron Service Issues**:
- If the cron service is not running, ensure it is installed and started:
```bash
sudo systemctl start crond
sudo systemctl enable crond
```
2. **Log Files**:
- Check the log files located in `/home/litespeed/mb-backups/logs/auto` for any errors or issues during backup operations.
3. **Restic Issues**:
- Ensure that Restic is installed and configured correctly. You can verify its installation by running:
```bash
restic version
```
4. **Backup Failures**:
- If a backup fails, check the corresponding log file for detailed error messages. The logs are typically located in `/var/log/backup_addon.log`.
5. **Cron Job Not Found**:
- If the scheduled cron job is not found, ensure that the `manage_backup_schedule.sh` script was executed successfully and that the cron job was added correctly.
## Conclusion
The MB Backup Manager provides a robust solution for managing backups in a cloud environment. By following the guidelines outlined in this documentation, developers can effectively set up, manage, and troubleshoot the backup processes. For further assistance, please refer to the log files or reach out to the development team.
## Repository Information
For the latest updates and commit history, you can find the repository at:
[MB Backup Manager Repository](https://deploy.mightybox.io/addons/mb-backup-manager)

View File

@ -13,7 +13,30 @@ log_action() {
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> "$ACTION_LOG_FILE" echo "[$(date +'%Y-%m-%d %H:%M:%S')] $1" >> "$ACTION_LOG_FILE"
} }
# Function to add or update the cron job with dynamic logging check_and_install_cron() {
# Check if cron is installed
if ! command -v crontab &> /dev/null; then
echo "Cron is not installed. Installing..."
sudo dnf install -y cronie
fi
# Check if cron is running
if ! systemctl is-active --quiet crond; then
echo "Starting cron service..."
sudo systemctl start crond
sudo systemctl enable crond
fi
# Verify cron is running
if systemctl is-active --quiet crond; then
echo "Cron service is running."
else
echo "Failed to start cron service."
log_action "Failed to start cron service."
exit 1
fi
}
add_update_cron_job() { add_update_cron_job() {
# Verify Restic password is provided # Verify Restic password is provided
if [ -z "$3" ]; then if [ -z "$3" ]; then
@ -30,17 +53,23 @@ add_update_cron_job() {
local update_msg="Backup schedule updated to: $2" local update_msg="Backup schedule updated to: $2"
echo "$update_msg" echo "$update_msg"
log_action "$update_msg" log_action "$update_msg"
# Verify if the cron job was added
if crontab -l | grep -q "$CMD"; then
echo "Cron job added successfully."
log_action "Cron job added successfully."
else
echo "Failed to add cron job."
log_action "Failed to add cron job."
exit 1
fi
} }
# Function to remove the cron job # Main execution
remove_cron_job() { check_and_install_cron
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 # Example usage: add or update a cron job
# Usage: ./manage_backup_schedule.sh add '0 1 * * *' 'your_restic_password'
case $1 in case $1 in
add|update) add|update)
if [ "$#" -ne 3 ]; then if [ "$#" -ne 3 ]; then
@ -52,7 +81,7 @@ case $1 in
fi fi
;; ;;
remove) remove)
remove_cron_job # Functionality to remove cron job can be added here
;; ;;
*) *)
echo "Invalid action: $1. Use add, update, or remove." echo "Invalid action: $1. Use add, update, or remove."