实时同步服务

脚本方式实现
inotify + rsync实现

#!/bin/bash
inotifywait   -rmq  --timefmt "%F %T"    --format "%T %e %w %f"     -e "create,move,close_write,delete"  /data/ |while read  data_info   
do
rsync -az  /data/  --delete [email protected]::backup --password-file=/etc/rsync.password >/dev/dull  2>&1
sleep  3
done

使用inotifywait -rmq --timefmt “%F %T” --format “%T %e %w %f” -e “create,move,close_write,delete” /data/ 来检测输出的事件,使用read读取后给while语句,while语句将这个读取出来的东西当做条件,进行循环。总体来说就是一个触发机制,而触发的条件就是当目录有事件变化时
read 是while语句中的某个用法,是属于while语句的
sersync软件实现
建议前提:实现先安装rsync,实验是否可以免密传输,不要因为这个原因导致软件出现错误。
sersync这个软件默认是无差异同步传输

历程一 :上传二进制包并解压(前提是安装了lrzsz,直接可以将软件包拖进Linux)

[root@localhost ~]# rz -E
rz waiting to receive.
[root@localhost ~]# ll
total 1948
-rw-------. 1 root root    1326 Jan  7 08:12 anaconda-ks.cfg
-rwxr-xr-x. 1 root root    1111 Jan  7 06:26 centos.sh
-rwxr-xr-x. 1 root root     651 Jan  7 11:58 modifynetwork.sh
-rw-r--r--  1 root root 1981010 Jan 13 22:07 sersync-master.zip

解压

[root@localhost ~]# unzip sersync-master.zip 
Archive:  sersync-master.zip
e6e4cda2583a73a5581d7015255838b5e68673c6
   creating: sersync-master/
  inflating: sersync-master/.gitattributes  
  inflating: sersync-master/.gitignore  
  inflating: sersync-master/README.md  
  inflating: sersync-master/inotify-tools-3.14.tar.gz  
 extracting: sersync-master/rsync-3.1.1.tar.gz  
  inflating: sersync-master/sersync2.5.4_64bit_binary_stable_final.tar.gz 

一直解压到这个目录下(unzip 或者 tar )

[root@localhost GNU-Linux-x86]# pwd
/root/sersync-master/GNU-Linux-x86
[root@localhost GNU-Linux-x86]# ll
total 1772
-rwxr-xr-x 1 root root    2214 Oct 25  2011 confxml.xml
-rwxr-xr-x 1 root root 1810128 Oct 26  2011 sersync2

将该软件的执行文件放到/usr/local下的子目录下(这里我是新建的子目录)

[root@localhost GNU-Linux-x86]# mkdir  /usr/local/server/sersync
mkdir: cannot create directory ‘/usr/local/server/sersync’: No such file or directory
[root@localhost GNU-Linux-x86]# mkdir  /usr/local/server/sersync -p
[root@localhost GNU-Linux-x86]# 
[root@localhost GNU-Linux-x86]# mv ./* /usr/local/server/sersync/

在/etc/profile文件下添加该环境变量,作用是让提高工作效率,执行起来可以sersync2命令

export PATH="$PATH:/usr/local/server/sersync/"

注意:修改环境变量不可以指定到具体的文件(二进制脚本文件),指定到文件的目录就够了。 还有一点就是需要必须放到/usr/local/下面的子目录中

=============================================
验证环境变量是否成功,查看命令帮助

[root@localhost GNU-Linux-x86]# sersync2 -h
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
_______________________________________________________
参数-d:启用守护进程模式
参数-r:在监控前,将监控目录与远程主机用rsync命令推送一遍
c参数-n: 指定开启守护线程的数量,默认为10个
参数-o:指定配置文件,默认使用confxml.xml文件
参数-m:单独启用其他模块,使用 -m refreshCDN 开启刷新CDN模块
参数-m:单独启用其他模块,使用 -m socket 开启socket模块
参数-m:单独启用其他模块,使用 -m http 开启http模块
不加-m参数,则默认执行同步程序
___________________________________________________

编辑配置文件:

[root@localhost sersync]# pwd
/usr/local/server/sersync
24         <localpath watch="/opt/tongbu">
 25             <remote ip="127.0.0.1" name="tongbu1"/>
 26             <!--<remote ip="192.168.8.39" name="tongbu"/>-->
 27             <!--<remote ip="192.168.8.40" name="tongbu"/>-->
 28         </localpath>
 29         <rsync>
 30             <commonParams params="-artuz"/>
 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>
 36         <failLog path="/tmp/rsync_fail_log.sh" timeToExecute="60"/><!--default every 60mins exe    cute once-->

在这里插入图片描述
在这里插入图片描述
最后启动服务

[root@localhost sersync]# sersync2 
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
daemon thread num: 10
parse xml config file
host ip : localhost	host port: 8008
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
run the sersync: 
watch path is: /data

说明:停止sersync2时只能kill杀死进程的方式

多说一点:想明白是在哪个服务器上部署sersync软件
部署在备份服务器上------让web服务器的数据存储到存储服务器-----在存储服务器上部署sersync软件----推送到备份服务器

发布了154 篇原创文章 · 获赞 14 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_42506599/article/details/103975039