Tuesday, 12 July 2011

Rsync

Rsync is a fast, versatile, remote (and local) file-copying tool which synchronizes files and directories from one location to another.

It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination.

Rsync is widely used for backups and mirroring and as an improved copy command for everyday use.

Access via remote shell:


Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST

Push:

If you want to push the data from /home of the server to another server then syntax would be 

rsync -vrplogDtH --exclude-from="/root/ignoredir" -e ssh /home/* root@IP:/home

rsync -vrplogDtH --exclude-from="/root/ignoredir" -e ssh /home/* root@10.10.10.101:/backup1/

Pull:

If you want to pull the data from /home of another server to your server then syntax would be  

rsync -vrplogDtH --exclude-from="/root/ignoredir" -e ssh  root@IP:/home/* /home/

Now let us summarize the regular options used in rsync for data copy.

-delete-excluded
In addition to deleting the files on the receiving side that are not on the sending side, this tells rsync to also delete any files on the receiving side that are excluded
Pull:
rsync -vrplogDtH --exclude-from="/root/ignoredir" -e ssh root@10.10.10.100:/home/* /home

-v, --verbose increase verbosity
-r, --recursive recurse into directories
-p, --perms preserve permissions
This option causes the receiving rsync to set the destination permissions to be the same as the source permissions.
-l, --links copy symlinks as symlinks
-o, --owner preserve owner (super-user only)
This option causes rsync to set the owner of the destination file to be the same as the source file, but only if the receiving rsync is being run as the super-user
-g, --group preserve group
-H, --hard-links preserve hard links
-e This option allows you to choose an alternative remote shell program to use for communication between the local and remote copies of rsync. Typically, rsync is configured to use ssh by default

-a, --archive archive mode; equals -rlptgoD

This option causes rsync to set the group of the destination file to be the same as the source file.
-D same as --devices –specials

--devices
This option causes rsync to transfer character and block device files to the remote system to recreate these devices.
--specials

This option causes rsync to transfer special files such as named sockets and fifos.

Under Linux and UNIX each and every hardware device treated as a file. a device file (also called as a special file) is an interface for a device driver

Device files are of two types


  • Character special files or Character devices
  • Block special files or Block devices

A character device is marked with a c as the first letter of the permissions strings.
$ ls -l /dev/console
A block device is marked with a b as the first letter of the permissions strings:
$ ls -l /dev/sdb1

-t, --times preserve modification times
This tells rsync to transfer modification times along with the files and update them on the remote system. Note that if this option is not used, the optimization that excludes files that have not been modified cannot be effective;

-H, --hard-links preserve hard links