code refined to be autonomous and triggered by cronjob, with effect handeling of usb external drives and usb ports

This commit is contained in:
arul 2025-04-04 09:24:23 +05:30
parent 7abc6d7977
commit f5ea71a638
8 changed files with 251 additions and 48 deletions

106
backup-server Normal file → Executable file
View File

@ -1,104 +1,114 @@
#!/bin/bash
# Log file
LOG_FILE="./backup.log"
# Function to log messages
log_message() {
echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" | tee -a "$LOG_FILE"
}
# Check if the script is run with sudo
if [ "$EUID" -ne 0 ]; then
echo "Please run this script with sudo."
log_message "Please run this script with sudo."
exit 1
fi
# Check whether server VMs are in shutdown state if not..shutdown the VMs
# Run enable-usb script to prepare USB
log_message "Running enable-usb script..."
./enable-usb
# Check whether server VMs are in shutdown state, if not, shutdown the VMs
log_message "Checking server states..."
./check-server1
./check-server2
./check-server3
# 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/"
VM_DIR_FF_SERVER="/home/arul/FF-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
log_message "Checking if backup drive is mounted..."
if ! mountpoint -q /mnt/my-drive; then
echo "Backup drive is not mounted. Attempting to mount $DEVICE..."
log_message "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."
log_message "Error: Failed to mount the drive. Please check the device path and try again."
exit 1
fi
echo "Backup drive mounted successfully."
log_message "Backup drive mounted successfully."
else
log_message "Backup drive is already mounted."
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"
log_message "Setting up backup directory structure..."
mkdir -p "$BACKUP_DRIVE/my_platforms" "$BACKUP_DRIVE/ldr-server" "$BACKUP_DRIVE/FF-server" "$BACKUP_DRIVE/home-server_nginx"
if [ $? -ne 0 ]; then
echo "Error: Failed to create backup directories. Please check permissions and try again."
log_message "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."
chmod 755 "$BACKUP_DRIVE/my_platforms" "$BACKUP_DRIVE/ldr-server" "$BACKUP_DRIVE/FF-server" "$BACKUP_DRVE/home-server_nginx"
log_message "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."
log_message "Deleting previous backups..."
rm -rf "$BACKUP_DRIVE/my_platforms/*" "$BACKUP_DRIVE/ldr-server/*" "$BACKUP_DRIVE/FF-server/*" "$BACKUP_DRIVE/home-server_nginx/*"
log_message "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..."
log_message "Copying VM files from my_platforms..."
cp -r "$VM_DIR_MY_PLATFORMS"/* "$BACKUP_DRIVE/my_platforms/"
echo "Copy of my_platforms completed."
log_message "Copy of my_platforms completed."
else
echo "Warning: $VM_DIR_MY_PLATFORMS does not exist. Skipping copy for my_platforms."
log_message "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..."
log_message "Copying VM files from ldr-server..."
cp -r "$VM_DIR_LDR_SERVER"/* "$BACKUP_DRIVE/ldr-server/"
echo "Copy of ldr-server completed."
log_message "Copy of ldr-server completed."
else
echo "Warning: $VM_DIR_LDR_SERVER does not exist. Skipping copy for ldr-server."
log_message "Warning: $VM_DIR_LDR_SERVER does not exist. Skipping copy for ldr-server."
fi
# Copy VM files from FF-server if the source directory exists
if [ -d "$VM_DIR_FF_SERVER" ]; then
log_message "Copying VM files from FF-server..."
cp -r "$VM_DIR_FF_SERVER"/* "$BACKUP_DRIVE/FF-server/"
log_message "Copy of FF-server completed."
else
log_message "Warning: $VM_DIR_FF_SERVER does not exist. Skipping copy for FF-server."
fi
# Copy Nginx configuration files
echo "Copying Nginx configuration files..."
log_message "Copying Nginx configuration files..."
cp -r "$NGINX_DIR"/* "$BACKUP_DRIVE/home-server_nginx/"
echo "Copy of Nginx configuration completed."
log_message "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!"
# Final message
log_message "Latest version of Server VM Backup completed successfully!"
figlet "Done"
echo "Unmounting the External backup drive"
umount -l /mnt/my-drive
# Unmount the external backup drive using the unmount-ssd script
log_message "Unmounting the external backup drive..."
./unmount-ssd
# Reboot the system after the copy operation
echo "Rebooting the system..."
reboot
# Run disable-usb script to disable USB
log_message "Running disable-usb script..."
./disable-usb
#systemd service to initiate all servers with time intervals for smooth initiation
log_message "Restarting all server VMs..."
sudo systemctl restart start_vms.service

41
backup.log Normal file
View File

@ -0,0 +1,41 @@
2025-04-03 15:53:31 - Running enable-usb script...
2025-04-03 15:53:37 - Checking server states...
2025-04-03 15:54:10 - Checking if backup drive is mounted...
2025-04-03 15:54:10 - Backup drive is not mounted. Attempting to mount /dev/sdb1...
2025-04-03 15:54:11 - Backup drive mounted successfully.
2025-04-03 15:54:11 - Setting up backup directory structure...
2025-04-03 15:54:11 - Backup directory structure set up with appropriate permissions.
2025-04-03 15:54:11 - Deleting previous backups...
2025-04-03 15:54:11 - Previous backups deleted.
2025-04-03 15:54:11 - Copying VM files from my_platforms...
2025-04-03 16:42:15 - Copy of my_platforms completed.
2025-04-03 16:42:15 - Copying VM files from ldr-server...
2025-04-03 17:06:47 - Copy of ldr-server completed.
2025-04-03 17:06:47 - Copying VM files from FF-server...
2025-04-03 17:09:11 - Copy of FF-server completed.
2025-04-03 17:09:11 - Copying Nginx configuration files...
2025-04-03 17:09:11 - Copy of Nginx configuration completed.
2025-04-03 17:09:11 - Latest version of Server VM Backup completed successfully!
2025-04-03 17:09:11 - Unmounting the external backup drive...
2025-04-03 17:09:25 - Running disable-usb script...
2025-04-03 17:48:42 - Running enable-usb script...
2025-04-04 01:45:50 - Running enable-usb script...
2025-04-04 01:45:56 - Checking server states...
2025-04-04 01:46:56 - Checking if backup drive is mounted...
2025-04-04 01:46:56 - Backup drive is not mounted. Attempting to mount /dev/sdb1...
2025-04-04 01:46:56 - Backup drive mounted successfully.
2025-04-04 01:46:56 - Setting up backup directory structure...
2025-04-04 01:46:56 - Backup directory structure set up with appropriate permissions.
2025-04-04 01:46:56 - Deleting previous backups...
2025-04-04 01:46:56 - Previous backups deleted.
2025-04-04 01:46:56 - Copying VM files from my_platforms...
2025-04-04 02:35:13 - Copy of my_platforms completed.
2025-04-04 02:35:13 - Copying VM files from ldr-server...
2025-04-04 02:59:44 - Copy of ldr-server completed.
2025-04-04 02:59:44 - Copying VM files from FF-server...
2025-04-04 03:02:11 - Copy of FF-server completed.
2025-04-04 03:02:11 - Copying Nginx configuration files...
2025-04-04 03:02:11 - Copy of Nginx configuration completed.
2025-04-04 03:02:11 - Latest version of Server VM Backup completed successfully!
2025-04-04 03:02:11 - Unmounting the external backup drive...
2025-04-04 03:02:21 - Running disable-usb script...

41
check-server3 Executable file
View File

@ -0,0 +1,41 @@
#!/bin/bash
# Define server IP, port, and SSH key path for FF-server
SERVER_IP="192.168.29.251"
SERVER_PORT="2224"
SERVER_NAME="FF-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

17
debug-usb Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
echo "🔄 Debugging USB issue..."
# Check who is using the usb_storage module
echo "⚡ Checking what is holding usb_storage..."
sudo lsmod | grep usb_storage
sudo lsof | grep usb-storage
sudo fuser -v /dev/sd*
echo "🔄 Checking USB devices..."
lsusb
echo "🔄 Checking kernel logs for USB errors..."
dmesg | tail -30

22
disable-usb Executable file
View File

@ -0,0 +1,22 @@
#!/bin/bash
USB_DEV="3-2"
MOUNT_POINT="/mnt/my-drive"
# Check if SSD is mounted before unmounting
if mountpoint -q "$MOUNT_POINT"; then
sync
umount "$MOUNT_POINT"
echo "✅ Unmounted $MOUNT_POINT"
else
echo "⚠ No drive mounted at $MOUNT_POINT"
fi
# Check if USB device exists before trying to disable it
if [[ -e /sys/bus/usb/devices/$USB_DEV ]]; then
echo 1 | sudo tee /sys/bus/usb/devices/$USB_DEV/remove
echo "✅ USB device $USB_DEV disabled"
else
echo "⚠ USB device $USB_DEV is already removed"
fi

33
enable-usb Executable file
View File

@ -0,0 +1,33 @@
#!/bin/bash
echo "🔄 Resetting USB system..."
# Step 1: Unmount the drive if it's mounted
MOUNT_POINT="/mnt/my-drive"
if mountpoint -q "$MOUNT_POINT"; then
echo "⚡ Unmounting $MOUNT_POINT..."
sudo umount "$MOUNT_POINT"
else
echo "⚠ No drive mounted at $MOUNT_POINT"
fi
# Step 2: Unload USB storage module
echo "⚡ Unloading usb_storage module..."
sudo rmmod usb_storage 2>/dev/null || sudo modprobe -r usb_storage
sleep 2
# Step 3: Reset the USB controller
echo "🔄 Unbinding USB controller..."
echo -n "0000:00:14.0" | sudo tee /sys/bus/pci/drivers/xhci_hcd/unbind
sleep 2
echo "🔄 Binding USB controller..."
echo -n "0000:00:14.0" | sudo tee /sys/bus/pci/drivers/xhci_hcd/bind
sleep 2
# Step 4: Reload USB storage module
echo "⚡ Reloading usb_storage module..."
sudo modprobe usb_storage
echo "✅ USB reset complete! Run 'lsusb' to verify."

21
peek-backup Executable file
View File

@ -0,0 +1,21 @@
#!/bin/bash
MOUNT_POINT="/mnt/my-drive"
DEVICE="/dev/sdb1" # Updated to the correct partition
echo "🔄 Checking if $DEVICE is already mounted..."
if mountpoint -q "$MOUNT_POINT"; then
echo "⚠ Already mounted at $MOUNT_POINT. Unmounting..."
sudo umount "$MOUNT_POINT" || { echo "❌ Failed to unmount. Exiting."; exit 1; }
fi
echo "⚡ Mounting $DEVICE to $MOUNT_POINT..."
sudo mkdir -p "$MOUNT_POINT"
sudo mount "$DEVICE" "$MOUNT_POINT" && echo "✅ Mounted successfully!" || { echo "❌ Failed to mount."; exit 1; }
echo "📂 Scanning disk usage with ncdu..."
ncdu "$MOUNT_POINT"
echo "🗂 Opening Ranger..."
ranger "$MOUNT_POINT"

18
unmount-ssd Executable file
View File

@ -0,0 +1,18 @@
#!/bin/bash
MOUNT_POINT="/mnt/my-drive"
echo "🔄 Checking if $MOUNT_POINT is mounted..."
if mountpoint -q "$MOUNT_POINT"; then
echo "⚡ Unmounting $MOUNT_POINT..."
sudo umount "$MOUNT_POINT" && echo "✅ Unmounted successfully!" || { echo "❌ Failed to unmount."; exit 1; }
else
echo "⚠ No drive mounted at $MOUNT_POINT"
exit 0
fi
# Display cool ASCII art
echo "🚀 Drive Unmounted!"
figlet "USB Ejected!"