#!/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