The rsync command in Linux stands for remote sync. Here we will show you various rsync example to help you to find your own way to use rsync command.
Rsync is a very useful tool that can be used to synchronize files and folders between two computers, or even across the Internet. It’s also one of those tools that are so easy to use but have such powerful features that it’s hard to believe they were designed by just a few people.
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.
Suggested Reading
Essential features of the rsync command:
Speed – As rsync command is used to sync the file to another location, So the first time, it replicates all files and data between the source and destination directories. Next time, it checks modification and changes in both location and transfers only the changed blocks or bytes of information into the destination directories, which makes the file or directories transfer fast.
Security – this command support data encryption during data transfer over SSH protocol.
Less Bandwidth– During synchronization of data between source and destination it using compression and decompression of data blocks by blocks at source and destination respectively. So, due to the compression of data rsync use less bandwidth compared to any other file transfer protocol.
Privileges – It is a very simple application; there is no requirement for any special rights to install and execute rsync command.
For rsync command syntax and options check here.
Now, let’s discuss on 15 practical uses of rsync command:
rsync Example 1 – Synchronize two directories in the same system
It is the primary use of rsync command, and to perform this operation just using 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
rsync Example 2 – Using rsync –a to preserve timestamp
This 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
rsync Example 3 – rsync use to synchronize only one file
We can use this 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 it 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
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 command, it is difficult to move on the previous state.
So, there is an option to run 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)
0 Comments