server-scripts/backup.sh

67 lines
1.7 KiB
Bash
Raw Permalink Normal View History

2024-12-27 23:11:17 -03:00
#!/bin/bash
### AUTO-UPDATER ###
# Variables
2024-12-27 23:32:53 -03:00
SERVER_FILE="https://git.ivanch.me/ivanch/server-scripts/raw/branch/main/backup.sh"
2024-12-27 23:11:17 -03:00
SERVER_OK=1
# Check if the server file exists
2024-12-27 23:44:46 -03:00
curl -s --head $SERVER_FILE | head -n 1 | grep -E "HTTP/[12] [23].." > /dev/null
2024-12-27 23:11:17 -03:00
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