From eb8ca78f4f02f4325b5b0aca4b3b0ee02926e801 Mon Sep 17 00:00:00 2001 From: Jose Henrique Date: Sun, 24 Aug 2025 20:55:14 -0300 Subject: [PATCH] tweaks to the scripts --- backup.sh | 71 +++++++++--------------------- docker-updater.sh | 102 +++++++++++++++----------------------------- scripts-download.sh | 6 +-- 3 files changed, 58 insertions(+), 121 deletions(-) diff --git a/backup.sh b/backup.sh index 9204f64..ade3c67 100644 --- a/backup.sh +++ b/backup.sh @@ -1,53 +1,23 @@ #!/bin/bash -### AUTO-UPDATER ### -# Variables -SERVER_FILE="https://git.ivanch.me/ivanch/server-scripts/raw/branch/main/backup.sh" -SERVER_OK=1 -HOSTNAME=$(cat /etc/hostname) - -NOTIFY_URL="http://notify.haven/notify" - # Function to send notification -send_notify() { - local title="$1" - local message="$2" - curl -s -X POST "$NOTIFY_URL" \ +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 "{\"title\": \"Docker Backup - $HOSTNAME\", \"message\": \"$message\"}" + -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}" } - -# Check if the server file exists -curl -s --head $SERVER_FILE | head -n 1 | grep -E "HTTP/[12] [23].." > /dev/null - -if [ $? -ne 0 ]; then - echo "Error: $SERVER_FILE not found." >&2 - send_notify "" "❌ Server file not found: $SERVER_FILE" - SERVER_OK=0 -fi - -if [ $SERVER_OK -eq 1 ]; then - echo "Running auto-update..." - - # Compare the local and server files sha256sum to check if an update is needed - LOCAL_SHA256=$(sha256sum backup.sh | awk '{print $1}') - SERVER_SHA256=$(curl -s $SERVER_FILE | sha256sum | awk '{print $1}') - - if [ "$LOCAL_SHA256" != "$SERVER_SHA256" ]; then - echo "Updating backup.sh..." - curl -s -o backup.sh $SERVER_FILE - echo "backup.sh updated." - - chmod +x backup.sh - echo "Permissions set up." - - echo "Running updated backup.sh..." - ./backup.sh - exit 0 - else - echo "backup.sh is up to date.." - fi -fi #################### @@ -61,31 +31,32 @@ 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_notify "" "⚠️ Some files or folders in $SOURCE_DIR could not be backed up (possibly in use or locked). Backup archive created with available files." + 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_notify "" "❌ Failed to create remote directory: $REMOTE_DIR on $REMOTE_HOST" + 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_notify "" "❌ Failed to transfer backup file to remote server: $REMOTE_HOST:$REMOTE_DIR" + 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_notify "" "⚠️ Failed to clean old backups on remote server: $REMOTE_HOST:$REMOTE_DIR" + send_error_notification "⚠️ Failed to clean old backups on remote server: $REMOTE_HOST:$REMOTE_DIR" false fi # Success notification -send_notify "" "✅ Backup completed successfully for: $SOURCE_DIR to $REMOTE_HOST:$REMOTE_DIR" +send_backup_notification "✅ Backup completed successfully for: $SOURCE_DIR to $REMOTE_HOST:$REMOTE_DIR" $BACKUP_SIZE echo "Backup completed successfully" exit 0 diff --git a/docker-updater.sh b/docker-updater.sh index 9d28935..c2e5a26 100644 --- a/docker-updater.sh +++ b/docker-updater.sh @@ -4,7 +4,6 @@ # # Description: Automatically updates Docker containers and manages Docker images # Features: -# - Self-updating capability # - Updates all Docker Compose projects in /root/docker # - Skips containers with .ignore file # - Removes obsolete Docker Compose version attributes @@ -13,6 +12,23 @@ # Version: 2.0 set -euo pipefail # Exit on error, undefined vars, and pipe failures +HOSTNAME=$(cat /etc/hostname) +NOTIFY_URL_ERROR="http://notify.haven/template/notify/error" +NOTIFY_URL_UPDATE="http://notify.haven/template/notify/update" +send_error_notification() { + local message="$1" + local critical="$2" + curl -s -X POST "$NOTIFY_URL_ERROR" \ + -H "Content-Type: application/json" \ + -d "{\"caller\": \"$HOSTNAME\", \"message\": \"$message\", \"critical\": $critical}" +} +send_update_notification() { + local message="$1" + local script_time="$2" + curl -s -X POST "$NOTIFY_URL_UPDATE" \ + -H "Content-Type: application/json" \ + -d "{\"host\": \"$HOSTNAME\", \"asset\": \"Docker containers\", \"time\": $script_time}" +} #============================================================================== # CONFIGURATION @@ -28,8 +44,6 @@ readonly LIGHT_GREY='\033[0;37m' readonly YELLOW='\033[1;33m' # Script configuration -readonly SCRIPT_NAME="docker-updater.sh" -readonly SERVER_BASE_URL="https://git.ivanch.me/ivanch/server-scripts/raw/branch/main" readonly DOCKER_FOLDER="/root/docker" readonly COMPOSE_FILES=("docker-compose.yml" "docker-compose.yaml" "compose.yaml" "compose.yml") @@ -43,11 +57,18 @@ readonly AUTO_UPDATE_ENABLED=true # Print formatted log messages log_info() { echo -e "${LIGHT_GREY}[i] $1${NC}"; } log_success() { echo -e "${LIGHT_GREEN}[✓] $1${NC}"; } -log_warning() { echo -e "${YELLOW}[!] $1${NC}"; } -log_error() { echo -e "${RED}[x] $1${NC}" >&2; } log_step() { echo -e "${LIGHT_BLUE}[i] $1${NC}"; } log_container() { echo -e "${LIGHT_BLUE}[$1] $2${NC}"; } +log_warning() { + echo -e "${YELLOW}[!] $1${NC}"; + send_error_notification "$1" false +} +log_error() { + echo -e "${RED}[x] $1${NC}" >&2; + send_error_notification "$1" true +} + # Exit with error message die() { log_error "$1" @@ -86,69 +107,6 @@ get_url_hash() { curl -s "$url" 2>/dev/null | sha256sum | awk '{print $1}' || echo "" } -# Check if server file is accessible -check_server_connectivity() { - local url="$1" - curl -s --head "$url" | head -n 1 | grep -E "HTTP/[12] [23].." >/dev/null 2>&1 -} - -#============================================================================== -# AUTO-UPDATE FUNCTIONALITY -#============================================================================== - -# Perform self-update if newer version is available -perform_self_update() { - if [[ "$AUTO_UPDATE_ENABLED" != "true" ]]; then - log_info "Auto-update is disabled" - return 0 - fi - - local server_url="$SERVER_BASE_URL/$SCRIPT_NAME" - - log_step "Checking for script updates..." - - # Check if server file is accessible - if ! check_server_connectivity "$server_url"; then - log_warning "Cannot connect to update server, continuing with current version" - return 0 - fi - - # Compare local and server file hashes - local local_hash - local server_hash - - local_hash=$(get_file_hash "$SCRIPT_NAME") - server_hash=$(get_url_hash "$server_url") - - if [[ -z "$local_hash" || -z "$server_hash" ]]; then - log_warning "Cannot determine file hashes, skipping update" - return 0 - fi - - if [[ "$local_hash" != "$server_hash" ]]; then - log_info "Update available, downloading new version..." - - # Create backup of current script - local backup_file="${SCRIPT_NAME}.backup.$(date +%s)" - cp "$SCRIPT_NAME" "$backup_file" || die "Failed to create backup" - - # Download updated script - if curl -s -o "$SCRIPT_NAME" "$server_url"; then - chmod +x "$SCRIPT_NAME" || die "Failed to set executable permissions" - log_success "Script updated successfully" - - log_step "Running updated script..." - exec ./"$SCRIPT_NAME" "$@" - else - # Restore backup on failure - mv "$backup_file" "$SCRIPT_NAME" - die "Failed to download updated script" - fi - else - log_success "Script is already up to date" - fi -} - #============================================================================== # DOCKER COMPOSE MANAGEMENT #============================================================================== @@ -315,6 +273,8 @@ cleanup_docker_resources() { #============================================================================== main() { + START_TIME=$(date +%s) + log_step "Starting Docker Container Updater" echo @@ -332,6 +292,12 @@ main() { echo log_success "Docker container update process completed!" + + END_TIME=$(date +%s) + DURATION=$((END_TIME - START_TIME)) + log_info "Total duration: $DURATION seconds" + + send_update_notification $DURATION } # Execute main function with all arguments diff --git a/scripts-download.sh b/scripts-download.sh index 12366dc..9187304 100644 --- a/scripts-download.sh +++ b/scripts-download.sh @@ -26,9 +26,9 @@ readonly AVAILABLE_SCRIPTS=("clean.sh" "backup.sh" "docker-updater.sh") # Format: [script_name]="cron_schedule" declare -A CRONTAB_SCHEDULES=( - ["clean.sh"]="0 23 * * *" # Daily at 11 PM - ["backup.sh"]="30 23 * * 1,5" # Monday and Friday at 11:30 PM - ["docker-updater.sh"]="0 3 */4 * *" # Every 4 days at 3 AM + ["clean.sh"]="0 3 * * *" # Daily at 3 AM + ["backup.sh"]="0 23 * * 1,5" # Monday and Friday at 11 PM + ["docker-updater.sh"]="0 3 * * 6" # Every Saturday at 3 AM ) #==============================================================================