This commit is contained in:
66
backup.sh
Normal file
66
backup.sh
Normal file
@@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
|
||||
### AUTO-UPDATER ###
|
||||
# Variables
|
||||
SERVER_FILE="http://h.local/backup.sh"
|
||||
SERVER_OK=1
|
||||
|
||||
# Check if the server file exists
|
||||
curl -s --head $SERVER_FILE | head -n 1 | grep "HTTP/1.[01] [23].." > /dev/null
|
||||
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: $SERVER_FILE not found." >&2
|
||||
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.local"
|
||||
REMOTE_DIR="/mnt/Storage/Backups/Docker/$(cat /etc/hostname)"
|
||||
|
||||
# Create a compressed backup file
|
||||
zip -r $BACKUP_FILE $SOURCE_DIR
|
||||
|
||||
# Check if remote path exists
|
||||
ssh $REMOTE_USER@$REMOTE_HOST "mkdir -p $REMOTE_DIR"
|
||||
|
||||
# Transfer the backup file to the remote server
|
||||
scp $BACKUP_FILE $REMOTE_USER@$REMOTE_HOST:$REMOTE_DIR
|
||||
|
||||
# Remove the backup file
|
||||
rm $BACKUP_FILE
|
||||
|
||||
# Erase last 7 days backups from remote server
|
||||
ssh $REMOTE_USER@$REMOTE_HOST "find $REMOTE_DIR -type f -name 'docker_backup_*' -mtime +7 -exec rm {} \;"
|
||||
|
||||
# Exit
|
||||
echo "Backup completed successfully"
|
||||
exit 0
|
||||
|
Reference in New Issue
Block a user