loading...

Ubuntu Server 18.04 – Putting it all together: Writing an rsync backup script

How to Install Docker on macOS

Let’s close this chapter with a Bash script that will not only prove to be very useful, but will also help you further your skills. The rsync utility is one of my favorites; it’s very useful for not only copying data from one place to another, but also helpful for setting up a backup job. Let’s use the following example rsync command to practice automation:

rsync -avb --delete --backup-dir=/backup/incremental/08-17-2018 /src /target

This variation of rsync is the exact one we used back in Chapter 8, Sharing and Transferring Files. Feel free to consult that chapter for an overview of how this utility works. This example rsync command uses the -a (archive) option, which we’ve discussed before. Basically, it retains the metadata of the file, such as the time stamp and owner. The -v option gives us verbose output, so we can see exactly what rsync is doing. The -b option enables backup mode, which means that if a file on the target will be overwritten by a file from the source, the previous file will be renamed so it won’t be overwritten. Combining these three options, we simplify it into -avb rather than typing -a -v -b. The --delete option tells rsync to delete any files in the target that aren’t present in the source (since we used -b, any file that is deleted will be retained). The --backup-dir option tells rsync that any time a file would’ve been renamed in this way (or deleted), to instead just copy it to another directory. In this case, we send any files that would’ve been overwritten to the /backup/incremental/08-16-2018 directory.

Let’s script this rsync job. One problem we can fix in our script right away is the date that is present inside the the directory we’re using for the --backup-dir. The date changes every day, so we shouldn’t be hard-coding this. Therefore, let’s start our script by addressing this:

#/bin/bash 
CURDATE=$(date +%m-%d-%Y) 

We’re creating a variable called CURDATE. We’re setting it equal to the output of the $(date +%m-%d-%Y)┬ácommand. You can execute date +%m-%d-%Y in your Terminal window to see exactly what that does. In this case, putting a command (such as date) in parenthesis and a dollar symbol means that we’re executing the command in a sub-shell. The command will run, and we’re going to capture the result of that command and store it in the CURDATE variable.

Next, let’s make sure rsync is actually installed, and install it if it’s not:

If [ ! -f /usr/bin/rsync ]; then 
    sudo apt install -y rsync 
fi 

Here, we’re simply checking to see whether rsync is not installed. If it’s not, we’ll install it via apt. This is similar to how we checked for the existence of apache2 earlier in this chapter.

Now, we add the final line:

rsync -avb --delete --backup-dir=/backup/incremental/$CURDATE /src /target

You definitely see the magic of variables in Bash now, if you haven’t already. We’re including $CURDATE in our command, which is set to whatever the current date actually is. When we add it all together, our script looks like this:

#/bin/bash 
CURDATE=$(date +%m-%d-%Y) 
if [ ! -f /usr/bin/rsync ]; then 
    sudo apt install -y rsync 
fi 
rsync -avb --delete --backup-dir=/backup/incremental/$CURDATE /src /target 

This script, when run, will run an rsync job that will copy the contents from /src to /target. (Be sure to change these directories to match the source directory you want to back up and the target where you want to copy it to.) The beauty of this is that /target can be an external hard drive or network share. So in a nutshell, you can automate a nightly backup. This backup, since we used the -b option along with --backup-dir, will allow you to retrieve previous versions of a file from the /backup/incremental directory. Feel free to get creative here as far as where to place previous file versions and where to send the backup.

Of course, don’t forget to mark the script as executable. Assuming it was saved with a name like backup.sh:

chmod +x backup.sh

At this point, you can put this script in a cron job to automate its run. To do so, it’s best to put the script in a central location where it can be found, such as in /usr/local/bin:

mv backup.sh /usr/local/bin

Now, simply create a cron job for this script to be run periodically. Refer to Chapter 6, Controlling and Monitoring Processes for more information on how to create cron jobs if you need a refresher on that.

Comments are closed.

loading...