Linux RSYNC command

rsync is a Linux tool for backup and the creation of mirror. It support copying of links, preserving owners, groups and permission. It can use ssh to do the copy. It is extremely useful for mirroring other machine. rsync can work for both directions, remote to local, and local to remote.
Anther advantage of the rsync is that it can compress the data during the sending to the remote machine, and by that save bandwidth and time.

The command has the following structure:

rsync <option> <source> <destination>

rsync useful  options:

  • -v  –verbose
    Show more information during the copy/sync
  • -h –human-readable
    show the number in human readable format, helpful for show file size in readable format.
  • -p  –perms
    preserve  the file permission, the destination will have the same permission as the source.
  • -z  –compress
    The data is compressed during the transfer.
  • -l  –links
    soft link (symbolic link)  are copied as soft link
  • -t  –time
    preserver the source modification time in the destination.
  • –delete
    will clean the destination from any file that doesn’t exist in the sources.
  • -r  –recursive
    recursive copy of directories.
  • -a  –archive
    archive mode. excellent for backup.
    This option include all the following:

    -r, –recursive recurse into directories
    -l, –links copy symlinks as symlinks
    -p, –perms preserve permissions
    -t, –times preserve modification times
    -g, –group preserve group
    -o, –owner preserve owner (super-user only)
    -D same as –devices –specials
    –devices preserve device files (super-user only)
    –specials preserve special files

So for example I have the following node project:


oren@oren-computer:~/workspace/scripts/nodeScripts/testing  
$ ls -l
total 28K
-rw-rw-r--   1 oren oren 1.7K אוק  8 21:50 app.js
drwxr-xr-x   2 oren oren 4.0K אוק  8 12:53 bin
drwxrwxr-x 107 oren oren 4.0K אוק  9 06:29 node_modules
-rw-rw-r--   1 oren oren  353 אוק  9 06:29 package.json
drwxr-xr-x   5 oren oren 4.0K אוק  8 21:32 public
drwxr-xr-x   2 oren oren 4.0K אוק  8 19:43 routes
drwxr-xr-x   2 oren oren 4.0K אוק  8 12:53 views

And I want to create backup for it on server 10.0.0.7, on folder backup and using oren as a user name to connect the server, I will run the following:


oren@oren-computer:~/workspace/scripts/nodeScripts/testing  
$ rsync -avhz ~/workspace/scripts/nodeScripts/testing oren@10.0.0.7:~/backup
oren@10.0.0.7's password: 
sending incremental file list
testing/
testing/app.js
testing/package.json
testing/bin/
testing/bin/www
testing/node_modules/
testing/node_modules/.bin/
testing/node_modules/.bin/acorn -> ../acorn/bin/acorn
testing/node_modules/.bin/cleancss -> ../clean-css/bin/cleancss
testing/node_modules/.bin/jade -> ../jade/bin/jade.js
testing/node_modules/.bin/mime -> ../mime/cli.js
testing/node_modules/.bin/mkdirp -> ../mkdirp/bin/cmd.js
testing/node_modules/.bin/semver -> ../semver/bin/semver
testing/node_modules/.bin/uglifyjs -> ../uglify-js/bin/uglifyjs
testing/node_modules/accepts/
testing/node_modules/accepts/HISTORY.md
testing/node_modules/accepts/LICENSE
testing/node_modules/accepts/README.md
testing/node_modules/accepts/index.js
testing/node_modules/accepts/package.json
testing/node_modules/acorn-globals/
.
.
.
.
.
.
testing/views/index.jade
testing/views/layout.jade

sent 2.36M bytes  received 23.49K bytes  367.04K bytes/sec
total size is 7.97M  speedup is 3.34

Now let analyze the command:
rsync
This is the command name.

-avhz
This is the command option. I used a for archive. z for compressing data during transfer. h for show the information as homan readble and v for verbose information, it show all the file name that it trasformed.

~/workspace/scripts/nodeScripts/testing
This was the source directory name

oren@10.0.0.7:~/backup
This is the destination. It mean the following: we want to copy it to server 10.0.0.7, to the folder backup in the home directory of the user, and to use user oren to connect to the destination server.
The part before the “:”, is the server information, the part after the “:” is the folder information. before the “@” we have the user name and after the “@” we have the server name or ip.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s