Back up using RSync

From GNU/Linux Guide
Jump to navigation Jump to search

Introduction

There are lots of options for backing up on GNU/Linux. You can use software applications, most which employ a familiar GUI. But this guide focuses on using the CLI to back up your data. The advantages of this approach compared to the former is that it won't become outdated any time soon and that it avoids additional bugs that software applications may introduce. In fact, most back up software are graphical front-ends for CLI commands. By learning to use the CLI commands directly, you avoid the additional issues that additional software brings and you won't be unnecessarily dependent upon said software.

There are different devices to which to back up your data to and different ways of accessing those devices. For example, you might want to back up your data to another computer remotely. The main focus of this guide, however, is backing up your data from the computer you physically use to a local external hard drive or solid state drive.

More than one command can be employed for backing up your data, but the most versatile and appropriate one for most backup operations is Rsync. Please check out the Rsync page for an introduction to this command before continuing.

Now that you've been introduced, let's dive into actually using it to back up your data!

Using Rsync

As you read on the Rsync page, Rsync has a lot of options. Don't let this intimidate you, though. Rsync's basic functions are really easy to use. For the purposes of this How-To Guide, you'll only need to use the following options:

  • -r (Recursive; includes sub-folders)
  • -a (Archive mode; includes sub-folders, while preserving permissions, groups, users, and times)
  • -v (Verbose; the entire process is printed to the terminal rather than remain hidden)
  • --delete (deletes files on the drive being copied to that no longer exist on drive being copied from)

The "recursive" options tells Rsync to copy all the folders within the folder you are copying. If you don't use this option, it will only copy the files in that folder, without copying the contents of any folders inside that folder.

The "archive" option does what "-r" does, in addition to preserving permissions, which is usually a good idea. To use this option, you should be superuser.

The "verbose" option lets you see what Rsync is doing on the screen, rather than leaving you in the dark until its finished. This is usually a good idea, too, though not indispensable.

The "delete" option deletes files on the drive being copied to that no longer exist on drive being copied from. This lets you synchronize both. This is a good idea if you are using Rsync as a backup tool, which discards any data you've chosen to delete from the original source. If you do not use this option and need to restore the backup later, it will restore everything you had already deleted or changed as well as the new data. On the other hand, if you are trying to have a backup or archive of all files and file versions, old and new, you won't want to use this.

Local Backup

To backup to a hard drive or another folder, all you need to type in the terminal, in this order, are:

  • "rsync", in order to invoke the Rsync utility.
  • The options you want enabled.
  • The source path (where the folder or file is being copied from)
  • The target path (where the folder or file is being copied to)

Each of these parts must be separated by a space. Folder paths also need to be in single quotation marks, unless there are no spaces in the paths, in which case they are optional. If the target folder exists Rsync will use this existing folder. If this folder does not exist, Rsync will create it.

The paths you type will depend upon where your folders and media are. An easy way to give Rsync folder paths, without having to remember them exactly is to use your file manager's GUI to drag your folders to the terminal window. On most systems these days, the terminal will automatically fill with the path of the folder, file or media you're dragging into the window. So instead of typing them in manually, you can just drag the first folder in, enter a space and then drag the backup destination into the same terminal window.

The format for all operations with RSync is the following:

rsync [options] '[source path]' '[target path]'

In essence, that's it.

Examples

So, for example if you wanted to backup all the contents of folder "My Work" from your home folder to your backup drive, you might type:

rsync -r '/home/username/' 'media/username/Backup Drive'

Watch your path!

If you are trying to copy the contents of a folder that already exists on the target, be sure to give the path to the folder on the target without including the name of the folder itself. For example, let's say you have a folder named "Important Files" that you've previously backed up to your backup drive. Let's say this folder exists at the root of your home folder on the source and at the root of the backup drive target.

To copy the new files to the target, then you you might type:

rsync -r '/home/username/Important Files' 'media/username/Backup Drive'

What you would not type is the following:

rsync -r '/home/username/Important Files' 'media/username/Backup Drive/Important Files'

Why? Because this command would actually create another "Important Files" folder inside your previously backed up "Important Files" folder, instead of simply copying over the new files to the existing folder on the target. So you need to leave "Important Files" (or obviously whatever the name of your folder is) out of the path if your intension is to copy the files from the source folder to the same-named target folder.

Watch your slash!

To illustrate a further detail, imagine you have a file named "Thesis.odt" inside folder "Important Files", and you type:

rsync -r '/home/username/Important Files' 'media/username/Backup Drive'

This will backup your folder and its contents to your Backup Drive. Your file will wind up in:

media/username/Backup Drive/Important Files/Thesis.odt

But let's say you type:

rsync -r '/home/username/Important Files/' 'media/username/Backup Drive'

Notice the extra "/" or slash in the source path. This will copy all the contents of "Important Files", without copying the "Important Files" folder itself. So your file will wind up in:

media/username/Backup Drive/Thesis.odt

So mind the slash in your path in accordance to your will.

Synchronizing

The above examples will copy files and folders from the source to the target, but won't synchronize them. So, every time you backup, Rsync will copy or update your current files over, but won't delete those files you've deleted from the source in-between backups. This way, Rsync keeps both your current and old files. If you want to have a backup of both your current and older data, this would do fine.

But if you want to synchronize your source and target, so that both have the same exact files and number of files, you'll need to use the “--delete” option to tell Rsync to keep only your current files in your backup. This option will not delete any files or folders on the source. It will only delete those files and folders on the target which have already been deleted on the source.

To do this, you might type:

rsync -r --delete '/home/username/' 'media/username/Backup Drive'

Archiving

All the former examples will copy files from the source to the target and the previous one will even synchronize them. But files have many attributes, such as their last modification time, and on GNU/Linux they also have permission attributes, and several others. And we must explicitly tell Rsync to copy these attributes over along with their files if we want to keep them in the backup. There are several options to do exactly this. However, there is one, easy-to-remember option that covers all of the attributes we need. And that's "archive" or "-a". The "-a" option also covers the "recursive" "-r" option, so whenever you use "-a", you don't need to use "-r". The only catch to this is that you need to be superuser.

So you might type:

sudo rsync -a '/home/username/Important Files' 'media/username/Backup Drive'

This would copy your "Important Files" folder with all its files, sub-folders and sub-files with all their permissions and other attributes intact over to "Backup Drive".

Putting it all Together

So let's say you wanted to backup your files with their attributes intact, but don't want to keep your previously deleted files on your backup. Then you might type:

sudo rsync -a --delete '/home/username/Important Files' 'media/username/Backup Drive'

If you wanted to see what Rsync was doing, you'd add the "-v" option:

sudo rsync -av --delete '/home/username/Important Files' 'media/username/Backup Drive'

Conclusion

Really, there's nothing more to using Rsync. You can add options in any combination. You can also experiment with options we haven't covered in this How-To Guide, but this is really all you need for regular backups. There's no fancy software to buy or subscribe to. Rsync is as solid as it gets. And once you get over your "Terminal-fears", you realize that it's as easy as it gets, too.

Rsync was first released almost 20 years ago (and then it was only replacing a command with similar functionality). And if I were a betting man, I would wager that it'll be around for at least another 20 years. Learn it and you won't be dependent upon whatever questionable-quality commercial software is in vogue.