Hello everyone,
I recently transitioned to Linux and started thinking about a cost-effective and efficient backup solution. After some experimentation, I developed a method that perfectly meets my needs, and I hope it will be useful for you as well.
My Solution to the Backup Problem
This script is designed to back up my SmarterMail server directories to an external hard drive. The script is structured to stop the SmarterMail service during the backup process to ensure data consistency, perform the backup with a real-time progress display, and then restart the service once the backup is complete.
What the Script Does
• Stops the SmarterMail service to ensure no data is modified during the backup.
• Backs up the directories /var/lib/smartermail and /etc/smartermail/ to an external hard drive located at /mnt/backup_vipmail/backup.
• Displays the backup progress in real-time, allowing you to monitor how far along the process is.
• Restarts the SmarterMail service after the backup is completed.
• Logs all steps and status messages in a log file located at /root/smartermail_backup.log.
Installing the Tool for Progress Display
To display the backup progress as a percentage, the script uses the pv (Pipe Viewer) tool. You can easily install pv on Ubuntu with the following command:
sudo apt-get install pv
Creating and Setting Up the Script
1. Create the Script: Create the script in the /root directory:
sudo nano /root/smartermail_backup.sh
2. Add the Script Content: Insert the following content into the script:
#!/bin/bash
SOURCE_DIR1="/var/lib/smartermail"
SOURCE_DIR2="/etc/smartermail"
DEST_DIR="/mnt/backup_vipmail/backup"
LOGFILE="/root/smartermail_backup.log"
log_and_echo() {
echo "$1" | tee -a $LOGFILE
}
mkdir -p $DEST_DIR
log_and_echo "$(date): Stopping SmarterMail service..."
sudo service smartermail stop
log_and_echo "$(date): SmarterMail service stopped."
log_and_echo "$(date): Starting backup of $SOURCE_DIR1..."
tar -cf - $SOURCE_DIR1 | pv -s $(du -sb $SOURCE_DIR1 | awk '{print $1}') | tar -xf - -C $DEST_DIR
log_and_echo "$(date): Backup of $SOURCE_DIR1 completed."
log_and_echo "$(date): Starting backup of $SOURCE_DIR2..."
tar -cf - $SOURCE_DIR2 | pv -s $(du -sb $SOURCE_DIR2 | awk '{print $1}') | tar -xf - -C $DEST_DIR
log_and_echo "$(date): Backup of $SOURCE_DIR2 completed."
log_and_echo "$(date): Starting SmarterMail service..."
sudo service smartermail start
log_and_echo "$(date): SmarterMail service started."
log_and_echo "$(date): Backup successfully completed."
3. Set the Permissions: Make the script executable:
sudo chmod +x /root/smartermail_backup.sh
4. Test the Script: Run the script manually to ensure everything works correctly:
sudo /root/smartermail_backup.sh
After running the script, check the log file at /root/smartermail_backup.log to confirm there were no errors and that the backup completed successfully.
Setting Up a Cron Job
To automate the script execution at 2 AM every night, you can set up a cron job as follows:
1. Open the Crontab File:
sudo crontab -e
2. Add the Cron Job:
Add the following line to schedule the script to run at 2 AM daily:
0 2 * * * /root/smartermail_backup.sh
Save the file and exit the editor.
I hope you find this script helpful and that it meets your backup needs. With this method, I’ve been able to avoid costly backup solutions like Acronis, and it delivers a fast and reliable result. For me, backing up 50 GB takes about 4 minutes – pretty impressive!
Best of luck with setting up your own backup solution, and I’m looking forward to your feedback!
George