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
|
||||
|
||||
### 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
|
||||
|
||||
|
Reference in New Issue
Block a user