tweaks to the scripts
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 41s
All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 41s
This commit is contained in:
71
backup.sh
71
backup.sh
@@ -1,53 +1,23 @@
|
|||||||
#!/bin/bash
|
#!/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
|
# Function to send notification
|
||||||
send_notify() {
|
HOSTNAME=$(cat /etc/hostname)
|
||||||
local title="$1"
|
NOTIFY_URL_ERROR="http://notify.haven/template/notify/error"
|
||||||
local message="$2"
|
NOTIFY_URL_BACKUP="http://notify.haven/template/notify/backup"
|
||||||
curl -s -X POST "$NOTIFY_URL" \
|
send_error_notification() {
|
||||||
|
local message="$1"
|
||||||
|
local critical="$2"
|
||||||
|
curl -s -X POST "$NOTIFY_URL_ERROR" \
|
||||||
-H "Content-Type: application/json" \
|
-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
|
# Create a compressed backup file
|
||||||
zip -q -r $BACKUP_FILE $SOURCE_DIR || true
|
zip -q -r $BACKUP_FILE $SOURCE_DIR || true
|
||||||
if [ $? -ne 0 ]; then
|
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
|
fi
|
||||||
|
|
||||||
# Check if remote path exists
|
# Check if remote path exists
|
||||||
if ! ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $REMOTE_DIR"; then
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Transfer the backup file to the remote server
|
# Transfer the backup file to the remote server
|
||||||
if ! scp $BACKUP_FILE $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR; then
|
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
|
exit 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Remove the backup file
|
# Remove the backup file
|
||||||
|
BACKUP_SIZE=$(du -m $BACKUP_FILE | cut -f1)
|
||||||
rm $BACKUP_FILE
|
rm $BACKUP_FILE
|
||||||
|
|
||||||
# Erase last 7 days backups from remote server
|
# 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
|
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
|
fi
|
||||||
|
|
||||||
# Success notification
|
# 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"
|
echo "Backup completed successfully"
|
||||||
exit 0
|
exit 0
|
||||||
|
|
||||||
|
@@ -4,7 +4,6 @@
|
|||||||
#
|
#
|
||||||
# Description: Automatically updates Docker containers and manages Docker images
|
# Description: Automatically updates Docker containers and manages Docker images
|
||||||
# Features:
|
# Features:
|
||||||
# - Self-updating capability
|
|
||||||
# - Updates all Docker Compose projects in /root/docker
|
# - Updates all Docker Compose projects in /root/docker
|
||||||
# - Skips containers with .ignore file
|
# - Skips containers with .ignore file
|
||||||
# - Removes obsolete Docker Compose version attributes
|
# - Removes obsolete Docker Compose version attributes
|
||||||
@@ -13,6 +12,23 @@
|
|||||||
# Version: 2.0
|
# Version: 2.0
|
||||||
|
|
||||||
set -euo pipefail # Exit on error, undefined vars, and pipe failures
|
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
|
# CONFIGURATION
|
||||||
@@ -28,8 +44,6 @@ readonly LIGHT_GREY='\033[0;37m'
|
|||||||
readonly YELLOW='\033[1;33m'
|
readonly YELLOW='\033[1;33m'
|
||||||
|
|
||||||
# Script configuration
|
# 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 DOCKER_FOLDER="/root/docker"
|
||||||
readonly COMPOSE_FILES=("docker-compose.yml" "docker-compose.yaml" "compose.yaml" "compose.yml")
|
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
|
# Print formatted log messages
|
||||||
log_info() { echo -e "${LIGHT_GREY}[i] $1${NC}"; }
|
log_info() { echo -e "${LIGHT_GREY}[i] $1${NC}"; }
|
||||||
log_success() { echo -e "${LIGHT_GREEN}[✓] $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_step() { echo -e "${LIGHT_BLUE}[i] $1${NC}"; }
|
||||||
log_container() { echo -e "${LIGHT_BLUE}[$1] $2${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
|
# Exit with error message
|
||||||
die() {
|
die() {
|
||||||
log_error "$1"
|
log_error "$1"
|
||||||
@@ -86,69 +107,6 @@ get_url_hash() {
|
|||||||
curl -s "$url" 2>/dev/null | sha256sum | awk '{print $1}' || echo ""
|
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
|
# DOCKER COMPOSE MANAGEMENT
|
||||||
#==============================================================================
|
#==============================================================================
|
||||||
@@ -315,6 +273,8 @@ cleanup_docker_resources() {
|
|||||||
#==============================================================================
|
#==============================================================================
|
||||||
|
|
||||||
main() {
|
main() {
|
||||||
|
START_TIME=$(date +%s)
|
||||||
|
|
||||||
log_step "Starting Docker Container Updater"
|
log_step "Starting Docker Container Updater"
|
||||||
echo
|
echo
|
||||||
|
|
||||||
@@ -332,6 +292,12 @@ main() {
|
|||||||
|
|
||||||
echo
|
echo
|
||||||
log_success "Docker container update process completed!"
|
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
|
# Execute main function with all arguments
|
||||||
|
@@ -26,9 +26,9 @@ readonly AVAILABLE_SCRIPTS=("clean.sh" "backup.sh" "docker-updater.sh")
|
|||||||
|
|
||||||
# Format: [script_name]="cron_schedule"
|
# Format: [script_name]="cron_schedule"
|
||||||
declare -A CRONTAB_SCHEDULES=(
|
declare -A CRONTAB_SCHEDULES=(
|
||||||
["clean.sh"]="0 23 * * *" # Daily at 11 PM
|
["clean.sh"]="0 3 * * *" # Daily at 3 AM
|
||||||
["backup.sh"]="30 23 * * 1,5" # Monday and Friday at 11:30 PM
|
["backup.sh"]="0 23 * * 1,5" # Monday and Friday at 11 PM
|
||||||
["docker-updater.sh"]="0 3 */4 * *" # Every 4 days at 3 AM
|
["docker-updater.sh"]="0 3 * * 6" # Every Saturday at 3 AM
|
||||||
)
|
)
|
||||||
|
|
||||||
#==============================================================================
|
#==============================================================================
|
||||||
|
Reference in New Issue
Block a user