#!/bin/bash # Function to send notification HOSTNAME=$(cat /etc/hostname) NOTIFY_URL_ERROR="http://notify.haven/template/notify/error" NOTIFY_URL_BACKUP="http://notify.haven/template/notify/backup" send_error_notification() { local message="$1" local critical="$2" curl -s -X POST "$NOTIFY_URL_ERROR" \ -H "Content-Type: application/json" \ -d "{\"caller\": \"Docker Backup - $HOSTNAME\", \"message\": \"$message\", \"critical\": $critical}" } send_backup_notification() { local message="$1" local backup_size="$2" curl -s -X POST "$NOTIFY_URL_BACKUP" \ -H "Content-Type: application/json" \ -d "{\"title\": \"Docker Backup - $HOSTNAME\", \"message\": \"$message\", \"backupSizeInMB\": $backup_size}" } #################### # Variables SOURCE_DIR="/root/docker" BACKUP_FILE="/tmp/docker_backup_$(date +%Y%m%d%H%M%S).tar.gz" REMOTE_USER="ivanch" REMOTE_HOST="nas.haven" REMOTE_DIR="/export/Backup/Docker/$(cat /etc/hostname)" # Create a compressed backup file zip -q -r $BACKUP_FILE $SOURCE_DIR || true if [ $? -ne 0 ]; then send_error_notification "⚠️ Some files or folders in $SOURCE_DIR could not be backed up (possibly in use or locked). Backup archive created with available files." false fi # Check if remote path exists if ! ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $REMOTE_DIR"; then send_error_notification "❌ Failed to create remote directory: $REMOTE_DIR on $REMOTE_HOST" true exit 1 fi # Transfer the backup file to the remote server if ! scp $BACKUP_FILE $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR; then send_error_notification "❌ Failed to transfer backup file to remote server: $REMOTE_HOST:$REMOTE_DIR" true exit 1 fi # Remove the backup file BACKUP_SIZE=$(du -m $BACKUP_FILE | cut -f1) rm $BACKUP_FILE # Erase last 7 days backups from remote server if ! ssh $REMOTE_USER@$REMOTE_HOST "find $REMOTE_DIR -type f -name 'docker_backup_*' -mtime +7 -exec rm {} \;"; then send_error_notification "⚠️ Failed to clean old backups on remote server: $REMOTE_HOST:$REMOTE_DIR" false fi # Success notification send_backup_notification "✅ Backup completed successfully for: $SOURCE_DIR to $REMOTE_HOST:$REMOTE_DIR" $BACKUP_SIZE echo "Backup completed successfully" exit 0