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

83 lines
2.2 KiB
Bash
Raw Permalink Normal View History

2024-09-18 16:53:20 +00:00
#!/bin/bash
2024-11-12 18:15:11 +00:00
# Enable error handling
set -e
trap 'echo "[$(date +%Y-%m-%d %H:%M:%S)] Backup failed" >> "$LOG_DIR/backup_error.log"' ERR
2024-09-18 16:53:20 +00:00
# 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')
2024-11-12 18:15:11 +00:00
# Ensure log directory exists
mkdir -p "$LOG_DIR"
2024-11-12 18:15:11 +00:00
# Logging function
2025-01-06 15:33:56 +00:00
log_message() {
local message="$1"
echo "[$(date +'%Y-%m-%d %H:%M:%S')] $message" | tee -a "${LOG_DIR}/backup_all.log"
2024-11-12 18:15:11 +00:00
}
# 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
2024-11-12 18:15:11 +00:00
# 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
2024-11-12 18:15:11 +00:00
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."
2025-01-06 15:13:20 +00:00
exit 1
fi
2024-09-18 16:53:20 +00:00
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
2024-09-18 16:53:20 +00:00
fi
2025-01-06 15:33:56 +00:00
log_message "Full backup process completed successfully."
2024-11-12 18:15:11 +00:00
}
2024-09-18 16:53:20 +00:00
2025-01-06 15:13:20 +00:00
# Argument handling
if [ "$#" -ne 1 ]; then
echo "Usage: $0 {manual|auto}"
2025-01-06 15:13:20 +00:00
exit 1
fi
case "$1" in
manual)
main "manual"
;;
auto)
main "auto"
;;
*)
echo "Usage: $0 {manual|auto}"
exit 1
;;
esac