rsync Command in Linux with Examples
rsync Command
rsync command, which stands for " remote synchronization" . It is important tool for sharing files and directories from one location to another location. It is also used to perform the backup operation in UNIX / Linux.
Features of rsync
1. No special privileges are required to install and execute rsync.
2. Sync allows encryption of data using SSH protocol during transfer.
3. Support for copying links, devices, owners, groups, and permissions.
4. rsync consumes less bandwidth as it uses compression and decompression method while sending and receiving data both ends.
5. If you want to take your regular backup from local to remote system. The backup file has 25 database contents if the file broken means SCP starts the file transfer from the beginning. This issue will not occur in rsync here if file broken means it start from where it stopped.
Options used in rsync
-v &rarr verbose
-r &rarr copies data recursively
-h &rarr human-readable
-z &rarr compress file data
-a &rarr archive mode
-e &rarr specify the remote shell to use
-p &rarr show progress during transfer
Syntax
rsync < options> < source > < destination>
To sync files and directories at locally
The rsync command shows the file my.txt. It transfers to a local machine from one location to another location using rsync. It is also used to transfer the directory from one location to another location locally.
[root@linuxhelp Desktop]# rsync -vh hello.txt /home/user2/Desktop
sending incremental file list
hello.txt
sent 226 bytes received 31 bytes 514.00 bytes/sec
total size is 134 speedup is 0.52
To sync the file and directory from local server to remote server
The below command shows sync directory dir from local to remote server. The -e option specify the remote shell to use. i.e. specify the which protocol name you want to use.
[root@linuxhelp Desktop]# rsync -avzhe ‘ ssh &ndash p24865’ linuxman user1@192.168.7.13:/home/user1/Documents
sending incremental file list
created directory /home /user2/Documents
linuxman/
linuxman/AWK command.odt
linuxman/Grep command.odt
.
.
linuxman/zip commands.odt
sent 464.42 bytes received 263 bytes 132.77k bytes/sec
total size is 491.82k speedup is 1.06
To sync the file and directory from remote server to local server
The below rsync command shows sync file from remote user to local user sandy at Picture directory.
[root@linuxhelp Desktop]# rsync -avhe ' ssh -p24865' notes.txt user1@192.168.7.251:/home/user1/Desktop/
user1@192.168.7.13' s password:
sending incremental file list
notes.txt
sent 148 bytes received 31 bytes 5.87 bytes/sec
total size is 2.34K speedup is 13.08
To display the Progress while transferring the Data
rsync command shows progress of transferring the data from one machine to a different machine with using option --progress. Ex. you are taking regular backup, you might want to know how many files are copied, at what rate it is copied the file, etc.., for this need it is much needed tool.
[root@linuxhelp Desktop]# rsync -avzhe ' ssh -p24865' --progress linuxfilenotes user3@192.168.7.13:/home/user3/Desktop
user3@192.168.7.13' s password:
sending incremental file list
linuxfilenotes/
linuxfilenotes/df
linuxfilenotes/df/df command.odt
43.53 100% 5.10kB/s 0:00:00 (xfer#1, to-check=6/8)
.
.
linuxfilenotes/rsync/RSYNC.conf
sent 1.57K bytes received 149 bytes 381.11 bytes/sec
total size is 1.75K speedup is 1.02
To set maximum size of files to transfer
Using option --max-size you can specify maximum files to be transfer, in our command we assigned maximum file transfer size is 50k, it only allow or transfer the file size between less than or equal 50k.
[root@linuxhelp Desktop]# rsync -avzhe ' ssh -p24865' --max-size=' 50K'
linuxfilenotes user3@192.168.7.13:/home/user3/Downloads
sending incremental file list
scp.odt
sent 43858 bytes received 31 bytes 87778.00 bytes/sec
total size is 45393 speedup is 1.03
To set minimize size of files to transfer
Using option --max-size you can specify maximum files to be transfer, in our command we assigned maximum file transfer size is 50k, it only allow or transfer the file size between less than or equal 50k.
[root@linuxhelp Desktop]# rsync -avzhe ' ssh -p24865' --min-size=' 5K' linuxfilenotes
user3@192.168.7.13:/home/user3/Download
sending incremental file list
mydir/
mydir/text3
mydir/text4
mydir/text5
sent 1.73 bytes received 31 bytes 401..00 bytes/sec
total size is 45393 speedup is 1.03
To limiting the bandwidth
You can set the bandwidth limit while transferring data from one machine to another machine with the the help of --bwlimit option.
[root@linuxhelp Desktop]# rsync --bwlimit=100 -avzhe ' ssh -p24865' linuxfilenotes/ user3@192.168.7.13:/home/user3/Pictures/
user3@192.168.7.13' s password:
sending incremental file list
./
df command.odt
ducommands.odt
linux file system.odt
scp.odt
sent 178.97K bytes received 91 bytes 39.79K bytes/sec
total size is 189.54K speedup is 1.06
rsync command using exclude option
Using option --exclude you can terminate the particular files while transferring from source to destination. Option --exclude terminate all files based on name starting d in destination.
[root@linuxhelp Desktop]# rsync -avzhe ' ssh -p24865' --exclude ' f*' /home/user3/Desktop/linuxfilenotes/
user192.168.7.13:/home/user3/Pictures/
user3@192.168.7.13' s password:
sending incremental file list
./
linux file system.odt
sent 104.68K bytes received 53 bytes 29.92K bytes/sec
total size is 112.00K speedup is 1.07
rsync command using include with exclude option
Using option --include it will allow only that particular files or directories to the destination. Here both include and exclude used, these include will transfer file name started with t and exclude skip the remaining all file.
[root@linuxhelp Desktop]# rsync -avzhe ‘ ssh &ndash p24865’ --include ‘ t*’ --exclude ‘ *’ mydir/ user3@192.168.7.13:/home/user3/Desktop
user32192.168.7.13’ s password:
sending incremental file list
./
text1
text1
text1
text1
To delete source file automatically after successfully transferred
The following command will very useful while taking regular backup. Suppose you have a 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. This automatic deletion can be done using ‘ --remove-source-files’ option.
[root@linuxhelp Desktop]# rsync --remove-source-files -zvh my.tar /home/user2/Documents/my.tar
sent 742.31M bytes received 31 bytes 1.86M bytes/sec
total size is 890.88k. speedup is 1.07
After run this command goes to Desktop location now the check the backup file, now the file will be automatically deleted and it stored in Document location.
rsync command using --delete option
The below command will perform if a file is not present at the source, but present at the target, you might want to delete the file at the target during rsync, in such a case you can use --delete option.
[root@linuxhelp Desktop]# rsync -avzhe ‘ ssh &ndash p24865’ --delete dir1 user3@192.68.7.13:/home/user3/Desktop
user3@192.168.7.13 password:
sending incremental file list
dir1/
deleting dir/f9
deleting dir/f9
deleting dir/f9
deleting dir/f9
dir1/a
dir2/b
dir3/c
sent 195 bytes received 73 byte 178.67 bytes/sec
total size is 0 speedup is 0.00
Do Not Overwrite the Modified Files at the Destination
The option using u will not overwrite the modified files at the destination, (i.e) you want share the file with name my.mp3 to your destination and you destination location also have the same file name my.txt in that location, now the my.txt will not overwrite in that location.
[root@linuxhelp Desktop]# rsync -avzue ‘ ssh &ndash p24865’ aa.txt user3@192.168.7.13:/home/user3/Desktop
user3@192.168.7.13' s password:
sending incremental file list
./
sent 57 bytes received 15 bytes 16.00 bytes/sec
total size is 79 speedup is 1.10
After run this command goes and check the destination location while the file is overwrites or not. Now try the same command without using u option and you know the difference between the both options.
Comments ( 0 )
No comments available