要求:兩臺Web服務器實現數據同步(我這裏使用的是Centos 6.2-x64) html
服務器一:172.16.11.126 vim
服務器二:172.16.11.127 bash
1、配置ssh備份源172.16.11.126(這裏推薦使用專用的普通用戶,注意相應的權限問題,如遇特殊狀況使用root用戶也能夠,即不用考慮權限問題了。 ) 服務器
1、新建備份用戶rget rput 分別用來上傳下載 ssh
[root@localhost ~]# useradd rget socket
[root@localhost ~]# useradd rput tcp
[root@localhost ~]# passwd rget 工具
[root@localhost ~]# passwd rput ui
二、確認sshd服務正常啓動,且容許用戶rget rput訪問 spa
[root@localhost ~]# vim /etc/ssh/sshd_config
[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
2、配置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
- 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 //存放帳戶信息的數據文件
[root@localhost html]# vim /etc/rsyncd_users.db
- 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 0.0.0.0:* LISTEN 5458/rsync
# 如需關閉rsync服務時 kill $(cat /var/run/rsyncd.pid)
[root@localhost 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
- }
[root@localhost html]# yum -y install xinetd
[root@localhost html]# /etc/init.d/xinetd start
3、使用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
4、配置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
- kernel.shmall = 268435456
- fs.inotify.max_queued_events = 16384
- fs.inotify.max_user_instances =1024
- fs.inotify.max_user_watches = 1048576
[root@localhost ~]# sysctl -p
二、安裝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/ &
三、編寫觸發式同步腳本
[root@localhost 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
[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)
- /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