[me@olympus] $ tree -d /home/meand let's further suppose that I need to copy my personal Web pages to a backup folder ("/mnt/zip/backup") on the same system.
|-- Desktop
| |-- Autostart
| |-- Templates
| `-- Trash
|-- bin
|-- public_html
| |-- cache
| `-- chatserver
| |-- chat
| | |-- admin
| | |-- config
| | |-- images
| | | |-- admin
| | | |-- smilies
| | | `-- tutorials
| | |-- install
| | | |-- database
| | | `-- languages
| | |-- lib
| | | |-- commands
| | | `-- database
| | `-- localization
| | `-- english
| `-- docs
|-- test
`-- tmp
27 directories
[me@olympus] $ ls -l /mnt/zip/backupWith rsync, accomplishing this is a snap:
total 0
[me@olympus] $ rsync --verbose --stats --recursive public_htmlAs 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:
/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
[me@olympus] $ ls /mnt/zip/backup/Yup, it did - as you can see, my backup folder now contains a copy of my Web pages.
public_html
[me@olympus] $ rsync --verbose --stats --recursive public_html mailNext, 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.
/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
[me@olympus] $ touch public_html/a.datAs 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] $ 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
[me@olympus] $ rsync --verbose --stats --recursive public_htmlPretty 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.
/mnt/zip/backup/
[me@olympus] $ rsync --verbose --stats --recursive --delete public_htmlAnd now my destination is an exact copy of my source.
/mnt/zip/backup/
[me@olympus] $ ls /mnt/zip/backup/public_html/cache/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.
index.php m2_h.gif m2_n.gif tmp.gif
log file = /var/log/rsyncd.logAs you can see, this configuration file is similar to a standard Windows INI file, in that it is broken up into different sections or "modules", each containing variable-value pairs. Modules are identified by square braces around the module name, and lines beginning with semi-colons (;) or hashes (#) are treated as comments and ignored.
[home]
path = /home/me
comment = My Home Area
list = yes
read only = no
log file = /var/log/rsyncd.logThe second part sets up a module on the server - this is simply a directory that is available to all connecting clients. In this case, I've selected the "/home/me" directory, given it the share name "home" and set it to be writeable by all users.
motd file = /var/log/message.txt
[home]The
path = /home/me comment = My Home Area list = yesread only = no
path = /home/meoption tells rsync where to locate the module on the server, while the
list = yesoption tells it to include the module in the list returned to connecting clients.
read only = nooption.
[me@olympus] $ rsync --daemonIf the rsync daemon starts up OK, you can attempt to connect to it from another host. Let's assume that this second host is named "xanadu", and already has rsync installed on it. What I'd like to do is transfer my home directory on "olympus" - the same one I backed up on the previous page - to "xanadu". Here's how I'd go about it:
[me@xanadu] $ rsync --verbose --progress --stats --recursiveSynchronization need not be in one direction only. Using exactly the same setup as above - an rsync server on "olympus" and an rsync client on "xanadu" - it's also possible for me to transfer files in the other direction. Consider the following example, which illustrates by copying the "sql" directory to my home area on "olympus":
olympus::home/ .Number of files: 230Number of files transferred: 187Total file size: 1054649 bytesTotal transferred file size: 1054649 bytesLiteral data: 1054649 bytesMatched data: 0 bytesFile list size: 4318Total bytes written: 3052Total bytes read: 1066527wrote 3052 bytes read 1066527 bytes 713052.67 bytes/sectotal size is 1054649 speedup is 0.99[me@xanadu] $ lsbin Desktop mail public_html test tmp
[me@xanadu] $ rsync --verbose --progress --stats --recursive sql
[me@xanadu] $ rsync olympus::Now, if I were to add a few more modules to the configuration file,
home My Home Area
[temp]restart the rsync server on "olympus",
path = /tmp comment = Temp Arealist = yes
[me@olympus] $ killall rsyncand attempt to reconnect to it from "xanadu", I'd have access to the new modules as well.
[me@olympus] $ rsync --daemon
[me@xanadu] $ rsync olympus::You can exclude modules from being listed in this manner by specifying a
home My Home Areatemp Temp Area
list = nooption within the module configuration.
[me@xanadu] $ rsync --verbose --progress --stats --recursiveFinally, rsync's default behaviour when encountering symbolic links is to omit them - as in the following example:
--exclude="*.tmp" olympus::home/ .
[me@xanadu] $ rsync --progress --recursive olympus::home/ .As you can see, when I attempt to copy the "/home/me/public_html" directory to "xanadu", every symbolic link within that directory is skipped. You can have rsync retain these links as is during the copy process by specifying the "--links" option on the command line,
skipping non-regular file "public_html/config.lib.php3" skippingnon-regular file "public_html/start.php"
[me@xanadu] $ rsync --progress --recursive --links olympus::home/ .or replace the symbolic links by the actual files being referenced with the "--copy-links" option.
[me@xanadu] $ rsync --progress --recursive --copy-links olympus::home/
.
ไม่มีความเห็น