Three, sersync + rsync file server to achieve real-time synchronization

First, why use rsync + sersync architecture?

1, sersync is based on inotify development, inotify-tools similar tools

2, sersync can record a file or directory is changed to listen directory (add, delete, modify) the specific name of a file or a directory, and then use rsync to synchronize the time, only sync changes

Two, rsync + difference inotify-tools and rsync + sersync architecture?

1、rsync+inotify-tools

a, inotify directory is recorded only in monitor changes (add, delete, change) which is not the particular file or which directory record change;

b, rsync synchronization of the time, does not know which specific file or directory changes, each time the entire directory synchronization, when a large amount of data, the entire directory synchronization is very time-consuming (rsync to the entire directory Find traverse the reference document), and therefore inefficient

 

2、rsync+sersync

a, sersync can record (add, delete, change) the specific name of a file or directory changes being monitored directory;

B, rsync file or directory in the synchronization, synchronization only changes (changes each time data corresponding to the entire synchronization of catalog data is small enough, when traversing rsync file lookup contrast, very fast), and therefore, high efficiency.

 

to sum up:

When a small amount of data directory synchronization, it is recommended to use rsync + inotify

When the directory synchronization of large amounts of data (hundreds of G 1T even more than) a lot of files, it is recommended to use rsync + sersync

 

 

#Rsync server configuration

 

1. deployment rsync service

yum install rsync # install rsync, if they think yum version is too low can also install the source code

2.vim /etc/rsyncd.conf # default rsync no configuration file, create a file in # and characters only comment, please use all annotations Clear

 

Server #Rsync
UID = the root
GID = the root
use safety-related CHROOT # = NO
max connections = 2000 # concurrent connections
timeout = 600 # timeout (in seconds)
pid = File / var / RUN / rsyncd.pid rsync the pid specified directory #
lock file = / var / run / rsync.lock # rsync specified lock file [important]
log file = /var/log/rsyncd.log # rsync specified log directory
ignore errors # ignore some I / O error
read only = false # rsync server settings file is read-write permissions
list = false # rsync server does not display the resource list
hosts allow = 10.1.0.0/16 # allows synchronization of customer data, client IP address, you can set up multiple, comma with the English state spaced
hosts deny = 0.0.0.0/32 # inhibiting data synchronization client IP address may be provided a plurality, separated by commas English state
auth users = rsync_backup # perform data synchronization user name, you can set up multiple, separated by commas in the English state
= / etc / rsync.password # user authentication configuration file, which saves the user name and password File Secrets
##### ############################################
[WWW] # module
comment WWW =
path = / Data / WWW /
######################################### ########
[BBS]
Comment BBS =
path = / Data / BBS /
############################# ####################
[Blog]
Comment = Blog
path = / Data / Blog /
#rsync_config____________end



3 , create a user authentication file

echo "rsync_backup: 123456"> / etc / rsync.password # configuration file, add the following

 

4 , set file permissions

chmod 600 /etc/rsync.password 

5. Start the daemon and writes the boot from the start

rsync --daemon
vim /etc/rc.local
# rsync server progress
/usr/bin/rsync --daemon

 

6. Create a relevant directory to be synchronized

mkdir -p /data/{www,bbs,blog}

 

#rsync client configuration

 

1. Installation the rsync , as above

2. Create a rsync configuration file, the client can be created without content

touch /etc/rsyncd.conf

3. Configure rsync client permissions Certification:

 echo "123456">/etc/rsync.password
 chmod 600 /etc/rsync.password

4. Create a data to be synchronized, to create some data on the client

mkdir -p /data/{www,bbs,blog}
touch /data/www/www.log /data/bbs/bbs.log  /data/blog/blog.log

5. Test rsync is synchronized

rsync  -avzP /data/www/ [email protected]::www  --password-file=/etc/rsync.password
#rsync  -avzP /backup/ rsync://[email protected]/backup/   --password-file=/etc/rsync.password 两种方法
#rsync  -avzP /backup/ rsync://[email protected]/backup/test/   --password-file=/etc/rsync.password #test为服务器上的目录
参数: --delete 无差异同步
          --bwlimit=KB/S  限速
           --exclude=PATTERN       exclude files matching PATTERN
           --exclude-from=FILE     read exclude patterns from FILE
           --include=PATTERN       don’t exclude files matching PATTERN
           --include-from=FILE     read include patterns from FILE

# This step must succeed to the next step

 

6. deploying sersync Service

tar fxz sersync2.5.4_64bit_binary_stable_final.tar.gz -C /usr/local/
cd /usr/local/
mv GNU-Linux-x86 sersync

7.配置sersync

 

cp sersync/confxml.xml  sersync/confxml.xml-bak
vim  sersync/confxml.xml

修改24--28行
 24         <localpath watch="/opt/tongbu">   ##监控的目录
 25             <remote ip="127.0.0.1" name="tongbu1"/> ##备份服务器的ip和模块
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
修改后的内容为:
24         <localpath watch="/data/www">
25             <remote ip="10.1.20.109" name="www"/>
26         </localpath>

修改29--35行,认证部分(rsync密码认证)
29         <rsync>
30             <commonParams params="-artuz"/>   ##rsync命令的参数
31             <auth start="false" users="root" passwordfile="/etc/rsync.pas"/> ##认证用户和密码文件的位置
32             <userDefinedPort start="false" port="874"/><!-- port=874 -->
33             <timeout start="false" time="100"/><!-- timeout=100 -->
34             <ssh start="false"/>
35         </rsync>

修改后的内容如下:
27         <rsync>
28             <commonParams params="-artuz"/>
29             <auth start="true" users="rsync_backup" passwordfile="/etc/rsync.password"/>
30             <userDefinedPort start="false" port="874"/><!-- port=874 -->
31             <timeout start="true" time="100"/><!-- timeout=100 -->
32             <ssh start="false"/>
33         </rsync>

 

 

 

8.开启sersync守护进程同步数据

 

/usr/local/sersync/bin/sersync  -d -r -o /usr/local/sersync/conf/confxml.xml
配置sersync环境变量
echo"PATH=$PATH:/usr/local/sersync/">>/etc/profile
source /etc/profil

启动命令后返回结果如下为正常:**

set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d     run as a daemon
option: -r     rsync all the local files to the remote servers before the sersync work
option: -o     config xml name:  ./confxml.xml
daemon thread num: 10
parse xml config file
XML Parsing error inside file './confxml.xml'.
Error: File not found
At line 0, column 0.

 

同步测试

[root@panwenbin1-sa GNU-Linux-x86]# ./sersync2 -d -r -o ./confxml-www.xml

set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d     run as a daemon
option: -r     rsync all the local files to the remote servers before the sersync work
option: -o     config xml name:  ./confxml-www.xml
daemon thread num: 10
parse xml config file
host ip : localhost    host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is    rsync_backup
passwordfile is     /etc/rsync.password
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12  = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 22 = 12(Thread pool nums) + 10(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data/www && rsync -artuz -R --delete ./  --timeout=100 [email protected]::www --password-file=/etc/rsync.password >/dev/null 2>&1
run the sersync:
watch path is: /data/www

 

 

9.多实例情况

 

 

   1、配置多个confxml.xml文件(比如:www、bbs、blog....等等)
    confxml-bbs.xml  confxml-blog.xml  confxml-www.xml(按照单个实例配置即可)
    2、根据不同的需求同步对应的实例文件
    rsync -avzP /data/www/ [email protected]::www/   --password-file=/etc/rsync.password
    rsync -avzP /data/bbs/ [email protected]::bbs/   --password-file=/etc/rsync.password
    rsync -avzP /data/test/ [email protected]::blog/   --password-file=/etc/rsync.passwor
分别启动即可

 

rsync缺点:

1.大量小文件同步时,比对时间长,有时候rsync进程会停止

2.同步大文件,10G这样的大文件有时也会有问题,会中断。未完整同步之前是隐藏文件,可通过参数续传

 

sersync的参数信息

参数-d:启用守护进程模式 参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍 c参数-n: 指定开启守护线程的数量,默认为10个 参数-o:指定配置文件,默认使用confxml.xml文件 参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块 参数-m:单独启用其他模块,使用 -m socket 开启socket模块 参数-m:单独启用其他模块,使用 -m http 开启http模块 不加-m参数,则默认执行同步程序

Guess you like

Origin www.cnblogs.com/yaokaka/p/11620753.html