This document is a comprehensive guide on creating a bash script to automate data backups using the rsync
utility. It is designed for individuals who are already familiar with Linux and Bash scripting.
Overview
rsync
is a fast, versatile, remote (and local) file-copying tool used widely for backups and mirroring. We will create a Bash script to leverage rsync
for creating automatic backups of your data.
Prerequisites
- A working Linux environment
- Familiarity with Bash scripting
rsync
installed in your system (you can install it using your package manager;sudo apt-get install rsync
for Ubuntu/Debian, orsudo yum install rsync
for CentOS/RHEL)
Create your bash script file
First, create a new bash script file. We’ll call it backup.sh
.
touch backup.sh
Make the file executable.
chmod +x backup.sh
Define variables for source, destination, and log file
Open backup.sh
in a text editor. We’ll start by defining some variables. The SOURCE
and DESTINATION
paths should be customized based on your needs.
#!/bin/bash
# User-defined variables
SOURCE="/path/to/source/directory"
DESTINATION="/path/to/destination/directory"
LOG_FILE="/path/to/log/file"
Integrate User Customization
To enable user customization, we’ll accept command line arguments that will overwrite the default SOURCE
, DESTINATION
, and LOG_FILE
variables.
#!/bin/bash
# User-defined variables
SOURCE="/path/to/source/directory"
DESTINATION="/path/to/destination/directory"
LOG_FILE="/path/to/log/file"
# Check for user input
while getopts s:d:l: option
do
case "${option}" in
s) SOURCE=${OPTARG};;
d) DESTINATION=${OPTARG};;
l) LOG_FILE=${OPTARG};;
esac
done
Now, you can run the script with custom parameters like this:
./backup.sh -s /custom/source -d /custom/destination -l /custom/logfile
Use rsync for backup
Now, let’s use rsync
to make the backup. We’ll log the output to the specified log file.
#!/bin/bash
# User-defined variables
SOURCE="/path/to/source/directory"
DESTINATION="/path/to/destination/directory"
LOG_FILE="/path/to/log/file"
# Check for user input
while getopts s:d:l: option
do
case "${option}" in
s) SOURCE=${OPTARG};;
d) DESTINATION=${OPTARG};;
l) LOG_FILE=${OPTARG};;
esac
done
# Use rsync to make the backup, and append output to log file
rsync -avz --delete $SOURCE $DESTINATION >> $LOG_FILE 2>&1
The -avz
option enables archive mode, verbose output, and compression. The --delete
option deletes extraneous files from the destination directories.
Error Handling and Notifications
To handle errors and notify the user, we’ll add some error handling code and a completion message.
#!/bin/bash
# User-defined variables
SOURCE="/path/to/source/directory"
DESTINATION="/path/to/destination/directory"
LOG_FILE="/path/to/log/file"
# Check for user input
while getopts s:d
:l: option
do
case "${option}" in
s) SOURCE=${OPTARG};;
d) DESTINATION=${OPTARG};;
l) LOG_FILE=${OPTARG};;
esac
done
# Use rsync to make the backup, and append output to log file
rsync -avz --delete $SOURCE $DESTINATION >> $LOG_FILE 2>&1
# Check rsync exit status
if [ "$?" -eq "0" ]
then
echo "Backup completed successfully at $(date)" >> $LOG_FILE
else
echo "Backup failed at $(date)" >> $LOG_FILE
fi
Now, your backup script will log whether the backup was successful or not, along with the timestamp.
Conclusion
This guide has provided a complete walkthrough on creating a Bash script for automating data backups using rsync
. By following these instructions, you’ll have a customizable and reliable script for backing up your important files.
Remember to regularly check the log file for any potential errors. Adjust the rsync
options or script variables as needed to best suit your backup needs.
NOTE: It’s highly recommended to test the script with non-critical data before using it on actual important data to avoid any unforeseen data loss.