配置rsync + inotify 实现数据实时同步 |
发布时间: 2012/9/15 18:10:47 |
要求:两台Web服务器实现数据同步(我这里使用的是Centos 6.2-x64) 服务器一:172.16.11.126 服务器二:172.16.11.127 一、配置ssh备份源172.16.11.126(这里推荐使用专用的普通用户,注意相应的权限问题,如遇特殊情况使用root用户也可以,即不用考虑权限问题了。 ) 1、新建备份用户rget rput 分别用来上传下载 [root@localhost ~]# useradd rget [root@localhost ~]# useradd rput [root@localhost ~]# passwd rget [root@localhost ~]# passwd rput 2、确认sshd服务正常启动,且允许用户rget rput访问 [root@localhost ~]# vim /etc/ssh/sshd_config 1. .......... 2. 3. UserDNS no 4. 5. AllowUsers rget rput 6. [root@localhost ~]# service sshd restart [root@localhost ~]# chown -R rput:rput/var/www/html [root@localhost ~]# setfacl -R -m user:daemon:rwx /var/www/html /upload [root@localhost ~]# getgacl /var/www/html/upload [root@localhost ~]# setfacl -m default:user:daemon:rwx /var/www/html/upload/ [root@localhost ~]# getfacl /var/www/html/upload | grep default 二、配置rsync源服务器。 [root@localhost ~]# yum install rsync [root@localhost ~]# /etc/init.d/httpd restart [root@localhost ~]# cd /var/www/html/ [root@localhost html]# /etc/init.d/sshd restart [root@localhost html]# vim /etc/rsyncd.conf 1. uid = nobody 2. gid = nobody 3. use chroot = yes //禁锢在源目录 4. address = 172.16.11.126 //监听地址 5. port 873 //监听端口 6. log file = /var/log/rsyncd.log //日志文件位置 7. pid file = /var/run/rsyncd.pid //存放进程ID的文件位置 8. hosts allow = 172.16.11.0/24 //允许访问的客户机地址 9. [wwwroot] //共享模块名称 10. path = /var/www/html //源目录的世纪路径 11. comment = Document Root of www1.dong.com 12. read only = yes //只读 13. dont compress = *.gz *.bz2 *.tgz *.zip *.rar *.z //同步时不再压缩的文件类型 14. auth users = backuper //授权账户 15. secrets file = /etc/rsyncd_users.db //存放账户信息的数据文件 [root@localhost html]# vim /etc/rsyncd_users.db 1. backuper:pwd123 [root@localhost html]# chmod 600 /etc/rsyncd_users.db [root@localhost html]# rsync –daemon //启动rsync服务 [root@localhost html]# netstat -anpt | grep rsync tcp 0 0 192.168.1.1:873 # 如需关闭rsync服务时 kill $(cat /var/run/rsyncd.pid) [root@localhost html]# vim /etc/xinetd.d/rsync 1. # default: off 2. # description: The rsync server is a good addition to an ftp server, a 3. s it \ 4. # allows crc checksumming etc. 5. service rsync 6. { 7. disable = no //将原有的yes改为no 8. socket_type = stream 9. wait = no 10. user = root 11. server = /usr/bin/rsync 12. server_args = --daemon //确认有—daemon服务选项 13. log_on_failure += USERID 14. } [root@localhost html]# yum -y install xinetd [root@localhost html]# /etc/init.d/xinetd start 三、使用rsync备份工具 SSH备份源 [root@localhost ~]# rsync -avz rget@172.16.11.126:/var/www/html/ /opt/ rsync备份源 [root@localhost ~]# rsync -avz backuper@172.16.11.126::wwwroot /root 或者 [root@localhost ~]# rsync -azv rsync://backuper@172.16.11.126/wwwroot /root 四、配置rsync + inotify实时同步 1、调整inotify内核参数 [root@localhost ~]# cat /proc/sys/fs/inotify/max_queued_events 16384 [root@localhost ~]# cat /proc/sys/fs/inotify/max_user_instances 1024 [root@localhost ~]# cat /proc/sys/fs/inotify/max_user_watches 1048576 [root@localhost ~]# vim /etc/sysctl.conf 1. kernel.shmall = 268435456 2. fs.inotify.max_queued_events = 16384 3. fs.inotify.max_user_instances =1024 4. fs.inotify.max_user_watches = 1048576 [root@localhost ~]# sysctl -p 2、安装inofity-tools工具 (这里我已经下载好了inotify-tools-3.14.tar.gz) [root@localhost ~]# tar -zxvf inotify-tools-3.14.tar.gz [root@localhost ~]# cd inotify-tools-3.14 [root@localhost inotify-tools-3.14]# ./configure [root@localhost inotify-tools-3.14]# make [root@localhost inotify-tools-3.14]# make install [root@localhost inotify-tools-3.14]# inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/ & 3、编写触发式同步脚本 [root@localhost inotify-tools-3.14]# vim /opt/inotifity_rsync.sh 1. #!/bin/bash 2. INOTIFY_CMD="/usr/local/bin/inotifywait -mrq -e modify,create,attrib,move,delete /var/www/html/" 3. RSYNC_CMD="/usr/bin/rsync -azH --delete /var/www/html/ /nfs/" 4. $INOTIFY_CMD | while read DIRECTORY EVENT FILE 5. do 6. if [ $(pgrep rsync | wc -l) -le 0 ]; then 7. $RSYNC_CMD 8. fi 9. done [root@localhost inotify-tools-3.14]# chmod +x /opt/inotifity_rsync.sh [root@localhost inotify-tools-3.14]# echo '/opt/inotifity_rsync.sh' >> /etc/rc.local 注意这是在备份源上面的操作 [root@localhost ~]# vim /etc/exports (172.16.11.126) 1. /var/www/html *(rw,no_root_squash) [root@localhost ~]# service nfs restart 把共享的目录挂在到本地 [root@localhost ~]# mount 172.16.11.126:/var/www/html/ /nfs/ 备份源与发起端生成密钥对 (连接时不需要进入交互式) [root@localhost ~]# ssh-keygen -t rsa [root@localhost ~]# ssh-copy-id -i .ssh/id_rsa.pub 172.16.11.127 本文出自:亿恩科技【www.enkj.com】 |