fbpx rsync Command on Linux: Backup & Sync (Guide) | Certificação Linux

Master rsync in Linux: Sync & Copy Local and Remote Files

📅 12/06/2021 ⏱ 4 min ✍️ Uira Ribeiro
Linuxcommands
Master rsync in Linux: Sync & Copy Local and Remote Files
Exam Pass App — mais de 7.000 questões LPI, offline, em 5 idiomas, disponível no Android e na App Store
Available on
Google Play
Available on
App Store

The rsync command is an essential tool in Linux for anyone who needs to copy, synchronize, or back up files and directories efficiently. With its powerful set of options, rsync allows you to perform incremental data transfers, saving time and bandwidth. In this complete guide, you will learn how to use rsync for different scenarios, from simple copy operations to complex backups.

What is the rsync Command?

rsync is a command-line tool used to synchronize files and directories between two locations. It can be used to copy files locally, between remote systems, or even to create backups. One of the greatest benefits of rsync is its ability to transfer only the parts of files that have been changed, making it very efficient.

Installing rsync on Linux

rsync usually comes pre-installed on most Linux distributions, but if for some reason it is not installed, you can easily do so through your distribution's package manager.

  • On Ubuntu or Debian:
  sudo apt-get install rsync
  • On Fedora:
  sudo dnf install rsync
  • On CentOS/RHEL:
  sudo yum install rsync

Basic rsync Commands

rsync can be used in several ways, depending on your needs. Let's start with the simplest examples.

1. Copying Files Locally with rsync

You can use rsync to copy files from one directory to another on the same machine:

rsync -av /caminho/origem/ /caminho/destino/

Options used:

  • -a: Enables archive mode, which preserves symbolic links, permissions, and timestamps.
  • -v: Enables verbose mode, showing the copy progress.

2. Synchronizing Directories Locally

rsync is often used to synchronize directories. This means it will only copy files that have been modified or are new:

rsync -av /caminho/origem/ /caminho/destino/

This operation ensures that the destination directory will be an exact replica of the source directory, saving time and resources.

3. Transferring Files to a Remote Server

You can also use rsync to transfer files to a remote server:

rsync -av /caminho/origem/ usuario@servidor:/caminho/destino/

Example:

rsync -av /home/user/docs/ [email protected]:/home/user/backup/

This copies all files from the docs directory to the backup directory on the remote server.

4. Receiving Files from a Remote Server

Similarly, you can copy files from a remote server to your local machine:

rsync -av usuario@servidor:/caminho/origem/ /caminho/destino/

Advanced Examples of Using rsync

1. Excluding Files During Synchronization

You can exclude specific files or directories during synchronization using the --exclude option:

rsync -av --exclude 'cache/' /caminho/origem/ /caminho/destino/

This command copies all files except the cache directory.

2. Compression During Transfer

To reduce transfer time, especially on slow networks, you can enable compression using the -z option:

rsync -avz /caminho/origem/ usuario@servidor:/caminho/destino/

Compression is especially useful when transferring text files, which can be compressed significantly.

3. Deleting Files at Destination that No Longer Exist at Source

If you want the destination to be an exact replica of the source, including deleting files that no longer exist at the source, use the --delete option:

rsync -av --delete /caminho/origem/ /caminho/destino/

This removes any files in the destination directory that are not present in the source.

4. Checking What Will Be Synchronized

If you want to see what will be copied or removed without making actual changes, use the --dry-run option:

rsync -av --dry-run /caminho/origem/ /caminho/destino/

This command simulates the operation, showing what would be done without performing any action.

5. Synchronizing Directories via SSH with SSH Key

You can combine rsync with SSH to transfer files securely, using an SSH key for automatic authentication:

rsync -avz -e "ssh -i /caminho/para/chave_ssh" /caminho/origem/ usuario@servidor:/caminho/destino/

When to Use the rsync Command?

  • Regular Backups: rsync is ideal for creating incremental backups, copying only the files that have been changed since the last run.
  • Data Migration: When migrating data from one server to another, rsync ensures that data is copied efficiently and securely.
  • File Synchronization: If you work on multiple machines and need to keep files synchronized, rsync is the perfect solution.

Conclusion

The rsync is a powerful and versatile tool for file synchronization and copying on Linux. It can be used across a wide range of scenarios, from simple local copies to complex transfers between remote servers. Its efficiency in copying only the differences between files makes it an indispensable tool for any systems administrator.

Tip: Explore the advanced options of rsync to optimize your copy and synchronization operations, thus ensuring the integrity and security of your data.

You can also use the tar command to compress files and directories, and if you wish, you can make a simple copy over the network with the scp command.

Uira Ribeiro

Prof. Uirá Ribeiro

Chair do Board do Linux Professional Institute

After years of watching professionals waste hours on simple tasks because they didn't know the right Linux tools, I decided to teach.
Today I'm the author of most of LPI's official Learning Materials and mentor to over 14,000 certified students.

See my full story →

That was just one piece. The course teaches the whole path — from your first command to exam day.

Certificação Linux — a plataforma líder em certificações Linux no Brasil
WhatsApp