#!/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." # Delete previous backups from the backup drive if they exist echo "Deleting previous backups..." rm -rf "$BACKUP_DRIVE/my_platforms/*" "$BACKUP_DRIVE/ldr-server/*" "$BACKUP_DRIVE/home-server_nginx/*" echo "Previous backups deleted." # Copy VM files from my_platforms if the source directory exists if [ -d "$VM_DIR_MY_PLATFORMS" ]; then echo "Copying VM files from my_platforms..." cp -r "$VM_DIR_MY_PLATFORMS"/* "$BACKUP_DRIVE/my_platforms/" echo "Copy of my_platforms completed." else echo "Warning: $VM_DIR_MY_PLATFORMS does not exist. Skipping copy for my_platforms." fi # Copy VM files from ldr-server if the source directory exists if [ -d "$VM_DIR_LDR_SERVER" ]; then echo "Copying VM files from ldr-server..." cp -r "$VM_DIR_LDR_SERVER"/* "$BACKUP_DRIVE/ldr-server/" echo "Copy of ldr-server completed." else echo "Warning: $VM_DIR_LDR_SERVER does not exist. Skipping copy for ldr-server." fi # Copy Nginx configuration files echo "Copying Nginx configuration files..." cp -r "$NGINX_DIR"/* "$BACKUP_DRIVE/home-server_nginx/" echo "Copy 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 copied files #echo "Removing executable permissions from copied files..." #chmod -x "$BACKUP_DRIVE/my_platforms/"* "$BACKUP_DRIVE/ldr-server/"* "$BACKUP_DRIVE/home-server_nginx/"* #echo "Executable permissions removed." echo "Latest version of Server VM Backup completed successfully!" figlet "Done" echo "Unmounting the External backup drive" umount -l /mnt/my-drive # Reboot the system after the copy operation echo "Rebooting the system..." reboot