Rsync (Remote Sync) is the most commonly used command for copying and synchronising files and directories remotely as well as locally in Linux/Unix systems. With the help of the rsync command, you can copy and synchronise your data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux machines.
This article explains some basic and advanced usage of the rsync command to transfer your files remotely and locally in Linux based machines. You don’t need to be a root user to run the rsync command.
Some advantages and features of the Rsync command
- It efficiently copies and sync files to or from a remote system.
- Supports copying links, devices, owners, groups and permissions.
- It’s faster than SCP (Secure Copy) because rsync uses a remote-update protocol which allows transferring just the differences between two sets of files. The first time, it copies the whole content of a file or a directory from source to destination, but from next time, it copies only the changed blocks and bytes to the destination.
- Rsync consumes less bandwidth as it uses the compression and decompression method while sending and receiving data on both ends.
The basic syntax of rsync command
# rsync options source destination
Some common options used with rsync commands
- -v: verbose
- -r: copies data recursively (but don’t preserve timestamps and permission while transferring data
- -a: archive mode, archive mode allows copying files recursively, and it also preserves symbolic links, file permissions, user & group ownerships and timestamps
- -z: compress file data
- -h: human-readable, output numbers in a human-readable format
Install rsync in your Linux machine
You can install rsync package with the help of the following command.
# yum install rsync (On Red Hat-based systems) # apt-get install rsync (On Debian based systems)
1. Copy/Sync Files and Directory Locally
Copy/Sync a File on a Local Computer
The following command will sync a single file on a local machine from one location to another location. Here in this example, a file name backup.tar needs to be copied or synced to /tmp/backups/ folder.
[root@tecmint]# rsync -zvh backup.tar /tmp/backups/ created directory /tmp/backups backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10
In the above example, you can see that if the destination is not already existed, rsync will create a directory automatically for a destination.
Copy/Sync a Directory on Local Computer
The following command will transfer or sync all the files from one directory to a different directory in the same machine. Here in this example, /root/rpmpkgs contains some rpm package files, and you want that directory to be copied inside /tmp/backups/ folder.
[root@tecmint]# rsync -avzh /root/rpmpkgs /tmp/backups/ sending incremental file list rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 4.99M bytes received 92 bytes 3.33M bytes/sec total size is 4.99M speedup is 1.00
2. Copy/Sync Files and Directory to or From a Server
Copy a Directory from Local Server to a Remote Server
This command will sync a directory from a local machine to a remote machine. For example, there is a folder in your local computer, “rpmpkgs”, that contains some RPM packages, and you want that local directory’s content sends to a remote server, you can use the following command.
[root@tecmint]$ rsync -avz rpmpkgs/ firstname.lastname@example.org:/home/ email@example.com's password: sending incremental file list ./ httpd-2.2.3-82.el5.centos.i386.rpm mod_ssl-2.2.3-82.el5.centos.i386.rpm nagios-3.5.0.tar.gz nagios-plugins-1.4.16.tar.gz sent 4993369 bytes received 91 bytes 399476.80 bytes/sec total size is 4991313 speedup is 1.00
Copy/Sync a Remote Directory to a Local Machine
This command will help you sync a remote directory to a local directory. Here in this example, a directory /home/himani/rpmpkgs, which is on a remote server, is being copied in your local computer in /tmp/myrpms.
[root@tecmint]# rsync -avzh firstname.lastname@example.org:/home/tarunika/rpmpkgs /tmp/myrpms email@example.com's password: receiving incremental file list created directory /tmp/myrpms rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm rpmpkgs/nagios-3.5.0.tar.gz rpmpkgs/nagios-plugins-1.4.16.tar.gz sent 91 bytes received 4.99M bytes 322.16K bytes/sec total size is 4.99M speedup is 1.00
3. Rsync Over SSH
With rsync, we can use SSH (Secure Shell) for data transfer, using SSH protocol while transferring our data. You can be ensured that your data is being transferred in a secured connection with encryption so that nobody can read your data while it is being transferred over the wire on the internet.
Also, when we use rsync, we need to provide the user/root password to accomplish that particular task, so using the SSH option will send your logins in an encrypted manner so that your password will be safe.
Copy a File from a Remote Server to a Local Server with SSH
To specify a protocol with rsync, you need to give the “-e” option with the protocol name you want to use. Here in this example, We will be using “ssh” with the “-e” option and perform data transfer.
[root@tecmint]# rsync -avzhe ssh firstname.lastname@example.org:/root/install.log /tmp/ email@example.com's password: receiving incremental file list install.log sent 30 bytes received 8.12K bytes 1.48K bytes/sec total size is 30.74K speedup is 3.77
Copy a File from a Local Server to a Remote Server with SSH
[root@tecmint]# rsync -avzhe ssh backup.tar firstname.lastname@example.org:/backups/ email@example.com's password: sending incremental file list backup.tar sent 14.71M bytes received 31 bytes 1.28M bytes/sec total size is 16.18M speedup is 1.10
4. Show Progress While Transferring Data with rsync
To show the progress while transferring the data from one machine to a different machine, we can use the ‘–progress’ option. It displays the files and the time remaining to complete the transfer.
[root@tecmint]# rsync -avzhe ssh --progress /home/rpmpkgs firstname.lastname@example.org:/root/rpmpkgs email@example.com's password: sending incremental file list created directory /root/rpmpkgs rpmpkgs/ rpmpkgs/httpd-2.2.3-82.el5.centos.i386.rpm 1.02M 100% 2.72MB/s 0:00:00 (xfer#1, to-check=3/5) rpmpkgs/mod_ssl-2.2.3-82.el5.centos.i386.rpm 99.04K 100% 241.19kB/s 0:00:00 (xfer#2, to-check=2/5) rpmpkgs/nagios-3.5.0.tar.gz 1.79M 100% 1.56MB/s 0:00:01 (xfer#3, to-check=1/5) rpmpkgs/nagios-plugins-1.4.16.tar.gz 2.09M 100% 1.47MB/s 0:00:01 (xfer#4, to-check=0/5) sent 4.99M bytes received 92 bytes 475.56K bytes/sec total size is 4.99M speedup is 1.00
5. Use of –include and –exclude Options
These two options allow us to include and exclude files by specifying parameters with these option helps us to specify those files or directories which you want to include in your sync and exclude files and folders with you don’t want to be transferred.
Here in this example, the sync command will include those files and directory only, which starts with ‘R’ and exclude all other files and directory.
[root@tecmint]# rsync -avze ssh --include 'R*' --exclude '*' firstname.lastname@example.org:/var/lib/rpm/ /root/rpm email@example.com's password: receiving incremental file list created directory /root/rpm ./ Requirename Requireversion sent 67 bytes received 167289 bytes 7438.04 bytes/sec total size is 434176 speedup is 2.59
6. Use of –delete Option
If a file or directory does not exist at the source but already exists at the destination, you might want to delete that existing file/directory at the target while syncing.
We can use the ‘–delete‘ option to delete files that are not there in the source directory.
Source and target are in sync. We are now creating a new file test.txt at the target.
[root@tecmint]# touch test.txt [root@tecmint]# rsync -avz --delete firstname.lastname@example.org:/var/lib/rpm/ . Password: receiving file list ... done deleting test.txt ./ sent 26 bytes received 390 bytes 48.94 bytes/sec total size is 45305958 speedup is 108908.55
Target has the new file called test.txt; when synchronising with the source with the ‘–delete‘ option, it removed the file test.txt.
7. Set the Max Size of Files to be Transferred
You can specify the Max file size to be transferred or sync. You can do it with the “–max-size” option. Here in this example, the Max file size is 200k, so this command will transfer only those files which are equal to or smaller than 200k.
[root@tecmint]# rsync -avzhe ssh --max-size='200k' /var/lib/rpm/ email@example.com:/root/tmprpm firstname.lastname@example.org's password: sending incremental file list created directory /root/tmprpm ./ Conflictname Group Installtid Name Provideversion Pubkeys Requireversion Sha1header Sigmd5 Triggername __db.001 sent 189.79K bytes received 224 bytes 13.10K bytes/sec total size is 38.08M speedup is 200.43
8. Automatically Delete source Files after successful Transfer
Now, suppose you have the main web server and a data backup server, you created a daily backup and synced it with your backup server, now you don’t want to keep that local copy of backup in your web server.
So, will you wait for the transfer to complete and then delete that local backup file manually? Of Course, NO. This automatic deletion can be done using the ‘–remove-source-files‘ option.
[root@tecmint]# rsync --remove-source-files -zvh backup.tar /tmp/backups/ backup.tar sent 14.71M bytes received 31 bytes 4.20M bytes/sec total size is 16.18M speedup is 1.10 [root@tecmint]# ll backup.tar ls: backup.tar: No such file or directory
9. Do a Dry Run with rsync
Suppose you are a newbie and using rsync and don’t know what exactly your command is going to do. Rsync could really mess up the things in your destination folder, and then doing an undo can be a tedious job.
Use of this option will not make any changes. Only do a dry run of the command and shows the output of the command; if the output shows exactly the same you want to do, then you can remove the ‘–dry-run‘ option from your command and run on the terminal.
root@tecmint]# rsync --dry-run --remove-source-files -zvh backup.tar /tmp/backups/ backup.tar sent 35 bytes received 15 bytes 100.00 bytes/sec total size is 16.18M speedup is 323584.00 (DRY RUN)
10. Set Bandwidth Limit and Transfer File
You can set the bandwidth limit while transferring data from one machine to another machine with the help of the ‘–bwlimit‘ option. This option helps us to limit I/O bandwidth.
[root@tecmint]# rsync --bwlimit=100 -avzhe ssh /var/lib/rpm/ email@example.com:/root/tmprpm/ firstname.lastname@example.org's password: sending incremental file list sent 324 bytes received 12 bytes 61.09 bytes/sec total size is 38.08M speedup is 113347.05
Also, by default, rsync syncs changed blocks and bytes only. If you want explicitly want to sync the whole file, then you use the ‘-W‘ option with it.
[root@tecmint]# rsync -zvhW backup.tar /tmp/backups/backup.tar backup.tar sent 14.71M bytes received 31 bytes 3.27M bytes/sec total size is 16.18M speedup is 1.10