Synchronizing folders in Unix with rsync

May 18, 08 by the programmer

Very important think when you are a programmer is to have the ablility and tools to synchronize your code on multiple locations.

For example you can have a development version a test version and a production version of the code, and you will want all of these versions to be synchronized.

One way of doing that in Unix operating systems is by using the rsync command.

Rsync is a free open source project.

In order to use this command you have to install it first. This article will not go into the installation procedure. You will have to download rsync from the official site (http://samba.anu.edu.au/rsync/) and install it on your unix operating system.

Usage examples

Rsync is a very efficient command because it uses a special protocol (rsync remote-update protocol) to synchronize files. It only synchronizes the differences between files, not the all files, which makes it very fast.

The command can be used to synchronize files/folders on a local computer and between remote computers.

The command has the following syntax:

rsync [options] src dest

  • options - see available options bellow
  • src - This is the source path. It can be a local or remote location. This is where the files that your are going to synchronize are located.
  • dest - This is the local or remote destination that you want to be synchronized with the source destination. All the files from the source destination will be copied here, and the source and destination folders will be the same.

You must specify a source and a destination, one of which may be remote.

Perhaps the best way to explain the syntax is with some examples:

rsync *.php foo:src/

this would transfer all files matching the pattern *.php from the current directory to the directory src on the machine foo.

rsync -avz foo:src/my_web_site /data/tmp

this would recursively transfer all files from the directory src/my_web_site on the machine foo into the /data/tmp/my_web_site directory on the local machine. The files are transferred in “archive” mode, which ensures that symbolic links, devices, attributes, permissions, ownerships etc are preserved in the transfer. Additionally, compression will be used to reduce the size of data portions of the transfer.

rsync -avz foo:src/my_web_site/ /data/tmp

a trailing slash on the source changes this behavior to transfer all files from the directory src/my_web_site on the machine foo into the /data/tmp/. A trailing / on a source name means “copy the contents of this directory”. Without a trailing slash it means “copy the directory”. This difference becomes particularly important when using the –delete option.

You can also use rsync in local-only mode.

rsync -avz *.php /home/dev/src/

The above line means synchronize all the php files from the current local directory to the “/home/dev/src/” local directory, and preserve all file/folder attributes.

The most interesting option that rsync has is the ability to use the command only to see what the diffrences are between two destinations. You can do that by sing the -n option.

rsync -navz *.php /home/dev/src/

The above line will only display the differences between the current folder and the “/home/dev/src/” folder, but it will not make any changes. This is very useful if you want to see what files you have worked on or which files were changed.

Available options

There are all the available options in rsync.

 -v, --verbose               increase verbosity
-q, --quiet                 decrease verbosity
-c, --checksum              always checksum
-a, --archive               archive mode
-r, --recursive             recurse into directories
-R, --relative              use relative path names
-b, --backup                make backups (default ~ suffix)
--backup-dir            make backups into this directory
--suffix=SUFFIX         override backup suffix
-u, --update                update only (don't overwrite newer files)
-l, --links                 copy symlinks as symlinks
-L, --copy-links            copy the referent of symlinks
--copy-unsafe-links     copy links outside the source tree
--safe-links            ignore links outside the destination tree
-H, --hard-links            preserve hard links
-p, --perms                 preserve permissions
-o, --owner                 preserve owner (root only)
-g, --group                 preserve group
-D, --devices               preserve devices (root only)
-t, --times                 preserve times
-S, --sparse                handle sparse files efficiently
-n, --dry-run               show what would have been transferred
-W, --whole-file            copy whole files, no incremental checks
--no-whole-file         turn off --whole-file
-x, --one-file-system       don't cross filesystem boundaries
-B, --block-size=SIZE       checksum blocking size (default 700)
-e, --rsh=COMMAND           specify rsh replacement
--rsync-path=PATH       specify path to rsync on the remote machine
-C, --cvs-exclude           auto ignore files in the same way CVS does
--existing              only update files that already exist
--ignore-existing       ignore files that already exist on the receiving side
--delete                delete files that don't exist on the sending side
--delete-excluded       also delete excluded files on the receiving side
--delete-after          delete after transferring, not before
--ignore-errors         delete even if there are IO errors
--max-delete=NUM        don't delete more than NUM files
--partial               keep partially transferred files
--force                 force deletion of directories even if not empty
--numeric-ids           don't map uid/gid values by user/group name
--timeout=TIME          set IO timeout in seconds
-I, --ignore-times          don't exclude files that match length and time
--size-only             only use file size when determining if a file should be transferred
--modify-window=NUM     Timestamp window (seconds) for file match (default=0)
-T  --temp-dir=DIR          create temporary files in directory DIR
--compare-dest=DIR      also compare destination files relative to DIR
-P                          equivalent to --partial --progress
-z, --compress              compress file data
--exclude=PATTERN       exclude files matching PATTERN
--exclude-from=FILE     exclude patterns listed in FILE
--include=PATTERN       don't exclude files matching PATTERN
--include-from=FILE     don't exclude patterns listed in FILE
--version               print version number
--daemon                run as a rsync daemon
--no-detach             do not detach from the parent
--address=ADDRESS       bind to the specified address
--config=FILE           specify alternate rsyncd.conf file
--port=PORT             specify alternate rsyncd port number
--blocking-io           use blocking IO for the remote shell
--no-blocking-io        turn off --blocking-io
--stats                 give some file transfer stats
--progress              show progress during transfer
--log-format=FORMAT     log file transfers using specified format
--password-file=FILE    get password from FILE
--bwlimit=KBPS          limit I/O bandwidth, KBytes per second
--read-batch=PREFIX     read batch fileset starting with PREFIX
--write-batch=PREFIX    write batch fileset starting with PREFIX
-h, --help                  show this help screen
Add your comment

5 responses for this post

  1. navz Says:

    […] project. In order to use this command you have to install it first. This article will not go into thttp://minanov.com/synchronizing-folders-in-unix-with-rsync-21Cramer’s ‘Mad Money’ Recap: Stay with New Tech TheStreet.comCramer says this long-term theme will […]

  2. Web 2.0 Announcer Says:

    Synchronizing folders in Unix with rsync…

    […]How to synchronize folders in Unix with rsync[…]…

  3. Synchronizing folders in Unix with rsync Says:

    […] http://minanov.com/synchronizing-folders-in-unix-with-rsync-21  […]

  4. Mike G. Says:

    One detail - the -z option on rsync doesn’t make sense for local file transfers, and at least my version of rsync isn’t smart enough to skip compressing/decompressing for a local transfer.

    So an
    rsync -av largefile dest
    takes 4 seconds for me for a 49 meg largefile, and
    rsync -avz largefile dest2
    takes 46 seconds!

    So you REALLY don’t want that -z for local transfers…

  5. Gforex Says:

    Gforex…

    i do agree but i don’t think that’s completely true. I’m so confused!…

Leave a Reply