simple shell script to backup home server files and VMs to external drive
This commit is contained in:
commit
7b0108c236
99
backup-server
Executable file
99
backup-server
Executable file
|
@ -0,0 +1,99 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Check if the script is run with sudo
|
||||
if [ "$EUID" -ne 0 ]; then
|
||||
echo "Please run this script with sudo."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
#Check whether server VMs are in shutdown state if not..shutdown the VMs
|
||||
|
||||
./check-server1
|
||||
./check-server2
|
||||
|
||||
|
||||
# Define source directories using the explicit path to your home directory
|
||||
VM_DIR_MY_PLATFORMS="/home/arul/my_platforms/"
|
||||
VM_DIR_LDR_SERVER="/home/arul/ldr-server/"
|
||||
NGINX_DIR="/etc/nginx/sites-available/"
|
||||
BACKUP_DRIVE="/mnt/my-drive/server-backup"
|
||||
DEVICE="/dev/sdb1" # Device name for the backup drive
|
||||
|
||||
# Check if the backup drive is mounted; if not, attempt to mount it
|
||||
if ! mountpoint -q /mnt/my-drive; then
|
||||
echo "Backup drive is not mounted. Attempting to mount $DEVICE..."
|
||||
mount $DEVICE /mnt/my-drive
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: Failed to mount the drive. Please check the device path and try again."
|
||||
exit 1
|
||||
fi
|
||||
echo "Backup drive mounted successfully."
|
||||
fi
|
||||
|
||||
# Ensure the main backup directory exists, and create necessary subdirectories if they don't exist
|
||||
echo "Setting up backup directory structure..."
|
||||
mkdir -p "$BACKUP_DRIVE/my_platforms" "$BACKUP_DRIVE/ldr-server" "$BACKUP_DRIVE/home-server_nginx"
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "Error: Failed to create backup directories. Please check permissions and try again."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Set permissions for the newly created backup directories
|
||||
chmod 700 "$BACKUP_DRIVE"
|
||||
chmod 755 "$BACKUP_DRIVE/my_platforms" "$BACKUP_DRIVE/ldr-server" "$BACKUP_DRIVE/home-server_nginx"
|
||||
echo "Backup directory structure set up with appropriate permissions."
|
||||
|
||||
# Backup VM files from my_platforms if the source directory exists
|
||||
if [ -d "$VM_DIR_MY_PLATFORMS" ]; then
|
||||
echo "Backing up VM files from my_platforms..."
|
||||
rsync -avh --progress --no-owner --no-group "$VM_DIR_MY_PLATFORMS" "$BACKUP_DRIVE/my_platforms/"
|
||||
echo "Backup of my_platforms completed."
|
||||
else
|
||||
echo "Warning: $VM_DIR_MY_PLATFORMS does not exist. Skipping backup for my_platforms."
|
||||
fi
|
||||
|
||||
# Backup VM files from ldr-server if the source directory exists
|
||||
if [ -d "$VM_DIR_LDR_SERVER" ]; then
|
||||
echo "Backing up VM files from ldr-server..."
|
||||
rsync -avh --progress --no-owner --no-group "$VM_DIR_LDR_SERVER" "$BACKUP_DRIVE/ldr-server/"
|
||||
echo "Backup of ldr-server completed."
|
||||
else
|
||||
echo "Warning: $VM_DIR_LDR_SERVER does not exist. Skipping backup for ldr-server."
|
||||
fi
|
||||
|
||||
# Backup Nginx configuration files
|
||||
echo "Backing up Nginx configuration files..."
|
||||
rsync -avh --progress --no-owner --no-group "$NGINX_DIR" "$BACKUP_DRIVE/home-server_nginx/"
|
||||
echo "Backup of Nginx configuration completed."
|
||||
|
||||
# Set permissions for Nginx configuration files
|
||||
echo "Setting permissions for Nginx configuration files..."
|
||||
chmod 644 "$BACKUP_DRIVE/home-server_nginx/"*
|
||||
echo "Permissions set for Nginx files."
|
||||
|
||||
# Set permissions for VM files if the directories exist
|
||||
if [ -d "$BACKUP_DRIVE/my_platforms" ]; then
|
||||
echo "Setting permissions for VM files in my_platforms..."
|
||||
chmod 600 "$BACKUP_DRIVE/my_platforms/"*
|
||||
fi
|
||||
|
||||
if [ -d "$BACKUP_DRIVE/ldr-server" ]; then
|
||||
echo "Setting permissions for VM files in ldr-server..."
|
||||
chmod 600 "$BACKUP_DRIVE/ldr-server/"*
|
||||
fi
|
||||
|
||||
# Remove executable permissions from backed up files
|
||||
echo "Removing executable permissions from backed up files..."
|
||||
chmod -x "$BACKUP_DRIVE/my_platforms/"* "$BACKUP_DRIVE/ldr-server/"* "$BACKUP_DRIVE/home-server_nginx/"*
|
||||
echo "Executable permissions removed."
|
||||
|
||||
echo "All backups completed successfully!"
|
||||
figlet "Done"
|
||||
|
||||
echo "Unmounting the External backup drive"
|
||||
umount -l /mnt/my-drive
|
||||
|
||||
# Reboot the system after the backup
|
||||
echo "Rebooting the system..."
|
||||
reboot
|
||||
|
42
check-server1
Executable file
42
check-server1
Executable file
|
@ -0,0 +1,42 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Define server IP, port, and SSH key path for selfhost-1
|
||||
SERVER_IP="192.168.29.251"
|
||||
SERVER_PORT="2222"
|
||||
SERVER_NAME="selfhost-1 server VM"
|
||||
SSH_KEY_PATH="/home/arul/.ssh/id_ed25519" # Add your SSH key path here
|
||||
|
||||
echo "[-] Shutting down $SERVER_NAME"
|
||||
ssh -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no -p "$SERVER_PORT" root@"$SERVER_IP" "poweroff"
|
||||
|
||||
# Wait for 10 seconds to give the server time to power off
|
||||
echo "[*] Waiting for the server to shut down..."
|
||||
sleep 10
|
||||
|
||||
# Check if the server is off by attempting to SSH and expecting failure
|
||||
echo "[*] Verifying server shutdown..."
|
||||
SERVER_OFF=false
|
||||
MAX_RETRIES=5
|
||||
for i in $(seq 1 "$MAX_RETRIES"); do
|
||||
# Try to SSH into the server
|
||||
ssh -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no -p "$SERVER_PORT" -o ConnectTimeout=5 root@"$SERVER_IP" "exit" 2>/dev/null
|
||||
|
||||
# Check if the SSH command failed, indicating the server is off
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "[+] $SERVER_NAME is powered off."
|
||||
SERVER_OFF=true
|
||||
break
|
||||
else
|
||||
echo "[*] Server is still shutting down. Checking again in 5 seconds..."
|
||||
sleep 5
|
||||
fi
|
||||
done
|
||||
|
||||
# Final check to ensure no warning message if the server is off
|
||||
if [ "$SERVER_OFF" = false ]; then
|
||||
echo "[!] Warning: $SERVER_NAME did not shut down as expected."
|
||||
else
|
||||
echo "[+] Proceeding with the rest of the script."
|
||||
fi
|
||||
|
||||
|
42
check-server2
Executable file
42
check-server2
Executable file
|
@ -0,0 +1,42 @@
|
|||
#!/bin/bash
|
||||
|
||||
# Define server IP, port, and SSH key path
|
||||
SERVER_IP="192.168.29.251"
|
||||
SERVER_PORT="2223"
|
||||
SERVER_NAME="selfhost-2 server VM"
|
||||
SSH_KEY_PATH="/home/arul/.ssh/id_ed25519" # Add your SSH key path here
|
||||
|
||||
echo "[-] Shutting down $SERVER_NAME"
|
||||
ssh -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no -p "$SERVER_PORT" root@"$SERVER_IP" "poweroff"
|
||||
|
||||
# Wait for 10 seconds to give the server time to power off
|
||||
echo "[*] Waiting for the server to shut down..."
|
||||
sleep 10
|
||||
|
||||
# Check if the server is off by attempting to SSH and expecting failure
|
||||
echo "[*] Verifying server shutdown..."
|
||||
SERVER_OFF=false
|
||||
MAX_RETRIES=5
|
||||
for i in $(seq 1 "$MAX_RETRIES"); do
|
||||
# Try to SSH into the server
|
||||
ssh -i "$SSH_KEY_PATH" -o StrictHostKeyChecking=no -p "$SERVER_PORT" -o ConnectTimeout=5 root@"$SERVER_IP" "exit" 2>/dev/null
|
||||
|
||||
# Check if the SSH command failed, indicating the server is off
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "[+] $SERVER_NAME is powered off."
|
||||
SERVER_OFF=true
|
||||
break
|
||||
else
|
||||
echo "[*] Server is still shutting down. Checking again in 5 seconds..."
|
||||
sleep 5
|
||||
fi
|
||||
done
|
||||
|
||||
# Final check to ensure no warning message if the server is off
|
||||
if [ "$SERVER_OFF" = false ]; then
|
||||
echo "[!] Warning: $SERVER_NAME did not shut down as expected."
|
||||
else
|
||||
echo "[+] Proceeding with the rest of the script."
|
||||
fi
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user