Creating backups of my files has always been a hassle for me, and I’ve lost a lot of data on multiple occasions because of it. Turns out it’s because I was working hard instead of smart. Instead of copy-pasting folders between my PC and external drive, I could just use a tool that sends over only the file changes. It takes seconds to back up files with this approach and I can do it by running a single command.
What is rsync, and who is it for?
Remote Sync or rsync is a free and open-source program that has been around since 1996, and it’s now a standard for Linux systems. It is a command-line tool designed for copying files and directories between two points.
Basically, it lets you copy files or directories between two locations incrementally. Unlike copy utilities like cp, it doesn’t copy the entirety of the file every time. It can compare changes between a file in two places and only transfer the changes, significantly cutting down on bandwidth and transfer times.
Since it takes this incremental approach, rsync is essentially syncing files and directories between two locations. It runs checksums to verify that no file contents were lost or corrupted in transit. The two points don’t even have to be local because rsync has networking features too, allowing it to transfer data over the encrypted SSH network protocol.
The function of rsync sounds simple on paper, but that’s what makes it incredibly powerful. System admins keep it in their toolkit to back up and mirror servers. Devs and organizations use it to deploy code or mirror large software repos.
For regular users like us, rsync is the perfect way to back up personal files and media. If you keep a backup external drive, you don’t have to copy entire directories or files every time you create a backup. You can run a single command and watch rsync transfer only the modified or new files to the external drive.
What can you do with rsync
In addition to incremental backups, you can use rsync to create perfect replicas of directories after you’ve made changes in the source. With a single command, rsync can erase any files that are present in the target directory but not in the source directory.
It’ll preserve permissions, ownership, timestamps, and symbolic links when mirroring those directories. Copying and pasting files with GUI file managers can change their metadata (date created or permissions), but rsync creates perfect copies, metadata and all.
For larger file transfers, rsync is the better option because it can compress data during transfer, and it can resume failed or interrupted transfers. File managers also don’t work well when there is a huge number of small files involved, but that doesn’t affect rsync. With rsync, you don’t lose any speed or waste time re-copying data.
You can also use it to transfer data over an SSH tunnel while using the safe features available for local transfers.
To recap: rsync is perfect for creating smart, incremental backups. It can create perfect mirrors of files and folders, metadata included. It’s faster than a graphical file manager. It can resume file transfers if they’re interrupted. It also supports the same features for network transfers.
The rsync basics
You can install rsync from the official repos of your package manager. For Debian or Ubuntu systems, run the following command to install rsync.
sudo apt install rsync
Arch users can install it using pacman.
sudo pacman -S rsync
For Fedora and its derivatives, try this command.
sudo dnf install rsync
To use rsync, you need to know the basic structure of its commands. You start by invoking the rsync program, then you add a flag or a combination of them to select which options should be toggled on, and finally you give it the directory paths of the source and destination.
rsync [options] [source] [destination]
There are a lot of options available in rsync, but you don’t need to know all of them to work with it. These are my go-to flags.
- Archive (-a) is for mirroring directories while perfectly preserving permissions, symbolic links, and metadata.
- Verbose (-v) gives you a log of files being transferred in real time.
- Compress (-z) compresses the files during transfer to make the transfer faster.
- Progress or partial (-p) makes sure you can resume the file transfer if interrupted.
- Dry run (-n) simulates what changes will be made and what the destination will look like before you commit to the changes. It’s good for making sure you aren’t accidentally overwriting or erasing any important files.
Let’s try moving a folder using rsync. I am trying to move this folder labeled “Apple” in my Downloads to my “OS” folder in a different drive.
rsync -avp /home/jellyjam/Downloads/Apple /mnt/7f1e8ebb-93ac-48eb-8138-42380cce3253/OS
The “-avp” flags trigger the archive, verbose, and progress options. Then I just added the paths of my source and destination folders.
If I had included a trailing “https://www.howtogeek.com/” at the end of either path, rsync would just copy the file contents inside the folder to the destination folder. Leaving the path without a slash at the end makes sure it copies the folder itself, not just its contents.
You can use rsync to clone your entire home directory to an external drive just like I did, creating a perfect backup. From time to time, you can run the same command to update the backup with modified or new files. Updating backups with rsync barely takes a fraction of the time it would take to copy everything.
