Photo by Markus Spiske on Unsplash

Are you overdue for a backup? For any work that you do on your computer, having backup copies of your files and data is critically important. That’s because systems crash and hard drives fail, and if you haven’t stored copies of your work, your out of luck. If you’re a system admin or a security professional, your employer’s policies likely require you to backup company files regularly.

Unfortunately, creating backups is often a slow, tedious and boring. There are, however, some better, smarter, and more efficient ways to get this job done. This post focuses on one tool that can make that process easier – Rsync.

Rsync is a Linux utility used for backup and file recovery. It transfers and synchronizes files between a machine and an external hard drive, or across a network. Rsync makes the process more efficient by comparing the modification dates, checksums and sizes of files, and only backing up when needed. Even if a small change has been made in a small region, Rsync only needs to transfer the changed region.

First, let’s cover some Rsync basics. How can you create a backup on a remote server using Rsync? How can you update the backup files? And how can you delete them? The simplest way to use Rsync to copy a directory is by using the following command:

rsync -r SRC DEST


In this syntax, SRC is the source directory of your files, and DEST is the destination that you want to copy them to. When uploading to a remote server, SRC would be a folder on the local machine, and DEST would be a folder on a remote server.

Users also have several options for customizing Rsync’s behavior. One of the most useful of these is -a, which tells Rsync to copy all files in the directory recursively, preserving timestamps, ownership information and permissions (but not hard links, ACLs or extended attributes). To upload a folder to a backup server, use the following command:

rsync -a local_directory username@remote_server:/path/to/backup_directory


Preserving extended attributes and ACLs requires root privileges on the destination location. Users can use the flag --fake-super to store these metadata in user-extended attributes.

Alternatively, a user might want to delete extra files from a backup server. The --delete option tells Rsync to delete files in the DEST directory that is not also present in the SRC directory. Here’s the command:

rsync -a --delete local_directory username@remote_server:/path/to/backup_directory


Congratulations! By following these steps, you have created a backup of your files. When the time comes, how can you download the backups to your local machine? You can simply reverse the SRC and DEST locations, and files will be downloaded from the remote server instead. Use the following command to do so.

rsync -a username@remote_server:/path/to/backup_directory local_directory


## Dry Runs

When possible, it’s a good practice to preview changes prior to actually making them in your directories. This is where Rsync’s dry run feature come into play. The --dry-run, or -n option tells Rsync to not execute any file transfers, but to show details of the file transfer that would happen if the changes were actually made. Here’s an example.

rsync -av --dry-run local_directory username@remote_server:/path/to/backup_directory


The -v option can be used if a user wants to see the verbose output. He or she can even specify a log file to store the results, if so desired:

rsync -av --dry-run local_directory username@remote_server:/path/to/backup_directory --log-file=/path/to/log_file


## Using The Rsync Protocol

In all of the above examples, Rsync will use a remote shell such as SSH as the transport mechanism. Alternatively, users can connect to a remote Rsync daemon directly by specifying the Rsync protocol for the remote path.

rsync -a rsync://username@remote_server:/module_name/file_to_download local_directory


## Rsync Server Configuration

A user can also launch an Rsync daemon on his or her local machine. They might want to do that if they want to share access to their files with others, or if they want to be able to upload files into their backup directories. The Rsync daemon’s configuration file is located at /etc/rsyncd.conf. If it is not already present, the developer will need to create and edit it. Here’s what a basic configuration file looks like:

uid = root
gid = root
use chroot = yes
log file = path to daemon lock file
pid file = path to process id file
# all hosts can connect
hosts allow = *

# module options
# module name
[module1]
comment = description for this module
# file path associated with this module
path = /vickie/public_rsync


After you’re done specifying the configuration, you can launch the daemon by running the following command:

rsync --daemon


Now the daemon is launched and Rsync is ready to accept connections. Anyone can now connect to your Rsync daemon by using the Rsync protocol. This command will download the file file.txt from your public Rsync directory located at /vickie/public_rsync. Be aware, however, that the Rsync protocol lacks encryption.

rsync -a rsync://username@your_server:/module1/file.txt local_directory


## Conclusion

Rsync is a useful utility that streamlines the backup process. In this post, we covered how to upload and download files through Rsync, and how to create a local Rsync server by launching the daemon. Don’t be the person who didn’t get around to doing a backup right before their system crashed. Good luck!