Home arrow Site Administration arrow Page 4 - File Synchronization With Rsync

Temporary Insanity - Administration

Save time (and money) on data transfers between hosts withrsync, a synchronization tool that allows easy, efficient replication offiles between different locations. Sync up, now!

TABLE OF CONTENTS:
  1. File Synchronization With Rsync
  2. Getting The Skinny
  3. Building Blocks
  4. Temporary Insanity
  5. Remote Control
  6. Doing More
  7. What's In A Name?
  8. Mirror, Mirror, On The Wall
  9. Link Out
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 93
December 04, 2002

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
The first (and most basic) thing you can do with rsync involves using it as a more intelligent copy command on your local system. So, let's suppose that I have the following directory tree in my home area on my Linux box,
[me@olympus] $ tree -d /home/me
|-- Desktop
|   |-- Autostart
|   |-- Templates
|   `-- Trash
|-- bin
|-- mail
|-- public_html
|   |-- cache
|   `-- chatserver
|       |-- chat
|       |   |-- admin
|       |   |-- config
|       |   |-- images
|       |   |   |-- admin
|       |   |   |-- smilies
|       |   |   `-- tutorials
|       |   |-- install
|       |   |   |-- database
|       |   |   `-- languages
|       |   |-- lib
|       |   |   |-- commands
|       |   |   `-- database
|       |   `-- localization
|       |       `-- english
|       `-- docs
|-- test
`-- tmp
27 directories
and let's further suppose that I need to copy my personal Web pages to a backup folder ("/mnt/zip/backup") on the same system.
[me@olympus] $ ls -l /mnt/zip/backup
total 0
With rsync, accomplishing this is a snap:
[me@olympus] $ rsync --verbose --stats --recursive public_html
/mnt/zip/backup/Number of files: 177Number of files transferred: 158Total file size: 1043209 bytesTotal transferred file size: 1043209 bytesLiteral data: 0 bytesMatched data: 1043209 bytesFile list size: 3453Total bytes written: 15469Total bytes read: 12018
As with a regular "cp" command, rsync needs to know the source and destination for the copy operation. Once it has this information, it compares the files in the two locations and updates the destination to an exact replica of the source. Let's see if it worked as advertised:
[me@olympus] $ ls /mnt/zip/backup/
public_html
Yup, it did - as you can see, my backup folder now contains a copy of my Web pages.

You can specify more than one source directory to be copied as well - let me add my "mail" directory to the list and run the command again.
[me@olympus] $ rsync --verbose --stats --recursive public_html mail
/mnt/zip/backup/Number of files: 181Number of files transferred: 161Total file size: 1043209 bytesTotal transferred file size: 1043209 bytesLiteral data: 0 bytesMatched data: 1043209 bytesFile list size: 3513Total bytes written: 15637Total bytes read: 12066wrote 15637 bytes read 12066 bytes 18468.67 bytes/sectotal size is 1043209  speedup is 37.66
Next, let's make a few changes to the original files, and see if rsync can detect them and selectively update the destination the next time I sync up.
[me@olympus] $ touch public_html/a.dat
[me@olympus] $ ls > public_html/a.dat[me@olympus] $ touch public_html/b.dat[me@olympus] $ rm public_html/cache/test.html[me@olympus] $ vi public_html/error.php
As you can see, I've added a couple of new files, deleted one old file and made changes to one PHP script. Let's sync up again and see what happens.
[me@olympus] $  rsync --verbose --stats --recursive public_html
/mnt/zip/backup/
Pretty cool, huh? rsync added the two extra files to my backup, and identified and copied the modified file as well. However, the single file I deleted from the source is still present in the backup - obviously, rsync didn't delete it.

A quick look at the rsync documentation clears that one up - by default, rsync doesn't delete files from the destination when synchronizing directories. This default behaviour can be overridden by adding the "--delete" parameter to the rsync command line.
[me@olympus] $ rsync --verbose --stats --recursive --delete public_html
/mnt/zip/backup/
And now my destination is an exact copy of my source.
[me@olympus] $ ls /mnt/zip/backup/public_html/cache/
index.php m2_h.gif m2_n.gif tmp.gif
It should be noted that the "--delete" option can cause substantial damage if used unwisely - the rsync manual suggests always performing a dry run first when using this option.

 
 
>>> More Site Administration Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

SITE ADMINISTRATION ARTICLES

- Coding: Not Just for Developers
- To Support or Not Support IE?
- Administration: Networking OSX and Win 7
- DotNetNuke Gets Social
- Integrating MailChimp with Joomla: Creating ...
- Integrating MailChimp with Joomla: List Mana...
- Integrating MailChimp with Joomla: Building ...
- Integrating MailChimp with Joomla
- More Top WordPress Plugins for Social Media
- Optimizing Security: SSH Public Key Authenti...
- Patches and Rejects in Software Configuratio...
- Configuring a CVS Server
- Managing Code and Teams for Cross-Platform S...
- Software Configuration Management
- Back Up a Joomla Site with Akeeba Backup

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: