This project uses inotify and rsync to synchronize the server in real time, where inotify is used to monitor file system events, and rsync is a widely used synchronization algorithm, which has the advantage of operating only the different parts of the file. So its advantage is much more than the way to use the hook file system for mirroring synchronization.
The more current version of the Synchronizer is Inotify-tools, and the other is the Google Open source project Openduckbill (dependent on Inotify-tools), both written in the scripting language, The design idea is also to use inotify and rsync command. Compared with the above two projects, the advantages of this project are:
1.sersync is written in C + + and filters the temporary files and duplicate file operations generated by the Linux system file system (which I'll mention later), thus saving both runtime and network resources when combined with rsync synchronization. So faster.
2. Compared with the above two items, Sersync configuration is very simple: Download the source code in Http://code.google.com/p/sersync/downloads/list (divided into 32 versions, and 64-bit version), In the bin directory, I have compiled a 2 of the file, with the bin directory of the XML file directly to use.
3. In addition, this project compared with other scripts open source projects, using multi-threaded synchronization, especially when synchronizing large files, can ensure that multiple servers in real-time synchronization state.
4. This project comes with an error handling mechanism to re-synchronize the failed files every 10 hours through the failure queue to re-error the failed file and, if it still fails.
5. This project comes with crontab function, just open in the XML configuration file, can be synchronized at your request, at intervals of time.
6. This project comes with socket and HTTP protocol extension to meet the needs of two times development.
Basic architecture:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/60/wKiom1VH9ivhENNvAAKnrM0fzV4465.jpg "title=" w_4]%) 7N2A ' VO%I7GFP ') t0.jpg "alt=" Wkiom1vh9ivhennvaaknrm0fzv4465.jpg "/>
Design Brief analysis:
as shown, the thread group thread is the daemon thread waiting for the thread queue, and when there is data in the queue, the thread group Daemon wakes up one after the other, and when the INotify event in the queue is full, it is all woken up to work. The purpose of this design is to be able to handle multiple inotify events at the same time, and re-use the server's concurrency capability (the number of cores *2+2).
It is called thread group thread, because each thread at work, depending on the number of servers to establish child threads, the child thread can ensure that all the files with the individual servers simultaneously, when the file to synchronize large, so that the design can ensure that each remote server can simultaneously obtain the files to synchronize.
The service thread has three functions, first of all the files are processed for synchronization failures, the files are synchronized again, and for files that fail again, the rsync_fail_log.sh script is generated and the failed events are logged. Scripts are executed at the same time every 10 hours, and the script is emptied. The third function of the service thread is the crontab function, which can synchronize all paths once at a certain time.
The filter queue is created to filter the duplicated INotify information generated in a short period of time, for example, when the folder is deleted, INotify will simultaneously produce the deleted folder files and delete the folder event, by filtering the queue when the deletion of the folder event occurs, Filters out all the deleted files that were previously added to the queue, resulting in an event that eases the burden of synchronization. At the same time, to modify the operation of the file, it will produce temporary files and repeat operations.
Example:
When we perform a WQ operation on a test file in VI, the following events are generated:
650) this.width=650; "class=" blogimg "src=" Http://www.linuxidc.com/upload/2012_02/120206185071032.bmp "style=" border:0px; "alt=" 120206185071032.bmp "/>
Even if you put the "." Beginning with "~" End of the world filtered, for the test file still have 3 operations, namely Delete, create and save, by filtering the queue, there is only one event, to a certain extent also improve efficiency.
Filter queue The second function, that is, when you delete a directory in the local, if you delete a directory with 5 files, INotify will produce 6 events, 5 file deletion events, and a delete directory event, if you use filter queue, normally will only produce a delete directory event, Greatly reduces the number of rsync communications. (Of course, this is not absolute.) If these 6 events are read into the queue multiple times, it is possible that the synchronization thread has taken the synchronization from the queue before it can be filtered out. But to a certain extent, you can reduce the number of simultaneous traffic to delete a folder).
Filter the third role of the queue, you can filter the folder under the Monitoring directory, if you do not want to sync some folders under the directory, or some suffix of the file. For sub-folders that do not need to be monitored, remove the monitor at inotify boot time, and the resulting file events will be filtered out of the sync queue before they are required for monitoring sub-files. If you use rsync with--exclude, this can be filtered, but it also interacts with the rsync daemon.
Architecture diagram:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/6C/60/wKiom1VH9wjRGkE7AAC_h0EvOzI248.jpg "title=" (5}@{ Q9@nz ' Vm77vz4wjr38.png "alt=" Wkiom1vh9wjrgke7aac_h0evozi248.jpg "/>
2. List of installation environment resources
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/5C/wKioL1VH-XqToncoAAFqdY0mXfc463.jpg "title=" k_icd0 ) 690p9e_jy ' O9qpa4.png "alt=" Wkiol1vh-xqtoncoaafqdy0mxfc463.jpg "/>
2.2 Checking the system environment
Cat/etc/redhat-release
Check the kernel version uname-r check the number of system bits uname-m
2.3 Check Upgrade rsync to 3.0
Rrync--version | Head-2 (can be upgraded with yum install rsync-y command if not installed)
2.4. Configuring the Rsync configuration file (this is to be created at the same time as 168 and 169 machines)
(The system is not automatically generated here, I want to create the rsync.conf profile manually)
(3 real-time synchronization directories created in the configuration file)
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/60/wKiom1VH-kmyaw7vAALfk1lzrYE175.jpg "title=" zzye6e }IUVH (O8~CFAW) vve.png "alt=" Wkiom1vh-kmyaw7vaalfk1lzrye175.jpg "/>
2.5 Creating three synchronization directories
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/60/wKiom1VH-tKjYFgEAACEEkjSAOM059.jpg "title=" Li4ys] Lpo01i6k]a6${gkfg.png "alt=" Wkiom1vh-tkjyfgeaaceekjsaom059.jpg "/>
2.6. Create permissions to configure authentication
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/5C/wKioL1VH_ImDfUe0AAChBqj818U359.jpg "title=" CG] Yal@6jl%rxm[b}21$3sd.png "alt=" Wkiol1vh_imdfue0aachbqj818u359.jpg "/>
2.7. Turn on rsync daemon
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/6C/60/wKiom1VH--fQ6wRVAABdrx7KNAk603.jpg "title=" 1] MM4BW (8%V{D$CV1) l%td4.png "alt=" Wkiom1vh--fq6wrvaabdrx7knak603.jpg "/>
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/5C/wKioL1VH_ZLzHfSzAAEWfd8SH5o569.jpg "title=" 8FGZH] 8tq26g@io% @D2DIT1. png "alt=" wkiol1vh_zlzhfszaaewfd8sh5o569.jpg "/>2.8.rsync service joins the start-up service
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/60/wKiom1VH_IHyVFvLAADQtgJd3o8814.jpg "title=" ZP8WR " (%0zzj[rx7]vsymfg.png "alt=" Wkiom1vh_ihyvfvlaadqtgjd3o8814.jpg "/>2.9. Close the rsync server
Pkill rsync
Rsync--deamon
Ps-ef |grep rsync
3. Configure rsync on the master167 server
3.1 Configure rsync permissions on master (the transmission password configured here is consistent with S1 and S2) 650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M01/6C/5C/wKioL1VH _3iqhfiiaacxoxtnntc772.jpg "title=" 0r00cq}467jfgmaocie5 (p4.png "alt=" Wkiol1vh_3iqhfiiaacxoxtnntc772.jpg "/>
3.2. Create a synchronized three directory on master (Www/bbs/www/blog www/www)
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/5C/wKioL1VIAFPSVha5AAEpr8cMt04416.jpg "title=" K) Unkfhb3uq9c2pf}57g@vl.png "alt=" Wkiol1viafpsvha5aaepr8cmt04416.jpg "/>
3.3 Manually perform a sync test (here I'm shutting down Ipabes Firewall service iptables stop in order not to affect the test)
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M02/6C/5C/wKioL1VIAQfgIsz4AAKCHDelhek004.jpg "title=" eqb4_ Eu$vs9e9 (z3l2n[]4n.png "alt=" Wkiol1viaqfgisz4aakchdelhek004.jpg "/>
To view the results of the execution:
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/60/wKiom1VH_96zsuopAADQ5X0LciU572.jpg "title="]i_~) G1rr59}n1i4%h0) 3w3.png "alt=" wkiom1vh_96zsuopaadq5x0lciu572.jpg "/>650" this.width=650; "src=" http:// S3.51cto.com/wyfs02/m02/6c/60/wkiom1viaahs_xv8aabzpac9giy160.jpg "title=" 4}dnmy{$LYR 1wx7g[(%2}_3.png "alt=" Wkiom1viaahs_xv8aabzpac9giy160.jpg "/>
OK, manual sync here no problem!! Hey
4. Start deployment on Master Sersync service
Software for
Mkdir-p/home/zzx/tools
Cd/home/zzx/tools
wget http://sersync.googlecode.com/files/sersync2.5.4_64bit_binary_stable_final.tar.gz
Can browse http://code.google.com/p/sersync/downloads/list
Decompression software
TAR-ZXF sersync2.5.4_64bit_binary_stable_final.tar.gz-c/usr/local/
cd/usr/local/
MV Gnu-linux-x86 Sersync
4.1 Specification Sersync configuration file
Cd/sersync
mkdir conf bin Logs
MV Confxml.xml conf
MV Sersync2 Bin/sersync
4.2 Backing up configuration files and modifying configuration files
---/bin/cp/conf/confxml.xml conf/confxml.xml.zzx.$ (date+%f)
OK CP confxml.xml confxml.xml.zzx.$ (date +%f)
(1) Modify the Conf/confxml.xml (24 lines to add the following)
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/60/wKiom1VIAfaTYctAAAOGJLGHROg726.jpg "title=" z[%n2) %6jo7g30@fmy{h8@e.jpg "alt=" Wkiom1viafatyctaaaogjlghrog726.jpg "/>
4.3 Turn on Sersync daemon
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/5F/wKioL1VIO0Sg25rIAACA1sUY6ZQ638.jpg "title=" 0PSIBI ) [25zxjp6xa}uzxtt.png "alt=" Wkiol1vio0sg25riaaca1suy6zq638.jpg "/>
4.4 Start Sersync
Sersync-r-d-o/usr/local/sersync/conf/confxml.xml
-R Synchronous Replication-D background Boot-o Specifies that the configuration file is not a binary file
There's a little episode here!! (This refers to the synchronization, the Www/www directory is valid)
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/5F/wKioL1VIQCTTmHgRAADI2aYZAyc973.jpg "title=" j~og~$ Zjdsf76cx ' 57ct~bb.png "alt=" Wkiol1viqcttmhgraadi2ayzayc973.jpg "/>
View the directories that are S1 and S2
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6C/63/wKiom1VIPyGivXTNAADjnojA8uw677.jpg "title=" UJW9 ( Ai]o} ' u]wk%rz]fhit.png ' alt= "wkiom1vipygivxtnaadjnoja8uw677.jpg"/>
To solve this problem, we configure a confing.xml for each directory (which is configured to produce a multi-instance configuration)
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/6C/63/wKiom1VIP6GyiLmkAAHVvo_ocjc679.jpg "title=" 3del5ykg$y$ ([ys%) @EZF $S. png "alt=" wkiom1vip6gyilmkaahvvo_ocjc679.jpg "/> are Bbs_confxml.xml, Www_confxml.xml, Blog_confxml.xml configuration files are synchronized with different directories
650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6C/63/wKiom1VIQUqC5zwiAAOgbrSurxI372.jpg "title=") UHW8W6R7SC@6IPA) n[fov1.png "alt=" Wkiom1viquqc5zwiaaogbrsurxi372.jpg "/>
Sersync Real-time synchronization software