Rsync, short for "remote sync," is a tool designed for locally and remotely synchronizing files. It efficiently transfers data by detecting and copying only the modified portions of files rather than the entire file, reducing data transfer and improving speed.
The rsync command allows you to copy and sync data both locally and remotely across different directories, storage devices, and networks. It is commonly used for data backups and mirroring between two Linux systems. Rsync is included by default in most modern Linux distributions, so no additional installation is needed.
In this guide, we will demonstrate essential and advanced ways of how to use the rsync command for transferring files both locally and remotely on Linux systems. You can run rsync without requiring root privileges.
Rsync Syntax
The basic rsync syntax for the is given below:
rsync [OPTIONS] SOURCE DESTINATION
Here, you specify options along with the source and destination paths for file transfer.
How to Use Rsync Command? (Rsync Examples)
Below, we have listed the common use cases of how to use rsync command:
Copy Files Locally (Rsync Files Locally)
Using rsync command, you can transfer or synchronize a file within the same system. This will copy a specific file from one directory to another.
For example, to copy the file ‘example.text’ to the /testfilesDirectory/ folder:
$ rsync -zvh example.text /testfilesDirectory/
If the destination folder doesn’t exist, rsync will automatically create it.
Rsync Directories (Rsync Folder to Folder)
The following command copies or syncs all files from one directory to another on the same system. For example, copy the ‘/testfilesDirectory/’ directory containing a text file to the ‘test’ directory.
$ rsync -avzh /testfilesDirectory/ test
Rsync a Folder from the Local to the Remote Server
To transfer a directory from a local system to a remote server, use the following command. For example, to sync the /testfilesDirectory/ folder containing text files from your local machine to a remote server use this command:
# rsync -avzh /testfilesDirectory/ root@remote-ip-address:/root/
Rsync Directories from the Local to the Remote Server
To sync a directory from a remote server to a local machine, use this command. For example, copy the ‘/testfilesDirectory/’ directory from the remote server to “/test/” on your local system.
$ rsync -avzh root@remote-server-ip:/testfilesDirectory/ /test
Rsync Over SSH
Using rsync, you can securely transfer data over SSH, ensuring encryption and protecting sensitive information during transmission. Using SSH also secures login credentials.
To use rsync with SSH, include the -e option:
$ rsync [OPTIONS] -e ssh /source-path/ user@remote:/destination-path
Copy a File from the Remote to the Local Server Using SSH (Rsync to remote server)
To transfer a file from a remote server to a local machine using SSH, specify the -e option with ssh.
$ rsync -avzhe ssh root@remote-ip:/root/testfile.text /tmp
Display Progress During Rsync Folders and Files Transfer
To monitor file transfers in real-time, use the --progress option. This shows the files being copied and the estimated time remaining.
$ rsync -avzhe ssh --progress /testfilesDirectory root@remote-ip:/testfilesDirectory
Include and Exclude Files in Rsync
Use the --include and --exclude options to filter files during transfer. This allows you to specify which files or directories to sync while skipping others.
For example, to include only files and directories starting with "R" and exclude everything else:
$ rsync -avze ssh --include 'R*' --exclude '*' root@remote-ip:/var/lib/rpm
Using the --delete Option in Rsync
If a file or directory exists in the destination but not in the source, the --delete option ensures it gets removed during synchronization.
For example, to sync directories and remove any extra files from the destination:
$ rsync -avz --delete root@destination-ip:/var/lib/rpm/ /root
Limit File Transfer Size in Rsync
To restrict the maximum file size for transfer, use the --max-size option. This ensures only files of the specified size or smaller are synced.
For example, to transfer files up to 200KB in size:
$ rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ root@destination-ip:/root/tmp
Automatically Remove Source Files After Transfer
If you want to transfer files and delete them from the source afterwards, use the --remove-source-files option. This is useful for backups, ensuring files are moved without keeping local copies.
$ rsync --remove-source-files -zvh backup.tar.gz root@destination-remote-ip:/tmp/backup
Perform a Dry Run with Rsync
If you're new to rsync or unsure about the outcome of a command, using rsync command, you can use the --dry-run option. This allows you to preview the changes without making any modifications. Once you're satisfied with the output, you can remove --dry-run and execute the actual command.
$ rsync --dry-run --remove-source-files -zvh backup.tar.gz root@remote-ip:/tmp
Rsync Set Transfer File and Bandwidth Limit
You can control the bandwidth usage during file transfers using the --bwlimit option in rsync. This allows you to set a maximum transfer rate, preventing excessive network load.
$ rsync --bwlimit=100 -avzhe ssh /var/lib/rpm/ root@destination-ip:/root/tmp
By default, rsync transfers only a file's modified blocks and bytes. However, you can use the W option if you need to sync the entire file regardless of changes.
$ rsync -zvhW backup.tar /tmp/backups/backup.tar
Advantages of the Rsync Command
Using Rsync, you can take advantage of the following benefits:
- Efficient Transfers: Uses a delta algorithm to copy only changed portions of files, reducing data transfer.
- Remote Synchronization: Supports local and remote file syncing over SSH.
- Incremental Backups: Transfers only new or modified files, making backups more efficient.
- Preserves File Attributes: Maintains permissions, ownership, timestamps, and symbolic links.
- Bandwidth Control: Allows limiting bandwidth usage and uses compression for optimized transfers.
- Speed: Often faster than SCP, especially for large directories or previously transferred files.
Conclusion
This wraps up our discussion on how to use rsync command and its features. To explore more advanced options and functionalities, refer to the official manual pages (man rsync) for detailed documentation.
Get a high-performance VPS server with Host-World! Choose from top VPS hosting plans that offer speed, security, and reliability. Powered by cutting-edge technology, our hosting solutions ensure a seamless and powerful online experience. Get started today!