Cyberithub

26 Popular Rsync Examples for Linux Professionals

Advertisements

In this article, I will take you through 26 Popular Rsync Examples for Linux Professionals. It is known as remote sync and is currently the best tool to sync data between source and destination. It is considered to be fast as it only sync the part of source file which is not available at the destination unless you tell it to copy entire files from source.

26 Popular Rsync Examples for Linux Professionals 1

Rsync Examples 

Also Read: 10 Best Examples of SCP Command in Linux

Advertisements

1. Check Rsync Command version

If you want to check rsync command version, then you need to use --version rsync options as shown below. As you can see from below output, current version is 3.1.3.

[root@localhost ~]# rsync --version
rsync version 3.1.3 protocol version 31
Copyright (C) 1996-2018 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, ACLs, xattrs, iconv, symtimes, prealloc

rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.

2. Copy Symlinks along with files and directories

If you want to transfer all the incremental symlinks as symlinks then you need to use -l option as shown below. This option prevents to recognize any symlink as file or directory and copy symlink as it is at the target location.

Advertisements
[root@localhost ~]# rsync -l -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,073 bytes received 73 bytes 16,292.00 bytes/sec
total size is 7,833 speedup is 0.96

3. Update Files and Directories

If you want to update the destination file with the change in data from source, then you need to use -u option as shown below. This option is very helpful in updating the file at the destination when you know there is a change in file at the source location.

[root@localhost ~]# rsync -u -rv example/ /opt/
sending incremental file list

sent 107 bytes received 12 bytes 238.00 bytes/sec
total size is 7,833 speedup is 65.82

4. Create Backup 

If you want to take Linux backup of files then you need to use -b option as shown below.

Advertisements
[root@localhost ~]# rsync -b -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,073 bytes received 73 bytes 16,292.00 bytes/sec
total size is 7,833 speedup is 0.96

5. Compress File Data During Transfer

If you want to reduce the size of files for transfer then you need to use -z option as shown below. This will compress the file size which will help in faster transfer of files.

[root@localhost ~]# rsync -z -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 3,916 bytes received 73 bytes 7,978.00 bytes/sec
total size is 7,833 speedup is 1.96

6. Copying Files in Archive Mode

If you want to enable archive during file sync then you need to use -a option as shown below. This is the most frequently rsync options which you will see in use for any rsync examples.

Advertisements
[root@localhost ~]# rsync -a -rv example/ /opt/
sending incremental file list
./
file.list
file.txt
file2.txt

sent 8,078 bytes received 76 bytes 16,308.00 bytes/sec
total size is 7,833 speedup is 0.96

7. Save all the File Access Lists during Copy

If you want to preserve files Access Control List during transfer then you need to use -A option as shown below.

[root@localhost ~]# rsync -A -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,080 bytes received 73 bytes 16,306.00 bytes/sec
total size is 7,833 speedup is 0.96

8. Preserve File owner permission during transfer

If you want to preserve file owner access at the destination then you need to use -o option as shown below. This option will preserve the file ownership at the target.

[root@localhost ~]# rsync -o -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,074 bytes received 73 bytes 16,294.00 bytes/sec
total size is 7,833 speedup is 0.96

9. Preserve group owner permission during transfer

If you want to preserve file group access at the destination then you need to use -g option as shown below. This option will preserve the group ownership of files at the destination path.

[root@localhost ~]# rsync -g -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,074 bytes received 73 bytes 16,294.00 bytes/sec
total size is 7,833 speedup is 0.96

10. Preserve Modification Times during Transfer

If you want to preserve file modification time during sync then you need to use -t option as shown below. Sometimes it is important to save the modification timestamp of a file for future use. This option finds many use cases in real world.

[root@localhost ~]# rsync -t -rv example/ /opt/
sending incremental file list
./
file.list
file.txt
file2.txt

sent 8,076 bytes received 76 bytes 16,304.00 bytes/sec
total size is 7,833 speedup is 0.96

11. Transfer Directory without recursing

If you want to preserve directory structure at the destination, then you need to use -d option as shown below.

[root@localhost ~]# rsync -d -v example/ /opt/
building file list ... done
file.list
file.txt
file2.txt

sent 8,067 bytes received 69 bytes 16,272.00 bytes/sec
total size is 7,833 speedup is 0.96

12. Transfer Files Quietly

If you want to transfer files quietly without showing any warnings, then you need to use -q option as shown below.

[root@localhost ~]# rsync -q -r example/ /opt/

13. Preserve hard links during transfer

If you want to preserve hard links at the receiving end then you need to use -H option as shown below.

[root@localhost ~]# rsync -H -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,073 bytes received 73 bytes 16,292.00 bytes/sec
total size is 7,833 speedup is 0.96

14. Perform a dry run of file transfer

If you want to simulate the transfer before doing any actual transfer then you need to use -n option as shown below. This option helps in identifying the output of a command before actually running that command. This option acts like a simulator.

[root@localhost ~]# rsync -n -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 120 bytes received 25 bytes 290.00 bytes/sec
total size is 7,833 speedup is 54.02 (DRY RUN)

15. Preserve all Extended Attributes during Transfer

If you want to preserve extended attributes during transfer then you need to use -X option as shown below.

[root@localhost ~]# rsync -X -rv example/ /opt/
sending incremental file list
./
file.list
file.txt
file2.txt

sent 8,160 bytes received 81 bytes 16,482.00 bytes/sec
total size is 7,833 speedup is 0.95

16. Ignore same timestamp files during transfer

If you want to ignore the same timestamp file at the destination and again want to copy entire file from source then you need to use -I option as shown below.

[root@localhost ~]# rsync -I -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,073 bytes received 73 bytes 16,292.00 bytes/sec
total size is 7,833 speedup is 0.96

17. Preserve Executability permission of file during transfer

If you want to preserve +x executable permission of files during incremental file transfer then you need to use -E option as shown below.

[root@localhost ~]# rsync -E -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,073 bytes received 73 bytes 16,292.00 bytes/sec
total size is 7,833 speedup is 0.96

18. Check Rsync Options using --help

If you want to check all other rsync options, then you need to use --help option to check that.

[root@localhost ~]# rsync --help

-C, --cvs-exclude auto-ignore files the same way CVS does
-f, --filter=RULE add a file-filtering RULE
-F same as --filter='dir-merge /.rsync-filter'
repeated: --filter='- .rsync-filter'
--exclude=PATTERN exclude files matching PATTERN
--exclude-from=FILE read exclude patterns from FILE
--include=PATTERN don't exclude files matching PATTERN
--include-from=FILE read include patterns from FILE
--files-from=FILE read list of source-file names from FILE
-0, --from0 all *-from/filter files are delimited by 0s

19. Check the Progress of Transfer

If you want to check the progress of file transfer then you need to use -P option with rsync command as shown below. You can check the progress of each and every incremental file transfer from source to destination server.

[root@localhost ~]# rsync -P -r example/ /opt/
sending incremental file list
file.list
47 100% 0.00kB/s 0:00:00 (xfr#1, to-chk=2/4)
file.txt
3,893 100% 3.71MB/s 0:00:00 (xfr#2, to-chk=1/4)
file2.txt
3,893 100% 3.71MB/s 0:00:00 (xfr#3, to-chk=0/4)

20. Save directory link from Sender End

If you want to preserve directory link from sender end while transferring the incremental contents then you need to use -k option as shown below.

[root@localhost ~]# rsync -k -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,073 bytes received 73 bytes 16,292.00 bytes/sec
total size is 7,833 speedup is 0.96

21. Save directory link from receiver End

If you want to preserve directory link at the receiver end then you need to use -K option as shown below.

[root@localhost ~]# rsync -K -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,073 bytes received 73 bytes 16,292.00 bytes/sec
total size is 7,833 speedup is 0.96

22. Delete Files at the Receiver End

If you want to delete all the files which is not present at the source but present at the destination then you need to use -d option as shown below.

[root@localhost opt]# rsync --delete -rv example/ /opt/
deleting chefdk/embedded/lib/ruby/gems/2.6.0/gems/azure_mgmt_resources-0.17.8/lib/2018-05-01/generated/azure_mgmt_resources/operations.rb
deleting chefdk/embedded/lib/ruby/gems/2.6.0/gems/azure_mgmt_resources-0.17.8/lib/2018-05-01/generated/azure_mgmt_resources/module_definition.rb
deleting chefdk/embedded/lib/ruby/gems/2.6.0/gems/azure_mgmt_resources-0.17.8/lib/2018-05-01/generated/azure_mgmt_resources/deployments.rb
deleting chefdk/embedded/lib/ruby/gems/2.6.0/gems/azure_mgmt_resources-0.17.8/lib/2018-05-01/generated/azure_mgmt_resources/deployment_operations.rb
deleting chefdk/embedded/lib/ruby/gems/2.6.0/gems/azure_mgmt_resources-0.17.8/lib/2018-05-01/generated/azure_mgmt_resources/
deleting chefdk/embedded/lib/ruby/gems/2.6.0/gems/azure_mgmt_resources-0.17.8/lib/2018-05-01/generated/azure_mgmt_resources.rb
deleting chefdk/embedded/lib/ruby/gems/2.6.0/gems/azure_mgmt_resources-0.17.8/lib/2018-05-01/generated/
deleting chefdk/embedded/lib/ruby/gems/2.6.0/gems/azure_mgmt_resources-0.17.8/lib/2018-05-01/

23. Check the difference between Source and Destination File

If you want to check the difference between source and destination files, then you need to use -i option as shown below.

[root@localhost ~]# rsync -i -rv example/ /opt/
sending incremental file list
>f..T...... file.list
>f..T...... file.txt
>f..T...... file2.txt

sent 8,073 bytes received 73 bytes 16,292.00 bytes/sec
total size is 7,833 speedup is 0.96

24. Transfer Entire Files

If you want to transfer entire files instead of syncing it with the destination then you need to use -W option as shown below. Sometimes instead of sending increment file data you want to copy entire file. In those cases this option will be very helpful.

[root@localhost ~]# rsync -W -rv example/ /opt/
sending incremental file list
file.list
file.txt
file2.txt

sent 8,073 bytes received 73 bytes 16,292.00 bytes/sec
total size is 7,833 speedup is 0.96

25. Transfer File Only of Certain Size

If you only want to transfer files of size less than a maximum size, then you need to define a maximum file size using --max-size option as shown below.

[root@localhost ~]# rsync --max-size=1k -rv example/ /opt/
sending incremental file list
file.list

sent 201 bytes received 35 bytes 472.00 bytes/sec
total size is 7,833 speedup is 33.19

26. Exclude Some file during Transfer

If you want to exclude some file during transfer then you need to use --exclude option as shown below. In this example, we are trying to exclude all the files starting with name file2 to get copied to the destination.

[root@localhost ~]# rsync --exclude file2* -rv example/ /opt/
sending incremental file list
file.list
file.txt

sent 4,115 bytes received 54 bytes 8,338.00 bytes/sec
total size is 3,940 speedup is 0.95

 

 

 

 

Also Read: Rsync Man page

Leave a Comment