Monday, April 2, 2018

Rsync (Remote Sync): 10 Practical Examples of Rsync Command in Linux

rsync is a file transfer program capable of efficient remote update via a fast differencing algorithm.Usage: rsync [OPTION]... SRC [SRC]... DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
  or   rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST
  or   rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST
  or   rsync [OPTION]... [USER@]HOST:SRC [DEST]
  or   rsync [OPTION]... [USER@]HOST::SRC [DEST]
  or   rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]
The ':' usages connect via remote shell, while '::' & 'rsync://' usages connect
to an rsync daemon, and require SRC or DEST to start with a module name.


Options
 -v, --verbose               increase verbosity
 -q, --quiet                 suppress non-error messages
     --no-motd               suppress daemon-mode MOTD (see manpage caveat)
 -c, --checksum              skip based on checksum, not mod-time & size
 -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
     --no-OPTION             turn off an implied OPTION (e.g. --no-D)
 -r, --recursive             recurse into directories
 -R, --relative              use relative path names
     --no-implied-dirs       don't send implied dirs with --relative
 -b, --backup                make backups (see --suffix & --backup-dir)
     --backup-dir=DIR        make backups into hierarchy based in DIR
     --suffix=SUFFIX         set backup suffix (default ~ w/o --backup-dir)
 -u, --update                skip files that are newer on the receiver
     --inplace               update destination files in-place (SEE MAN PAGE)
     --append                append data onto shorter files
     --append-verify         like --append, but with old data in file checksum
 -d, --dirs                  transfer directories without recursing
 -l, --links                 copy symlinks as symlinks
 -L, --copy-links            transform symlink into referent file/dir
     --copy-unsafe-links     only "unsafe" symlinks are transformed
     --safe-links            ignore symlinks that point outside the source tree
 -k, --copy-dirlinks         transform symlink to a dir into referent dir
 -K, --keep-dirlinks         treat symlinked dir on receiver as dir
 -H, --hard-links            preserve hard links
 -p, --perms                 preserve permissions
 -E, --executability         preserve the file's executability
     --chmod=CHMOD           affect file and/or directory permissions
 -A, --acls                  preserve ACLs (implies --perms)
 -X, --xattrs                preserve extended attributes
 -o, --owner                 preserve owner (super-user only)
 -g, --group                 preserve group
     --devices               preserve device files (super-user only)
     --copy-devices          copy device contents as regular file
     --specials              preserve special files
 -D                          same as --devices --specials
 -t, --times                 preserve modification times
 -O, --omit-dir-times        omit directories from --times
     --super                 receiver attempts super-user activities
     --fake-super            store/recover privileged attrs using xattrs
 -S, --sparse                handle sparse files efficiently
 -n, --dry-run               perform a trial run with no changes made
 -W, --whole-file            copy files whole (without delta-xfer algorithm)
 -x, --one-file-system       don't cross filesystem boundaries
 -B, --block-size=SIZE       force a fixed checksum block-size
 -e, --rsh=COMMAND           specify the remote shell to use
     --rsync-path=PROGRAM    specify the rsync to run on the remote machine
     --existing              skip creating new files on receiver
     --ignore-existing       skip updating files that already exist on receiver
     --remove-source-files   sender removes synchronized files (non-dirs)
     --del                   an alias for --delete-during
     --delete                delete extraneous files from destination dirs
     --delete-before         receiver deletes before transfer, not during
     --delete-during         receiver deletes during the transfer
     --delete-delay          find deletions during, delete after
     --delete-after          receiver deletes after transfer, not during
     --delete-excluded       also delete excluded files from destination dirs
     --ignore-errors         delete even if there are I/O errors
     --force                 force deletion of directories even if not empty
     --max-delete=NUM        don't delete more than NUM files
     --max-size=SIZE         don't transfer any file larger than SIZE
     --min-size=SIZE         don't transfer any file smaller than SIZE
     --partial               keep partially transferred files
     --partial-dir=DIR       put a partially transferred file into DIR
     --delay-updates         put all updated files into place at transfer's end
 -m, --prune-empty-dirs      prune empty directory chains from the file-list
     --numeric-ids           don't map uid/gid values by user/group name
     --timeout=SECONDS       set I/O timeout in seconds
     --contimeout=SECONDS    set daemon connection timeout in seconds
 -I, --ignore-times          don't skip files that match in size and mod-time
     --size-only             skip files that match in size
     --modify-window=NUM     compare mod-times with reduced accuracy
 -T, --temp-dir=DIR          create temporary files in directory DIR
 -y, --fuzzy                 find similar file for basis if no dest file
     --compare-dest=DIR      also compare destination files relative to DIR
     --copy-dest=DIR         ... and include copies of unchanged files
     --link-dest=DIR         hardlink to files in DIR when unchanged
 -z, --compress              compress file data during the transfer
     --compress-level=NUM    explicitly set compression level
     --skip-compress=LIST    skip compressing files with a suffix in LIST
 -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
 -s, --protect-args          no space-splitting; only wildcard special-chars
     --address=ADDRESS       bind address for outgoing socket to daemon
     --port=PORT             specify double-colon alternate port number
     --sockopts=OPTIONS      specify custom TCP options
     --blocking-io           use blocking I/O for the remote shell
     --stats                 give some file-transfer stats
 -8, --8-bit-output          leave high-bit chars unescaped in output
 -h, --human-readable        output numbers in a human-readable format
     --progress              show progress during transfer
 -P                          same as --partial --progress
 -i, --itemize-changes       output a change-summary for all updates
     --out-format=FORMAT     output updates using the specified FORMAT
     --log-file=FILE         log what we're doing to the specified FILE
     --log-file-format=FMT   log updates using the specified FMT
     --password-file=FILE    read daemon-access password from FILE
     --list-only             list the files instead of copying them
     --bwlimit=KBPS          limit I/O bandwidth; KBytes per second
     --write-batch=FILE      write a batched update to FILE
     --only-write-batch=FILE like --write-batch but w/o updating destination
     --read-batch=FILE       read a batched update from FILE
     --protocol=NUM          force an older protocol version to be used
     --iconv=CONVERT_SPEC    request charset conversion of filenames
     --checksum-seed=NUM     set block/file checksum seed (advanced)
 -4, --ipv4                  prefer IPv4
 -6, --ipv6                  prefer IPv6
     --version               print version number
(-h) --help                  show this help (-h is --help only if used alone)


10 Practical Examples of Rsync Command in Linux

Remote Sync is a most commonly used command for copying and synchronizing files and directories remotely as well as locally in Linux systems. With the help of rsync command you can copy and synchronize your data remotely and locally across directories, across disks and networks, perform data backups and mirroring between two Linux machines.

You don’t need to be root user to run rsync command.

Advantages and features of Rsync
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 remote-update protocol which allows to transfer just the differences between two sets of files. 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 compression and decompression method while sending and receiving data both ends

We can install rsync package with the help of following command.

# yum install rsync (On Red Hat based systems)
# apt-get install rsync (On Debian based systems)



Basic syntax of rsync command

# rsync  <options>  <source>  <destination>

rsync commands options
-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

========================================================
1. Copy/Sync Files and Directory Locally

This following command will sync a single file on a local machine from one location to another location.
Example, a file name test.log.1.gz needs to be copied or synced to /tmp/data_backup/ folder.
---------------------------------------------------------
[root@localhost mnt]# rsync -zvh    test.log.1.gz     /tmp/data_backup/
created directory /tmp/data_backup
test.log.1.gz

sent 1.35K bytes  received 31 bytes  2.77K bytes/sec
total size is 1.27K  speedup is 0.92
---------------------------------------------------------
In above example, you can see that if the destination is not already exists rsync will create a directory automatically for destination
========================================================
The following command will transfer or sync all the files of from one directory to a different directory in the same machine

1.1 Copy/Sync Files and Directory Locally
---------------------------------------------------------
[root@localhost mnt]# rsync -avzh test/ /tmp/data_backup/
sending incremental file list
./
test.log.1.gz
test.log.2.gz
test.log.3.gz

sent 4.04K bytes  received 72 bytes  2.74K bytes/sec
total size is 3.82K  speedup is 0.93
========================================================
2. Copy/Sync Files and Directory to or From a Server

This command will sync a directory from a local machine to a remote machine.
---------------------------------------------------------
[root@localhost]$ rsync -avz test/ root@192.168.10.11:/home/
root@192.168.10.11's password:
sending incremental file list
./
test.log.1.gz
test.log.2.gz
test.log.3.gz
sent 4993369 bytes  received 91 bytes  399476.80 bytes/sec
total size is 4991313  speedup is 1.00
========================================================
2.1 Copy/Sync a Remote Directory to a Local Machine

This command will help you sync a remote directory to a local directory
---------------------------------------------------------
[root@tecmint]# rsync -avzh root@192.168.10.11:/home/test /tmp/backup/
root@192.168.10.11's password:
receiving incremental file list
created directory /tmp/backup
test.log.1.gz
test.log.2.gz
test.log.3.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   

Copy a File from a Remote Server to a Local Server with SSH

To specify a protocol with rsync you need to give “-e” option with protocol name you want to use.
Here in this example, We will be using “ssh” with “-e” option and perform data transfer.

[root@localhost]# rsync -avzhe ssh root@192.168.10.11:/var/log/mgimx/access.log /tmp/
========================================================
3.1 Copy a File from a Local Server to a Remote Server with SSH

[root@localhost]# rsync -avzhe ssh backup.tar root@192.168.10.11:/tmp/backups/
========================================================
4. Show Progress While Transferring Data with rsync

[root@localhost]# rsync -avzhe ssh --progress /home/demo.test root@192.168.0.100:/root/demo.test
root@192.168.0.100's password:
sending incremental file list
created directory /root/demo.test
demo.test/
demo.test/httpd-2.2.3-82.el5.centos.i386.rpm
1.02M 100%        2.72MB/s        0:00:00 (xfer#1, to-check=3/5)
demo.test/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)
demo.test/nagios-3.5.0.tar.gz
1.79M 100%        1.56MB/s        0:00:01 (xfer#3, to-check=1/5)
demo.test/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 allows 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, rsync command will include those files and directory only which
starts with ‘php’ and exclude all other files and directory.

[root@localhost]# rsync -avze ssh --include 'php*' --exclude '*' root@192.168.0.101:/var/www/html/ /tem/demo
========================================================
6. Use of –delete Option

If a file or directory 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 ‘–delete‘ option to delete files that are not there in source directory.

Source and target are in sync. Now creating new file test.txt at the target.
-------------------------------------------------------------
[root@localhost]# touch test.txt
[root@localhost]# rsync -avz --delete root@192.168.10.11:/var/www/html/  .
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 synchronize with the source with ‘–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 “–max-size” option. Here in this example,
ax file size is 200k, so this command will transfer only those files which are equal or smaller than 200k.

[root@localhost]# rsync -avzhe ssh --max-size='200k' /var/www/html/ root@192.168.0.100:/tmp/backup 
========================================================

8. Automatically Delete source Files after successful Transfer
Now, 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.

So, will you wait for transfer to complete and then delete those local backup
file manually? Of Course NO. This automatic deletion can be done using ‘–remove-source-files‘ option.

[root@localhost]# 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
If you are a newbie and using rsync and don’t know what exactly your command going 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 same you want to do then you can
remove ‘–dry-run‘ option from your command and run on the terminal.

root@localhost]# 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 the help of ‘–bwlimit‘ option. This options helps us to limit I/O bandwidth.
----------------------------------------------------------
[root@localhost]# rsync --bwlimit=100 -avzhe ssh  /var/www/html/  root@192.168.0.100:/tmp/backups/
root@192.168.10.11'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 whole file then you use ‘-W‘ option with it.
----------------------------------------------------------
[root@localhost]# 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
 =====================================================
 How to Sync Two Apache Web Servers/Websites Using Rsync
 =====================================================
How to Use Rsync to Sync New or Changed/Modified Files in Linux 
 =====================================================