Home > Useful Commands > Synchronize or backup files with “rsync”

Synchronize or backup files with “rsync”

rsync” is a powerful synchronization command for files and directories (local and remote) commonly available for Unix and Windows systems, despite it’s very useful, using it can be intimidating at first (if a mistake is made, stuff could get wrongfully replaced or lost :-P ).

Let’s explain how it works by example, here’s the basic syntax for the command:

rsync [OPTIONS...] SRC... [DEST]

Suppose we have two directories:

folder1/
  cssfile1.css
  jsfile1.js
  phpfile1.php
  subfolder1/
    cssfile2.css
    phpfile2.php
    subfolder2/ 
      phpfile3.php

folder2/
  [EMPTY]

Simplest case, want to sync folder2 with folder1?, just run:

rsync -av folder1/ folder2
 
[OUTPUT, files copied]:
  ./
  cssfile1.css
  phpfile1.php
  subfolder1/
  subfolder1/cssfile2.css
  subfolder1/phpfile2.php
  subfolder1/subfolder2/
  subfolder1/subfolder2/phpfile3.php

And that’s it!, one important thing to note is the trailing “/” at the SRC parameter:

- SRC with trailing slash tells rsync: “Check and copy ONLY the contents of SRC to DEST directory”.
- SRC without trailing slash tells rsync: “Create a subdirectory in DEST and put the copied files/directories inside”.

How about copying only files with certain extension (recursively and keeping the directory structure)?

rsync -av --include="*/" --include="*.php" --exclude="*" folder1/ folder2
 
[OUTPUT, files copied]:
  ./
  phpfile1.php
  subfolder1/
  subfolder1/phpfile2.php
  subfolder1/subfolder2/
  subfolder1/subfolder2/phpfile3.php

Only PHP files and recursively! nicee :-)

First “include” is to keep the sync/copy recursive, second “include” is to copy only PHP files and “exclude” is used to keep out everything else.

rsync is heavy gear, most commonly used to keep servers in sync, here through an SSH connection:

rsync -avze SSH ssh_user@server:/path/to/server/dir/ /path/to/local/dir

The most awesome feature of rsync is that is “smart”, it transfers only what is supposed to (new or modified files); for modified files it can transfer only the part that changed, some kind of “diff”, keeping the execution really fast, even for big directories.

An interesting use is to combine it with CRON to keep servers in sync, e.g. keeping an Staging server in sync with Production, etc.

rsync options used in this examples:

-a archive mode, shorthand for -r -l -p -t -g -o -D, useful for backups
-v verbose
-z compress file data during the transfer
-e remote shell to use 
Categories: Useful Commands Tags: , , ,
  1. November 29th, 2011 at 00:59 | #1

    and backs up your emails

  1. No trackbacks yet.