Skip to content

Rsync Command in Linux with Examples

The rsync command is a powerful tool that is used for data synchronization and backup purposes in Linux. It is a popular command that is widely used by system administrators and developers to efficiently transfer data between different systems.

With the help of rsync, users can copy, synchronize, and backup files and directories across different servers and systems.

Understanding the rsync command is essential for anyone who works with Linux systems and data synchronization. The command is designed to be efficient, fast, and reliable, making it an ideal tool for transferring large amounts of data.

Setting up rsync is straightforward and can be done in just a few steps. Once set up, users can start using the basic features of rsync to transfer files and directories between systems.

Basic usage of rsync involves specifying the source and destination directories and choosing the appropriate options for the transfer.

Advanced features of rsync include options for filtering files, preserving file permissions, and transferring only the changed parts of files.

Optimizing rsync performance is also possible by adjusting various options and settings. With practical examples, users can learn how to use rsync for backup and recovery purposes, making it an essential tool for any Linux user.


Key Takeaways

  • The rsync command is a powerful tool for data synchronization and backup purposes in Linux.
  • Setting up rsync is straightforward and can be done in just a few steps.
  • With practical examples, users can learn how to use rsync for backup and recovery purposes, making it an essential tool for any Linux user.

Understanding Rsync Command

Rsync Syntax and Options

Rsync is a powerful command-line tool used for copying and synchronizing files and directories in Linux. The basic syntax of the rsync command is:

rsync [options] source destination

Here, the source and destination can be either a local path or a remote path specified in the format of user@host:/path/to/directory.

The following are some of the commonly used options of the rsync command:

OptionDescription
-aArchive mode, which preserves the permissions, ownership, and timestamps of the files being transferred
-vVerbose mode, which displays detailed information about the transfer process
-zCompression mode, which compresses the data being transferred to reduce the network bandwidth usage
--deleteDeletes the files in the destination directory that are not present in the source directory
--excludeExcludes the specified files or directories from the transfer process

Rsync vs SCP and CP

Rsync is often compared with SCP and CP commands, as they all are used for copying files and directories in Linux. However, there are some key differences between them.

SCP is a secure copy command that is used for transferring files between a local and a remote host over a secure SSH connection. It is a simple tool that does not provide any advanced options for synchronization or incremental backups.

CP is a basic copy command that is used for copying files and directories within a local system. It does not provide any options for transferring files over a network or synchronizing directories.

In contrast, rsync is a versatile tool that provides advanced options for efficient copying and synchronization of files and directories over a network. It uses a delta-transfer algorithm that transfers only the differences between the source and destination files, which makes it faster and more efficient than SCP and CP commands.

Overall, rsync is a powerful tool that can be used for various purposes, such as backup, mirroring, and remote file transfer, in Linux systems.

SCP Command in Linux: 14 Examples for Secure File Transfer

Setting Up Rsync

Installing Rsync

Before using rsync, it must be installed on the system. Rsync is available on most Linux distributions, including Ubuntu, Debian, CentOS, and Fedora.

The installation process varies depending on the distribution, but it can be done easily using the package manager.

To install rsync on Ubuntu or Debian, open a terminal and run the following command as root:

sudo apt-get install rsync

To install rsync on CentOS or Fedora, run the following command as root:

sudo yum install rsync

Rsync over SSH

Rsync can also be used over SSH, which provides secure data transfer over the network. To use rsync over SSH, both the source and destination machines must have SSH installed and configured.

To transfer files from the source machine to the destination machine over SSH, use the following command:

rsync -avz -e ssh /path/to/source user@destination:/path/to/destination

Here, -e ssh specifies that rsync should use SSH for the transfer, and user is the username on the destination machine.

To transfer files from the destination machine to the source machine over SSH, use the following command:

rsync -avz -e ssh user@source:/path/to/source /path/to/destination

In both cases, -avz specifies that rsync should preserve file permissions, ownership, and timestamps, and compress the data during transfer to reduce the amount of data sent over the network.

Basic Rsync Usage

Rsync is a powerful command-line tool used to synchronize files and directories between local and remote systems. It is commonly used for backup and mirroring purposes. In this section, we will explore the basic usage of rsync command with examples.

Copying Files Locally

To copy files locally, use the rsync command followed by the source and destination directories. For example, to copy all files and directories from /home/user1 to /backup directory, run the following command:

rsync -av /home/user1 /backup

In the above command, -a option preserves the permissions, ownership, and timestamps of the files and directories, while -v option displays the verbose output.

Copying Files to a Remote System

To copy files to a remote system, use the rsync command followed by the source and destination directories in the following format:

rsync [OPTIONS] SOURCE [USER@]HOST:DESTINATION

For example, to copy all files and directories from /home/user1 directory to the remote system with IP address 192.168.1.100 and destination directory /backup, run the following command:

rsync -av /home/user1 [email protected]:/backup

In the above command, -a option preserves the permissions, ownership, and timestamps of the files and directories, while -v option displays the verbose output. user is the username of the remote system, and 192.168.1.100 is the IP address of the remote system.

Rsync can also be used with SSH for secure file transfer. To use SSH, add -e ssh option to the rsync command. For example:

rsync -av -e ssh /home/user1 [email protected]:/backup

In the above command, -e ssh option enables SSH encryption for secure file transfer.

SCP Command in Linux: 14 Examples for Secure File Transfer

Advanced Rsync Features

Archive Mode

One of the most powerful features of rsync is the archive mode, which can be enabled with the -a option. This option preserves all of the file attributes, including permissions, ownership, timestamps, and symbolic links. It also enables recursion and preserves the directory structure. In other words, it makes rsync behave like a backup tool, which is why it is often used for backup and synchronization tasks.

Excluding Files

There may be times when you want to exclude certain files or directories from the synchronization process. Rsync provides several options for this purpose, such as --exclude, --exclude-from, and --include-from. The --exclude option allows you to specify a pattern that matches files or directories to exclude, while the --exclude-from and --include-from options allow you to specify a file that contains a list of patterns to exclude or include, respectively.

For example, to exclude all files with the extension .log and the directory temp from the synchronization process, you can use the following command:

rsync -av --exclude '*.log' --exclude 'temp/' source/ destination/

Dry Run Option

Sometimes you may want to see what rsync would do without actually copying any files. This is where the --dry-run option comes in handy. When this option is enabled, rsync will simulate the synchronization process and show you a list of files that would be copied or deleted, but it won’t actually perform any actions.

For example, to perform a dry run of the synchronization process between the directories source and destination, you can use the following command:

rsync -av --dry-run source/ destination/

By default, rsync shows only the names of the files that would be copied or deleted. If you want to see more detailed information, such as the progress and the amount of data transferred, you can use the --verbose and --progress options, respectively.

Overall, rsync is a powerful and versatile tool that can be used for a wide range of synchronization and backup tasks. Its advanced features, such as archive mode, excluding files, and dry run option, make it an indispensable tool for system administrators and power users alike.

Rsync for Backup and Recovery

Rsync is an efficient and reliable tool for creating and restoring backups in Linux. It can copy files and directories recursively while preserving permissions, ownership, timestamps, and links. In this section, we will explore how to use Rsync for backup and recovery purposes.

Creating Backups

To create a backup using Rsync, you need to specify the source and destination directories. The source directory is the one you want to back up, and the destination directory is where you want to store the backup. Here is an example command:

rsync -avz --delete /home/user/documents/ /mnt/backup/documents/

In this command, the -a option tells Rsync to preserve permissions, ownership, timestamps, and links. The -v option enables verbose output, and the -z option compresses the data during transfer. The --delete option removes any files from the destination directory that are not present in the source directory.

Restoring from Backups

To restore a backup using Rsync, you need to specify the source and destination directories in reverse order. The source directory is the backup directory, and the destination directory is where you want to restore the files. Here is an example command:

rsync -avz --delete /mnt/backup/documents/ /home/user/documents/

In this command, the options are the same as in the previous example. The only difference is the source and destination directories.

By using Rsync for backup and recovery, you can ensure that your data is safe and easily recoverable in case of any disaster. With its powerful features and options, Rsync is a must-have tool for any Linux user who values data security and reliability.

Optimizing Rsync Performance

Bandwidth Limiting

By default, rsync will use all available bandwidth to transfer files. However, this can cause problems if other applications on the network are also trying to use the same bandwidth. To avoid this, you can use the --bwlimit option to limit the amount of bandwidth that rsync uses. For example, to limit rsync to 1 MB/s, you would use the following command:

rsync --bwlimit=1000 /path/to/source/ user@remote:/path/to/destination/

Speeding Up Transfers

There are several ways to speed up rsync transfers. One way is to use the --compress option, which compresses data before sending it over the network. This can significantly reduce transfer times, especially for large files. For example:

rsync --compress /path/to/source/ user@remote:/path/to/destination/

Another way to speed up transfers is to use the --partial option, which allows rsync to resume interrupted transfers. This can be useful if the transfer is interrupted for any reason, such as a network outage or a power failure.

rsync command not found

If you receive an error message that the rsync command is not found, it may not be installed on your system. To install rsync on Debian-based systems, you can use the following command:

sudo apt-get install rsync

On Red Hat-based systems, you can use the following command:

sudo yum install rsync

Once rsync is installed, you should be able to use it as normal.

Practical Rsync Examples

Rsync is one of the most powerful tools for synchronizing files and directories between different systems. It is widely used for mirroring, backup, and migration purposes. Here are some practical examples of using rsync command in Linux.

Synchronizing Files

To synchronize two directories, use the following command:

rsync -avh /path/to/source/ /path/to/destination/

This command will copy all the files and directories from the source directory to the destination directory. The -a option stands for archive mode, which preserves all the file attributes and permissions. The -v option stands for verbose mode, which displays the progress of the transfer. The -h option stands for human-readable mode, which displays the file sizes in a human-readable format.

Mirroring

To mirror two directories, use the following command:

rsync -avh --delete /path/to/source/ /path/to/destination/

This command will synchronize the source directory with the destination directory, deleting any files in the destination directory that do not exist in the source directory. The --delete option is used to delete the files in the destination directory that are not present in the source directory.

Delete Files

To delete files from the destination directory that are not present in the source directory, use the following command:

rsync -avh --delete /path/to/source/ /path/to/destination/

This command will synchronize the source directory with the destination directory, deleting any files in the destination directory that do not exist in the source directory.

Remote Sync

To synchronize files between a local and a remote host, use the following command:

rsync -avh /path/to/local/ user@remote:/path/to/remote/

This command will copy all the files and directories from the local directory to the remote directory. The user is the username of the remote host. The remote is the hostname or IP address of the remote host.

Remote Host

To synchronize files between two remote hosts, use the following command:

rsync -avh user1@remote1:/path/to/source/ user2@remote2:/path/to/destination/

This command will copy all the files and directories from the source directory on remote1 to the destination directory on remote2. The user1 is the username of the first remote host. The remote1 is the hostname or IP address of the first remote host. The user2 is the username of the second remote host. The remote2 is the hostname or IP address of the second remote host.

These are just a few practical examples of using the rsync command in Linux. With its powerful features and options, rsync can help you synchronize and backup your files and directories easily and efficiently.

Conclusion

In conclusion, rsync is a powerful utility that allows users to synchronize files and directories between local and remote servers. It provides a flexible and efficient way to transfer data securely over the network. The command line interface of rsync is easy to use and provides a variety of options to customize the synchronization process.

With rsync, users can transfer large files and directories quickly and efficiently, saving time and bandwidth. The ability to resume interrupted transfers and exclude unnecessary files and directories makes rsync a valuable tool for managing data backups and migrations.

Using rsync in the terminal requires some knowledge of command line syntax, but the benefits of this utility are well worth the effort. The ability to perform remote transfers and synchronize data between servers makes rsync an essential tool for system administrators and developers alike.

Overall, rsync is a reliable and efficient utility that provides a simple and effective way to transfer data between servers. Whether you are managing backups, migrating data, or synchronizing files, rsync is a valuable tool that can save time and effort.

Frequently Asked Questions

How can I use the rsync command to copy an entire directory in Linux?

To copy an entire directory in Linux using rsync, you can use the following command:

rsync -avz /path/to/source/directory/ /path/to/destination/directory/

This command will recursively copy all files and subdirectories from the source directory to the destination directory, preserving file permissions and ownership.

What are the steps to transfer a file from one Linux server to another using rsync?

To transfer a file from one Linux server to another using rsync, you can use the following command:

rsync -avz /path/to/local/file username@remote_host:/path/to/remote/directory/

Replace /path/to/local/file with the path to the file on your local machine, username with the username on the remote server, remote_host with the hostname or IP address of the remote server, and /path/to/remote/directory/ with the path to the directory on the remote server where you want to copy the file.

How do I resolve an ‘rsync command not found’ error on my Linux system?

If you encounter an ‘rsync command not found’ error on your Linux system, it means that rsync is not installed or not in your system’s PATH. To resolve this error, you can install rsync using your system’s package manager.

For example, on Ubuntu or Debian, you can install rsync using the following command:

sudo apt-get install rsync

What is the difference between rsync and scp for file transfers in Linux?

Rsync and scp are both used for file transfers in Linux, but they have different features and use cases. Rsync is designed for efficient synchronization of files between two locations, while scp is designed for secure copying of files between two systems.

Rsync can transfer only the differences between two files, while scp transfers the entire file. Rsync can also synchronize directories and preserve file permissions, ownership, and timestamps, while scp cannot.

How can I synchronize two directories on the same Linux machine using rsync?

To synchronize two directories on the same Linux machine using rsync, you can use the following command:

rsync -avz /path/to/source/directory/ /path/to/destination/directory/

This command will recursively copy all files and subdirectories from the source directory to the destination directory, preserving file permissions and ownership, and deleting any files in the destination directory that do not exist in the source directory.

What is the procedure to configure rsync for file synchronization on a Linux server?

To configure rsync for file synchronization on a Linux server, you can create a configuration file called rsyncd.conf in the /etc directory. This file specifies the directories to be synchronized and the options to be used.

Here is an example rsyncd.conf file:

uid = nobody
gid = nobody
use chroot = yes

[example]

path = /path/to/directory comment = Example directory read only = no

This configuration file specifies that the example directory should be synchronized, and that all users should have read and write access to it. To start the rsync daemon and begin synchronization, you can use the following command:

sudo rsync --daemon

Last Updated on January 15, 2024 by admin

Share this post on social

Copyright 2022 Linuxguidehq.com. All rights reserved. No part of this website may be reproduced, distributed, or transmitted in any form or by any means, including photocopying, recording, or other electronic or mechanical methods, without the prior written permission of the copyright owner.