Ubuntu: How to write an rsync backup script

Updated: January 28, 2024 By: Guest Contributor Post a comment

Introduction

When it comes to backing up your data on Ubuntu, rsync is an incredibly powerful tool that you can tailor to fit nearly any backup strategy. Writing an rsync backup script will not only give you confidence in your backup process but can also save you time by automating these tasks. In this tutorial, we will go through the process of creating simple to advanced rsync backup scripts suitable for various scenarios.

Getting Started with rsync

First, ensure that rsync is installed on your Ubuntu system. Most modern Unix-like systems, including Ubuntu, come with rsync pre-installed. To confirm, open your terminal and run:

rsync --version

If rsync is not installed, you can easily install it via the package manager:

sudo apt-get update
sudo apt-get install rsync

Let’s start with the basic usage of rsync:

rsync -av /source/directory /destination/directory

This command will synchronize the contents of /source/directory to /destination/directory, maintaining file permissions and modification times. The -a option stands for “archive”, which incorporates multiple options to preserve the filesystem attributes, and the -v option is for verbosity, to provide detailed output about the sync process.

Basic Backup Script

Creating a basic backup script in Ubuntu is straightforward. Begin by creating a new file using a text editor, such as nano:

nano ~/rsync_backup.sh

Add the following lines to create a script that performs a simple backup:

#!/bin/bash

# Source and destination directories
dest="/backup/destination"
src="/home/your_username"

# Run rsync command
rsync -av --delete "${src}" "${dest}"

After saving and exiting the editor, make your script executable:

chmod +x ~/rsync_backup.sh

Run your new backup script:

./rsync_backup.sh

The –delete option ensures that files no longer present in the source will be removed from the destination, keeping both directories in sync. Remember to replace /home/your_username with the actual directory you wish to backup, and /backup/destination with your preferred backup location.

Incremental Backups

An incremental backup only copies the changes since the last backup, saving disk space and transfer time. To perform an incremental backup, add the following to your script:

dest="$HOME/backup/$(date +%Y%m%d%H%M)"

rsync -av --link-dest=$HOME/backup/current "$src" "$dest"

# Update the current symlink to point to the latest backup
rm -f $HOME/backup/current
ln -s "$dest" $HOME/backup/current

This will create a new backup directory with the current date and time, and use hard links for files that haven’t changed, saving space. After completion, it updates a symbolic link called ‘current’ to point to the latest backup.

Remote Backups

You can use rsync to backup to a remote server as well. Here is a basic remote backup example:

rsync -avz --delete "${src}" user@remote-server:/path/to/backup

The -z option compresses the data during the transfer. Make sure to replace user with the actual remote username and remote-server with the remote server’s address.

Advanced Backup Script with Logging and Error Handling

For a robust backup script, you can add logging and error handling:

#!/bin/bash

log="${HOME}/backup/rsync_backup_
.log"dest="${HOME}/backup/destination"
src="/source/directory"

# Start backup
{ 
    echo "Starting backup: $(date '+%Y-%m-%d %H:%M:%S')";
    rsync -avz --delete --exclude-from='${HOME}/rsync-exclude.txt' "${src}" "${dest}";
    echo "Backup completed: $(date '+%Y-%m-%d %H:%M:%S')";
} | tee -a "${log}"

# Error handling
status=$?
if [ $status -ne 0 ]; then
    echo "Rsync failed with exit code $status" | tee -a "${log}"
    exit $status
else
    echo "Rsync completed successfully." | tee -a "${log}"
fi

Conclusion

Now that you have a collection of rsync backup scripts, from basic to advanced, you’re well-equipped to handle the data backups on your Ubuntu system. Remember to test your backups periodically to ensure that they’re working as expected. Happy scripting!