mb-backup-manager/scripts/imports/backup_all.sh

83 lines
2.2 KiB
Bash

#!/bin/bash
# Enable error handling
set -e
trap 'echo "[$(date +%Y-%m-%d %H:%M:%S)] Backup failed" >> "$LOG_DIR/backup_error.log"' ERR
# Configuration
LOG_DIR="/home/jelastic/mb-backups/logs"
CORE_BACKUP_SCRIPT="/home/jelastic/mb-backups/backup_core_files.sh"
DATABASE_BACKUP_SCRIPT="/home/jelastic/mb-backups/backup_database.sh"
MEDIA_BACKUP_SCRIPT="/home/jelastic/mb-backups/backup_media.sh"
TIMESTAMP=$(date +'%Y-%m-%d_%H-%M-%S')
# Ensure log directory exists
mkdir -p "$LOG_DIR"
# Logging function
log_message() {
local message="$1"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "${LOG_DIR}/backup_all.log"
}
# Main execution
main() {
local backup_type="$1"
local backup_tag=""
# Determine the backup tag based on the type
if [ "$backup_type" == "manual" ]; then
backup_tag="manual-backup-$TIMESTAMP"
log_message "Starting manual backup process with tag: $backup_tag"
elif [ "$backup_type" == "auto" ]; then
backup_tag="automated-backup-$TIMESTAMP"
log_message "Starting automated backup process with tag: $backup_tag"
else
log_message "ERROR: Invalid backup type. Use 'manual' or 'auto'."
exit 1
fi
# Run individual backup scripts
if bash "$CORE_BACKUP_SCRIPT" "$(cat /etc/restic-password)" "$backup_tag"; then
log_message "Core files backup completed successfully."
else
log_message "ERROR: Core files backup failed."
exit 1
fi
if bash "$DATABASE_BACKUP_SCRIPT" "$(cat /etc/restic-password)" "$backup_tag"; then
log_message "Database backup completed successfully."
else
log_message "ERROR: Database backup failed."
exit 1
fi
if bash "$MEDIA_BACKUP_SCRIPT" "$(cat /etc/restic-password)" "$backup_tag"; then
log_message "Media files backup completed successfully."
else
log_message "ERROR: Media files backup failed."
exit 1
fi
log_message "Full backup process completed successfully."
}
# Argument handling
if [ "$#" -ne 1 ]; then
echo "Usage: $0 {manual|auto}"
exit 1
fi
case "$1" in
manual)
main "manual"
;;
auto)
main "auto"
;;
*)
echo "Usage: $0 {manual|auto}"
exit 1
;;
esac