All checks were successful
Check scripts syntax / check-scripts-syntax (push) Successful in 2s
92 lines
2.9 KiB
Bash
92 lines
2.9 KiB
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"
|
|
|
|
# Function to send notification
|
|
send_notify() {
|
|
local title="$1"
|
|
local message="$2"
|
|
curl -s -X POST "$NOTIFY_URL" \
|
|
-H "Content-Type: application/json" \
|
|
-d "{\"title\": \"$title\", \"message\": \"$message\"}"
|
|
}
|
|
|
|
# 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 "[Docker Backup][$HOSTNAME] Error" "❌ <span style='color:red'>Server file not found:</span> $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
|
|
|
|
####################
|
|
|
|
# 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
|
|
if ! zip -r $BACKUP_FILE $SOURCE_DIR; then
|
|
send_notify "Backup Error" "❌ <span style='color:red'>Failed to create backup archive for:</span> $SOURCE_DIR"
|
|
exit 1
|
|
fi
|
|
|
|
# Check if remote path exists
|
|
if ! ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $REMOTE_DIR"; then
|
|
send_notify "[Docker Backup][$HOSTNAME] Error" "❌ <span style='color:red'>Failed to create remote directory:</span> $REMOTE_DIR on $REMOTE_HOST"
|
|
exit 1
|
|
fi
|
|
|
|
# Transfer the backup file to the remote server
|
|
if ! scp $BACKUP_FILE $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR; then
|
|
send_notify "[Docker Backup][$HOSTNAME] Error" "❌ <span style='color:red'>Failed to transfer backup file to remote server:</span> $REMOTE_HOST:$REMOTE_DIR"
|
|
exit 1
|
|
fi
|
|
|
|
# Remove the backup file
|
|
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 "[Docker Backup][$HOSTNAME] Warning" "⚠️ <span style='color:orange'>Failed to clean old backups on remote server:</span> $REMOTE_HOST:$REMOTE_DIR"
|
|
fi
|
|
|
|
# Success notification
|
|
send_notify "[Docker Backup][$HOSTNAME] Success" "✅ <span style='color:green'>Backup completed successfully for:</span> $SOURCE_DIR to $REMOTE_HOST:$REMOTE_DIR"
|
|
echo "Backup completed successfully"
|
|
exit 0
|
|
|