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