rsync 強化技術(手動修改端口開啓防火牆的情況下)並且通過腳本只同步需要的服務器
客戶端:192.168.0.22 ,192.168.0.20
服務端:192.168.0.21(源)用來同步其他服務器
我前面寫過
http://chenhao6.blog.51cto.com/6228054/1298375inotify+rsync+mutt+msmtp 實現linux文件或者目錄自動更新並且實現發郵件給管理員
但是有些功能不能實現,如果讓你只更新格外的幾臺 不全部更新,你怎麼辦。
這裏給出方案
首先192.168.0.22 客戶端配置 腳本
#!/bin/bash #客戶端192.168.0.22(多臺) yum -y install rsync cat >> /etc/rsyncd.conf << EOF uid = root gid = root use chroot = no max connections = 100 timeout = 600 pid file=/var/run/rsyucd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log [www] path = /home/www ignore errors read only = no list = no hosts allow = 192.168.0.21/255.255.255.0 auth users = www1 secrets file = /etc/www1.pwd EOF echo "www1:123" >> /etc/www1.pwd chmod 600 /etc/www1.pwd echo "Welcome to use the rsync services" >> /var/rsyncd.motd /usr/bin/rsync --daemon echo "/usr/bin/rsync --daemon" >> /etc/rc.local
192.168.0.20 客戶端配置 腳本
#!/bin/bash #客戶端192.168.0.20 #這些配置文件的意思以前的文章寫過具體請看頭上的連接 yum -y install rsync cat >> /etc/rsyncd.conf << EOF uid = root gid = root use chroot = no max connections = 100 timeout = 600 pid file=/var/run/rsyucd.pid lock file = /var/run/rsyncd.lock log file = /var/log/rsyncd.log [www] path = /home/www ignore errors read only = no list = no hosts allow = 192.168.0.21/255.255.255.0 #這裏註釋掉,允許誰訪問如果多臺就空格加192.168.0.22/255.255.255.0 auth users = www1 secrets file = /etc/www1.pwd EOF echo "www1:123" >> /etc/www1.pwd chmod 600 /etc/www1.pwd echo "Welcome to use the rsync services" >> /var/rsyncd.motd /usr/bin/rsync --daemon echo "/usr/bin/rsync --daemon" >> /etc/rc.local
1
下面關鍵的時刻到了
service iptables start
2臺服務器都運行
/usr/bin/rsync --address=192.168.0.22 --port=998 --daemon 他自己的端口 /usr/bin/rsync --address=192.168.0.21 --port=998 --daemon 更新源端口
這裏就手動指定端口了
服務端192.168.0.21的配置
#客戶端192.168.0.21(單臺源) yum install -y rsync touch /root/rsync.sh cat>/root/rsync.sh<<EOF #!/bin/bash ip=$(cat /root/ip.txt) #這裏的ip.txt 是ip地址就是你想把文件更新到那幾臺服務器上 src=/home/www/ date=`date +%Y-%m-%d` des1=www user1=www1 for host in $ip do rsync -vzrtopg --delete --port=998 --progress ${src} ${user1}@${host}::${des1} --password-file=/etc/www1.pwd && #這裏的998 是我自己指定的端口 後面有說明 if [ $? -eq 0 ] then echo "${host} , $date 更新成功" >> /root/rsync.log 2>&1 else echo "${host} , $date 更新失敗" >> /root/rsync.log 2>&1 fi done
echo "123" >> /etc/www1.pwd chmod 600 /etc/www1.pwd chmod +x /root/rsync.sh
home/www目錄權限必須是root.root 才行
下面截圖 因爲是真實環境 目錄屏蔽了
這裏192.168.0.21 是root@ceshi 主機名字
root@localhost root@abc 分別爲192.168.0.22,192.168.0.20主機名字
下面開始測試
2臺主機那邊都是空的
這裏再說下ip.txt 裏面有什麼內容
[root@ceshi data]# vi /root/ip.txt 192.168.0.22 192.168.0.20
讓我們運行腳本,見證奇蹟 sh /root/rsync.sh 829 100% 809.57kB/s 0:00:00 (xfer#4, to-check=126/132) wh/cq* 3877 100% 3.70MB/s 0:00:00 (xfer#5, to-check=125/132) wh/nohup.out 1306196 100% 41.52MB/s 0:00:00 (xfer#6, to-check=124/132) wh/server.ver 7287 100% 229.56kB/s 0:00:00 (xfer#7, to-check=123/132) wh/server.ver.ver 37 100% 1.17kB/s 0:00:00 (xfer#8, to-check=122/132) wh/wh* 13698828 100% 14.78MB/s 0:00:00 (xfer#9, to-check=121/132) whserver/wh* 11259096 100% 6.72MB/s 0:00:01 (xfer#10, to-check=120/132) wh/wh* 21539939 100% 10.81MB/s 0:00:01 (xfer#11, to-check=119/132) wh/11233/ wh/abc/ wh/core/ wh/data/ wh/data/conf_* 1182 100% 1.28kB/s 0:00:00 (xfer#12, to-check=111/132) wh/data/server.ver 7287 100% 7.91kB/s 0:00:00 (xfer#13, to-check=110/132) wh/data/server.ver.ver 37 100% 0.04kB/s 0:00:00 (xfer#14, to-check=109/132) wh/data/server.ver.zip 3063 100% 3.32kB/s 0:00:00 (xfer#15, to-check=108/132) wh/data/versions.xml 564 100% 0.61kB/s 0:00:00 (xfer#16, to-check=107/132) wh/data/gameconfs/ wh/data/gameconfs/* 6939 100% 7.51kB/s 0:00:00 (xfer#17, to-check=103/132) wh/data/gameconfs/* 84401 100% 91.07kB/s 0:00:00 (xfer#18, to-check=102/132) wh/data/gameconfs/* 106431 100% 114.59kB/s 0:00:00 (xfer#19, to-check=101/132) wh/data/gameconfs/* 251564 100% 269.08kB/s 0:00:00 (xfer#20, to-check=100/132) wh/data/gameconfs/* 1538 100% 1.65kB/s 0:00:00 (xfer#21, to-check=99/132) wh/data/gameconfs/* 30796 100% 32.90kB/s 0:00:00 (xfer#22, to-check=98/132) sent 19301219 bytes received 2275 bytes 4289665.33 bytes/sec total size is 96052675 speedup is 4.98 sending incremental file list sent 2501 bytes received 23 bytes 5048.00 bytes/sec total size is 96052675 speedup is 38055.73yinwe
這裏的目錄我都屏蔽了,公司隱祕但是技術可以分享
[root@ceshi data]# cat /root/rsync.log 192.168.0.22 , 2013-11-09 更新成功 192.168.0.20 , 2013-11-09 更新成功
這裏大家有沒有發現問題,防火牆開着的情況下爲啥能訪問,其實我添加端口進去了
# Firewall configuration written by system-config-firewall # Manual customization of this file is not recommended. *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT -A INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -I INPUT -p tcp --dport 998 -j DROP -I INPUT -s 192.168.0.21/32 -p tcp --dport 998 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT
只允許21能訪問998端口
這裏實現了一起同步,如果個別同步怎麼做,其實很簡單,腳本已經出來了,只要修改ip.txt就行了
這裏我新建個文件夾
#註釋掉不需要的ip地址 [root@ceshi whserver]# vi /root/ip.txt 192.168.0.22 #192.168.0.20
[root@ceshi whserver]# sh /root/rsync.sh sending incremental file list ./ abc/ sent 2523 bytes received 30 bytes 5106.00 bytes/sec total size is 96052675 speedup is 37623.45
這樣的效果也達到了,安全也達到了,但是如果中心就做熱備份2需要2臺機子,怎麼辦,我們只需要在
vi /etc/rsyncd.conf #只需要在這個配置文件上修改這段話 加入主機即可 hosts allow = 192.168.0.21/255.255.255.255 192.168.0.22/255.255.255.255
防火牆也是一樣的
-I INPUT -p tcp --dport 998 -j DROP -I INPUT -s 192.168.0.21/32 -p tcp --dport 998 -j ACCEPT -I INPUT -s 192.168.0.20/32 -p tcp --dport 998 -j ACCEPT
這樣既保證了安全也保證了另外的需求,也可以用監控實時監控只要移動修改刪除增加,都會自動同步,但是不建議,如果你只需要更新幾臺服務器 就用我這種辦法。實驗已經完成,已經同步測試。需求達到,並且端口可以自定義
/usr/bin/rsync --address=192.168.0.22--port=998 --daemon 他自己的端口 /usr/bin/rsync --address=192.168.0.21 --port=998 --daemon 更新源端口