home-server-backup-script/backup-server

115 lines
4.1 KiB
Bash
Executable File

#!/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
log_message "Please run this script with sudo."
exit 1
fi
# 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
log_message "Backup drive is not mounted. Attempting to mount $DEVICE..."
mount $DEVICE /mnt/my-drive
if [ $? -ne 0 ]; then
log_message "Error: Failed to mount the drive. Please check the device path and try again."
exit 1
fi
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
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
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/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
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
log_message "Copying VM files from my_platforms..."
cp -r "$VM_DIR_MY_PLATFORMS"/* "$BACKUP_DRIVE/my_platforms/"
log_message "Copy of my_platforms completed."
else
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
log_message "Copying VM files from ldr-server..."
cp -r "$VM_DIR_LDR_SERVER"/* "$BACKUP_DRIVE/ldr-server/"
log_message "Copy of ldr-server completed."
else
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
log_message "Copying Nginx configuration files..."
cp -r "$NGINX_DIR"/* "$BACKUP_DRIVE/home-server_nginx/"
log_message "Copy of Nginx configuration completed."
# Final message
log_message "Latest version of Server VM Backup completed successfully!"
figlet "Done"
# Unmount the external backup drive using the unmount-ssd script
log_message "Unmounting the external backup drive..."
./unmount-ssd
# 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