A服务器作为防篡改源服务器,也就是正常的文件服务器 B服务器作为对外开放,也就是WEB目录服务器 同时将A服务器作为网站后台更新内容的服务器 在B服务器上配置好rsync + inotify 触发式实时同步 图例如下:
yum install rsync 服务器A(防篡改系统rsync配置) vi /etc/rsyncd.conf [test] uid = root #运行rsync的用户 gid = root #运行rsync的用户组 ignore errors #忽略一些无关的I/O错误 path = /app/sinova/python/ #需要备份的文件路径 read only = false #false为关闭,true表示开启。表示只读,不允许上传文件 writeonly = false #不允许下载 list = false #客户请求可以使用模块列表时是否被列出 secres file = /etc/rsync.password #指定一个包含“用户名:密码”格式的文件 #echo "root:123456789" >> /etc/rsync.password - 为 /etc/rsync.password授权为 600(这个文件的权限必须是 600)
#chmod 600 /etc/rsync.password #/usr/bin/rsync --daemon & #echo "/usr/bin/rsync --daemon" >> /etc/rc.local 1、设置 rsync客户端的密码文件,服务器B只需要设置 rsync同步的密码即可,不用设置用户名 # yum install rsync # echo "123456789" > /etc/rsync.password - 将密码文件的权限设置成 600(这个文件的权限必须是600)
# chmod 600 /etc/rsync.password 3、配置 Inotify(在 服务器B上配置) # tar zxf inotify-tools-3.14.tar.gz # cd inotify-tools-3.14 # ./configure && make && make install
- 写一个脚本来实现,当/app/test中文件有变化时,让rsync服务同步数据:
#!/bin/bash DATE=`date +%F_%T` rsyncCmd="rsync -vzrtopg --progress --delete 192.168.101.129::test /app/test --password-file=/etc/rsync.password" Log=/app/logs/notify.log /usr/local/bin/inotifywait -mrq --timefmt '%d/%m/%y %H:%M' --format '%T %w%f' -e modify,delete,create,attrib /app/test | while read file do ${rsyncCmd} echo "${DATE}:Change:${file} was rsynced" >> ${Log} done - 编写监听inotify进程脚本,防止inotify由于某种原因中断无法实时同步
vi /app/bin/inotify_monitor.sh #!/bin/bash #time 2015-09-01 #program: inspect inotify script function check(){ if ! ps -ef|grep "$1"|grep -v "grep" >/dev/null;then nohup /app/bin/inotify.sh & fi } check inotifywait #crontab -e */1 * * * * /app/bin/inotify_monitor.sh &> /dev/null # chmod +x /root/inotify.sh # nohup /app/bin/inotify.sh & #在后台执行
- [url=]服务启动与停止[/url]
- 启动服务器A(防篡改)与服务器B(WEB) rsync服务
#/usr/bin/rsync --daemon # nohup /app/bin/inotify.sh & #crontab -e */1 * * * * /app/bin/inotify_monitor.sh &> /dev/null 关闭监听inotify进程脚本,添加#注释掉计划任务 #crontab -e #*/1 * * * * /app/bin/inotify_monitor.sh &> /dev/null 查找后台进程并kill掉 #kill -9 `ps -ef|grep inotify|grep -v grep|awk '{print $2}'`
向服务器B(WEB服务器)/app/test下目录进行更改、删除文件进行测试;
从上图可以看出,删除、更改文件后目录下无变化。
- [url=]风险评估[/url]
- 设置rsync+inotiy后更新文件需从服务器A(防篡改)进行文件同步;
|