Home » Linux Command » 15 practical examples of rsync command

15 practical examples of rsync command

Update on:
Sep 4, 2021

The rsync command in Linux stands for remote sync.

Generally, we use rsync to perform the backup process in Linux/UNIX.

Linux utility rsync is very powerful to synchronize files and directories from one location to another.

Now, let’s discuss on 15 practical uses of rsync command:

Example 1 –

Synchronize two directories in the same system

It is the primary use of rsync command, and to perform this operation just using rsync command with “-zvr” option like below:

$ rsync –zvr /home/linuxconcept/newweb/ /home/510848.cloudwaysapps.com/kqawmysncg/public_html/ 
building file list … done 
home.html 
index.html 
. 
sent 23452 bytes 	received 1023 bytes 	563423.00 bytes/sec 
total size is 34233 	speedup is 1.32
$

In the above example we use three options:

  • -z  we use to enable compression
  • -v  is for run command in verbose mode
  • -r  is used to recursive execution of the command

Now we can check the timestamp for both the files copied and source; you can see both file’s timestamp is different is not preserve timestamps during sync.

$ ls –l /home/linuxconcept/newweb/index.html /home/510848.cloudwaysapps.com/kqawmysncg/public_html/index.html 
– r – – r – – r – – 1 bin bin 949 Sep 18 2018 /home/linuxconcept/newweb/index.html 
– r – – r – – r – – 1 bin bin 949 Apr 13 2019 /home/510848.cloudwaysapps.com/kqawmysncg/public_html/index.html

Example 2 –

Using rsync –a to preserve timestamp

The rsync command use with an option “-a” to run command in archive mode. This option does all the following

  • Recursive mode operation
  • Preserves timestamp during sync
  • Preserves Permissions during sync
  • Preserver Owner and Group settings
  • Preserves symbolic links, if have

Now, we execute the same previous command with “-a” option, as shown below:

$ rsync –zva /home/linuxconcept/newweb/ /home/510848.cloudwaysapps.com/kqawmysncg/public_html/ 
building file list … done 
home.html 
index.html 
. 
sent 23452 bytes 	received 1023 bytes 	563423.00 bytes/sec 
total size is 34233 	speedup is 1.32 
$

Now we can see the timestamp for both the files

– r – – r – – r – – 1 bin bin 949 Sep 18 2018 /home/linuxconcept/newweb/index.html 
– r – – r – – r – – 1 bin bin 949 Sep 18 2018 /home/510848.cloudwaysapps.com/kqawmysncg/public_html/index.html

Example 3 –

rsync use to synchronize only one file

We can use rsync command to copy one file also, as shown below:

$ rsync –v /etc/ssl/linuxconcept/pubkeys /home/linuxconcept/pubkeys 
pubkeys 
sent 56 bytes 	received 24321 bytes 	3456.14 bytes/sec 
total size is 12288 	speedup is 0.76

Example 4 –

rsync also use to synchronize files from local system to remote system

The rsync command allows us to synchronize files/directories between local and remote system.

$ rsync -avz /root/temp/ linuxconcept@192.168.2.10:/home/linuxconcept/temp/ 
Password: 
building file list … done 
./ 
satish/ 
satish/abc.txt 
satish/test.txt 
sent 15815561 bytes 	received 452 bytes 	2425411.23 bytes/sec 
total size is 45024658 	speedup is 2.17

We are doing file synchronization with remote server/system, we need to specify the remote system IP and username to login on the remote system and specify the location on the remote system to sync file in the particular location.
As you can see in the above example, it asks for a password to login on the remote system.

Sometimes we don’t want to enter a password and execute the same command from a script when we want to perform the backup operation and schedule it for automation.

To perform rsync without password, you need to set up ssh passwordless login.

Example 5 –

Use rsync command to synchronize files from remote system to local system.

This is similar to previous one, here we specify the remote path into source and local system path into destination.

$ rsync -avz linuxconcept@192.168.2.10:/home/linuxconcept/satish /root/temp 
Password: 
receiving file list … done 
satish/ 
satish/abc.txt 
. 
sent 426 bytes 	received 1565230 bytes 	2437635.54 bytes/sec 
total size is 45345658 	speedup is 2.37

Example 6 –

Specify remote shell into rsync

The rsync utility allows specifying the remote shell into a command which you want to use. Like the below example we use ssh to enable the secure connection with a remote system.

You can use “-e” option to specify the shell in the command, as shown below:

$ rsync -avz -e ssh linuxconcept@192.168.2.10:/home/linuxconcept/satish 
/root/temp 
Password: receiving file list … done 
satish/ 
satish/Basenames 
sent 416 bytes 	received 15345230 bytes 	2145605.54 bytes/sec 
total size is 45672858 	speedup is 2.37

Example 7 –

Use rsync without overwriting in destination

In general, if the files are modified in the destination location, we don’t want to overwrite those files.

To prevent files from overwriting you can use the “-u” option. In the below example you can see the file abc.txt is already modified, So it will not be overwritten while using rsync –u command.

$ ls -l /root/temp/abc.txt 
total 39088 
-rwxr-xr-x 1 root root 4096 Oct 2 11:35 abc.txt 
$ rsync -avzu linuxconcept@192.168.2.10:/home/linuxconcept/satish /root/temp 
Password: 
receiving file list … done 
satish/ 
sent 112 bytes 	received 535 bytes 	104.00 bytes/sec 
total size is 45603558 	speedup is 34258.31 
$ ls -lrt 
total 39088 
-rwxr-xr-x 1 root root 4096 Oct 2 11:35 abc.txt

Example 8 –

During rsync show progress

To show the progress on the terminal while transferring files from one system to another system using “- -progress” option. It will display the file name which is currently transfer and remaining time to transfer.

$ rsync -avzhe ssh –progress /home/linuxconcept linuxconcept@192.168.2.10:/root/rpmpkgs 
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.3.0.tar.gz 1.72M 100% 1.06MB/s 0:00:01 (xfer#3, to-check=1/5) 
rpmpkgs/nagios-plugins-1.3.16.tar.gz 2.09M 100% 1.27MB/s 0:00:01 (xfer#4, to-check=0/5) 
sent 4.79M bytes 	received 94 bytes 	465.56K bytes/sec 
total size is 4.99M 	speedup is 1.00

Example 9 –

Delete files in the destination

Sometimes we use rsync command to create a replica of any application, and in this case, we want to make destination directory exact similar to the source directory.

In this case, if some of the files are in the destination directory but not in the source directory, we wanted to delete them automatically.

To perform this operation you can use “- – delete” option like shown below:

$ touch testing.txt 
$ rsync –avz – -delete linuxconcept@192.168.2.10:/home/linuxconcept/ . 
Password: 
receiving file list . . . done 
deleting testing.txt 
./ 
sent 24 bytes 	received 340 bytes 	48.64 bytes/sec 
total size is 45678234 	speedup is 10872.34

Example 10 –

Specify max file size with rsync command

You can also specify the maximum file size to be transferred while using rsync utility. To determine the maximum file size you can use “- -max-size” option in rsync command.

$ rsync –avzhe ssh – -max-size=’400k’ /home/linuxconcept/satish/ linuxconcept@192.168.2.10:/home/linuxconcept/tmp 
Password: 
sending incremental file list 
create directory /home/linuxconcept/tmp 
./ 
abc.txt 
testing.txt 
index.html 
home.html 
sent 168.45K bytes 	received 234 bytes 	14.20K bytes/sec 
total size is 34.06M 	speedup is 134.43

Example 11 –

Delete source files after successful execution of rsync

Suppose you are taking backup your production data every day on your production server and transferring on the local system using rsync and don’t want to keep after that copying into the local system.

In this case, we can delete the source file automatically after copying data to the local system. To perform this operation, you can use “- -remove-source-files’ option.

$rsync –zvh – -remove-sourc-files web-backup.tar /home/linuxconcept/backups/ 
webbackup.tar 
sent 13.24M bytes 	received 36 bytes 	4.02M bytes/sec 
total size is 24.36M 	speedup is 1.34 
$ ll webbackup.tar 
ls: webbackup.tar: No such file or directory

Example 12 –

Files include and exclude in rsync

The rsync command gives the option to include and exclude files or directories while doing synchronization.

In the below example, we include only files or directories starting with ‘S’ using “- -include” option and exclude all other files using “- -exclude” option.

$ rsync –avz – -include ‘S*’ – -exclude ‘*’ linuxconcept@192.168.2.10:/home/linuxconcept/ 
Password: 
receving file list . . . done 
./ 
satish.txt 
satish01.txt 
SSH-config 
sent 234 bytes 	received 10625780 bytes 	23456718.73 bytes/sec 
total size is 42356718 	speedup is 2.45

Example 13 –

Set bandwidth while synchronizing

You can use “- -bwlimit” option to set and utilize network bandwidth during file transfer using rsync command.

This option is used to limit the input and output network bandwidth over the network.

$ rsync –avz – -bwlimit=100 /home/satish/tmp/ linuxconcept@192.168.2.10:/home/linuxconcept/
Password: 
sending incremental file list 
sent 314 bytes 	received 14 bytes 	43.35 bytes/sec 
total size is 24.03M 	speedup is 523456.05

Example 14 –

Synchronize Whole files using

The rsync we use for its best feature is that it synchronizes only the changed block to the destination, instead of sending the whole file.

If you don’t have a network bandwidth issue and having enough CPU to do the process for a long time, you can use the “-W” option for transfer whole files.

$rsync –avzW  linuxconcept@192.168.2.10:/home/linuxconcept/  /home/satish/linuxconcept
Password:
receiving file list . . . done
./
abc.txt
home.html
index.html
sent 403 bytes       received 1452670 bytes     8.62 bytes/sec
total size is 42567812       speedup is 2.38

Example 15 –

Dry Run option for rsync

After running of rsync job, doing an undo can be a tedious job. If anything gets mess up with rsync, it is difficult to move on the previous state.

So, there is an option to run rsync command with “- -dry-run” option where it executes to show what will happen if this command will execute.

Use of this option will not make any changes on source or destination and show the output similar to the actual execution of the command. You can test the command using the “- -dry-run” option and remove this option and execute the command when you satisfy with output.

$ rsync –avz – -dry-run – -remove-source-files webbackup.tar /home/linuxconcept/backup 
webbackup.tar 
sent 32 bytes 	received 16 bytes 	101.00 bytes/sec 
total size is 12.12M 	speed is 324567.00 (DRY RUN)

Related Posts

The 30 most useful Linux commands

This article will introduce some practical and common Linux or Unix command , which is the Linux system administrators normally use the command . This article is not a complete list, but a brief list of commands that can be useful...

15 Practical examples of tcpdump command

15 Practical examples of tcpdump command

The tcpdump is the most essential and powerful command-line network packet sniffing tool. We use it to analyze network packets, capture or filter TCP/IP packets that transferred and received over the network on a specific network interface. It is available in almost...

15 Useful “df” Commands to Check Disk Space in Linux

15 Useful “df” Commands to Check Disk Space in Linux

There are several Linux tools available on the internet to check disk space utilization; we can use any of them. But, Linux has a best built-in command-line utility called “df”. The “df” is the short form of “disk filesystem”, we can get detailed information on used...

15 Practical examples of echo command

15 Practical examples of echo command

If you are writing a bash script, you know echo command very well. It is use to display text lines on an output screen or a file. The echo command is the most common command used by all Linux users, and it is built-in Linux command. echo command syntax: echo...

Follow Us

Our Communities

More on Linux Command

The Ultimate Managed Hosting Platform
Load WordPress Sites in as fast as 37ms!

0 Comments

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

20 − 3 =

Shares