Overview

One of my favorite utilities on the Linux command-line, and block storage is one of my favorite features on Linode’s platform, so in this article I get to combine both of these together – because what I’m going to be doing is show you how to use rsync to copy data from one server to another, in the form of a backup. What’s really cool about this, is that this example will utilize block storage.

Note: I’ll be using the Nextcloud server that was set up in a previous article, but it doesn’t really matter if it’s Nextcloud – you can back up any server that you’d like.

Setting up our environment

On the Linode dashboard, I created an instance named “backup-server” to use as the example here. On your side, be sure to have a Linode instance ready to go in order to have a destination to copy your files to. Also, create a block storage volume to hold the backup files. If you don’t already have block storage set up, you can check out other articles and videos on Linode’s documentation and YouTube channel respectively, to see an overview of the process.

Again, in the examples, I’m going to be backing up a Nextcloud instance, but feel free to back up any server you may have set up – just be sure to update the paths accordingly to ensure everything matches your environment. In the Nextcloud video, I set up the data volume onto a block storage volume, so block storage is used at both ends.

First, let’s create a new directory where we will mount our block storage volume on the backup server. I decided to use /mnt/backup-data:

sudo mkdir /mnt/backup-data

Since the backup server I used in the example stores backups for more than one Linode instance, I decided to have each server back up to a sub-directory within the /mnt/backup-data directory.

sudo mkdir /mnt/backup-data/nextcloud.learnlinux.cloud

Note: I like to name the sub-directories after the fully qualified domain name for that instance, but that is not required.

Continuing, let’s make sure our local user (or a backup user) owns the destination directory:

sudo chown jay:jay /mnt/backup-data/nextcloud.learnlinux.cloud

After running that command, the user and group you specify will become the owner of the target directory, as well as everything underneath it (due to the -R option).

Note: Be sure to update the username, group name, and directory names to match your environment.

So now what we’re going to do is prepare the source server (the server that we want to back up to our backup server). Depending on the permission structure of the source server, be sure to either switch to root or whichever user you happen to use that has access to all the files that you wish to back up. On that server, we’ll perform a “dry run” of the backup, before performing an actual backup.

Testing the rsync backup (Dry Run)

It’s very important to perform a “dry run” of an rsync backup, before actually performing the backup – that will help you understand what rsync wants to do, before it actually does it. The following command can be used as an example:

rsync --dry-run -avz /mnt/nextcloud-data/ [email protected]:/mnt/backup-data/nextcloud.learnlinux.cloud

In that example, we’re using the --dry-run option to clarify that we’d like to run a dry run, and then also options -avz, which are archive, verbose, and compress respectively. Of course, be sure to update everything in the command to match your environment, and this is the last time I’ll mention that so please keep that in mind going forward.

The rsync command provided will connect to the target server, even though it’s a dry run. And the output actually goes by very quickly – but the output should give you an idea of what would’ve been backed up, had we not included the --dry-run option. Take a look at the output, and ensure that the simulated summary matches what you’d like to happen.

Performing an rsync backup

Now that you’ve tested rsync, if you’re ready to create the backup, you can do so by simply removing the --dry-run option from the command:

rsync --dry-run -avz /mnt/nextcloud-data/ [[email protected]](mailto:[email protected]):/mnt/backup-data/nextcloud.learnlinux.cloud

If successful, rsync should connect to the backup server, and transfer the files within the designated path. The process may take a while, if you’re backing up a large number of files.

Creating an rsync backup script

If you think you’ll use the same rsync command multiple times in the future, you should consider scripting it. Below is an example rsync script, which utilizes the same example scenario that we’ve been using:

#!/bin/bash DATE=$(date '+%F')$ rsync --dry-run -avz /mnt/nextcloud-data/ [email protected]:/mnt/backup-data/nextcloud.learnlinux.cloud/$DATE

Save the file, then mark it executable:

chmod +x backup.sh

Then, when you’re ready, run the script:

./backup.sh

This is actually my favorite method of running rsync. With the $DATE variable, every day the backup script runs, a new directory named after the current date will be created on the backup server, so that way you’ll immediately be able to find a backup from a specific date should you need to restore a file.

Closing

And that’s basically it – there’s a great deal more that you can do with the rsync command, but the example given here should be effective in helping you back up data to a backup server. There’s additional options while running rsync that you might consider adding, but for the goal of this article – we should be able to complete our goal with these examples.

You can watch the tutorial here:

[embedded content]

Posted by Contributor