Hello! Welcome to the final lesson covering new material in your "Troubleshooting, Recovery, and Maintenance" module and for the CompTIA Linux+ course as a whole.
In our last session, we mastered the tar command, learning how to create, list, and extract static file archives. This is a crucial skill for packaging files for storage or transfer. Today, we build on that foundation by exploring a more dynamic and powerful tool for backups and synchronization: rsync.
This lesson addresses the learning outcome: Synchronize files and directories for backups using rsync. While tar is excellent for creating a single, point-in-time snapshot, rsync excels at keeping two locations in sync over time by intelligently transferring only the parts of files that have changed. This efficiency makes it an indispensable tool for system administrators and a vital topic for your exam.
1. What is rsync and Why is it So Efficient?
rsync (remote sync) is a command-line utility used to synchronize files and directories between two locations. These locations can be on the same computer or on different computers connected over a network.
Its main advantage over simple copy commands like cp or scp is its use of a delta-transfer algorithm.
- The first time you run
rsync, it copies all the data, similar tocp. - On subsequent runs, it analyzes the source and destination files to identify the exact differences (the "deltas") and transfers only those changed blocks of data.
This process, illustrated below, dramatically reduces the amount of data sent over the network and makes rsync incredibly fast and efficient for regular backups.

To understand this concept more deeply, let's read a short explanation.
How To Use Rsync to Sync Local and Remote Directories
The article "How To Use Rsync to Sync Local and Remote Directories" from DigitalOcean provides a great definition of rsync and its delta-transfer algorithm.
Read the section titled "Defining Rsync." Focus on how the delta-transfer algorithm distinguishes rsync from other file copy tools.
2. Basic Synchronization and Key Options
Now, let's dive into the practical usage of rsync. The general syntax is straightforward:
rsync [options] source/ destination/
We will follow a comprehensive video tutorial to explore the most important options.
Linux/Mac Terminal Tutorial: How To Use The rsync Command - Sync Files Locally and Remotely
The video "How To Use The rsync Command" by Corey Schafer provides a clear, step-by-step guide to using rsync, starting with local synchronization.
Watch from 00:47 to 05:56. The presenter will guide you through: A basic rsync command to sync files between two local directories. Using the -r (recursive) option to sync entire directories and their subdirectories. The critical difference a trailing slash (/) on the source path makes. Using the -a (archive) option, which is the recommended standard for most backups.
The Trailing Slash: A Critical Detail
As the video highlights, the presence or absence of a trailing slash (/) on your source path is one of the most important concepts in rsync.
rsync -a source/ destination: Copies the contents ofsourceintodestination.rsync -a source destination: Copies the directorysourceitself intodestination, resulting indestination/source.
Getting this wrong is a common mistake, so always double-check your source path.
The -a (Archive) Flag
The -a flag is a convenient combination of several other options. It stands for "archive" and is the go-to option for creating backups because it:
- Recurses into directories (
-r). - Preserves symbolic links (
-l). - Preserves permissions (
-p). - Preserves modification times (
-t). - Preserves group (
-g) and owner (-o).
Using -a ensures that the destination is as close a replica of the source as possible in terms of both content and metadata.
Test your understanding!
You have a directory /home/user/data containing important files. You want to back up its contents to a directory on an external drive mounted at /media/backup. Which command correctly copies the contents of the data directory into the backup directory, preserving all permissions and timestamps?
rsync -r /home/user/data /media/backuprsync -a /home/user/data /media/backup/rsync -a /home/user/data/ /media/backup/
Show answer
The correct command is 3. rsync -a /home/user/data/ /media/backup/
- It uses the
-aflag to preserve permissions, timestamps, etc. - It uses the trailing slash on the source (
/home/user/data/) to copy the contents of the directory, not the directory itself. Command #2 would have created/media/backup/data/.
3. Safe and Effective Mirroring
By default, rsync is additive; it will add new files and update changed ones, but it won't remove files from the destination, even if they've been deleted from the source. To create a true mirror, and to do so safely, you need to know about two more essential options.
Linux/Mac Terminal Tutorial: How To Use The rsync Command - Sync Files Locally and Remotely
Let's continue with Corey Schafer's video to learn about the --dry-run and --delete options.
Watch from 05:56 to 11:23. Pay close attention to: How to use --dry-run (or -n) with -v (verbose) to see what rsync would do without actually making any changes. How the --delete option makes the destination an exact mirror of the source. The critical warning about using --delete—always test with a dry run first!
To summarize this crucial workflow:
- Construct your command: Build your
rsynccommand, including the--deleteflag if you want to mirror. - Perform a dry run: Add the
-n(or--dry-run) flag and run the command (e.g.,rsync -anv --delete source/ destination/). - Review the output: Carefully check the list of files that
rsyncreports it will copy, update, and—most importantly—delete. - Execute for real: If the output matches your expectations, remove the
-nflag and run the command again to perform the actual synchronization.
4. Remote Backups and Network Options
rsync's power truly shines when working with remote systems. It uses SSH for secure transport, and the syntax is very similar to scp.
- Push (local to remote):
rsync -a [source_local] user@host:[destination_remote] - Pull (remote to local):
rsync -a user@host:[source_remote] [destination_local]
When transferring over a network, two additional flags are very useful:
-z: Compresses file data during transfer, which can significantly speed things up on slower connections.-P: A combination of--progressand--partial. It shows a progress bar for each file and allows you to resume an interrupted transfer.
Let's see this in action.
Linux/Mac Terminal Tutorial: How To Use The rsync Command - Sync Files Locally and Remotely
The final segment of the video demonstrates syncing files to and from a remote server, incorporating the network-friendly -z and -P options.
Watch from 11:23 to 16:27. Observe how the syntax is structured for remote hosts and how the -z and -P flags are combined with -a into a common command: rsync -azP.
The combination rsync -azP is a very common and robust command for remote synchronization tasks.
Test your understanding!
You are a developer working on a project in /home/dev/website. You need to push your changes to the live web server. The server's document root is /var/www/html, and your username on the server is webadmin at IP 192.168.1.100. You want to make the remote directory an exact mirror of your local directory, deleting any old files on the server.
What command would you use to perform a safe test run before deploying?
Show answer
A safe test run command would be:rsync -aznvP --delete /home/dev/website/ webadmin@192.168.1.100:/var/www/html/
-a: Archive mode (preserves everything).-z: Compress data for network transfer.-n: Dry run (the most important part for safety).-v: Verbose, to see the planned actions.-P: Show progress.--delete: Specifies the intent to mirror.- The trailing slash on the source ensures you sync the contents.
After reviewing the output and confirming it's correct, you would run the command again without the -n.
Conclusion
You have now reached the end of the new material for your CompTIA Linux+ preparation course! Today, you've learned to use rsync, a powerful and efficient utility that is fundamental to modern backup and file synchronization strategies.
Key Takeaways:
rsyncefficiently synchronizes directories by transferring only the changed parts of files using its delta-transfer algorithm.- The
-a(archive) flag is essential for creating true backups that preserve permissions and metadata. - The trailing slash (
/) on the source path is critical:source/copies contents, whilesourcecopies the directory itself. - Always use
--dry-run(-n) to test your commands before execution, especially when using the destructive--deleteflag. - The
rsync -azPcombination is a robust choice for remote transfers, providing compression, progress information, and resumability.
Over the span of this course, you have journeyed from the core architecture of Linux to mastering the command line, managing users, software, security, storage, and networking. You have acquired the essential skills of a Linux system administrator.
Your next step is to review the topics we've covered, practice the commands in your virtual machine, and solidify your knowledge. We wish you the best of luck on your CompTIA Linux+ exam