您的当前位置:首页 --> CentOS入门
rsync 强化技术通过脚本只同步需要的服务器
浏览次数:1957 关键词 ( 脚本  服务器  技术  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 客户端配置 脚本

1234567891011121314151617181920212223242526#!/bin/bash#客户端192.168.0.22(多台)yum -y installrsynccat>> /etc/rsyncd.conf << EOFuid = rootgid = rootuse chroot = nomax connections = 100timeout = 600pid file=/var/run/rsyucd.pidlock file= /var/run/rsyncd.locklog file= /var/log/rsyncd.log[www]path = /home/wwwignore errorsreadonly = nolist = nohosts allow = 192.168.0.21/255.255.255.0auth users= www1secrets file= /etc/www1.pwdEOFecho"www1:123">> /etc/www1.pwdchmod600 /etc/www1.pwdecho"Welcome to use the rsync services">> /var/rsyncd.motd/usr/bin/rsync--daemonecho"/usr/bin/rsync --daemon">> /etc/rc.local

192.168.0.20 客户端配置 脚本

12345678910111213141516171819202122232425262728#!/bin/bash#客户端192.168.0.20#这些配置文件的意思以前的文章写过具体请看头上的连接yum -y installrsynccat>> /etc/rsyncd.conf << EOFuid = rootgid = rootuse chroot = nomax connections = 100timeout = 600pid file=/var/run/rsyucd.pidlock file= /var/run/rsyncd.locklog file= /var/log/rsyncd.log[www]path = /home/wwwignore errorsreadonly = nolist = nohosts allow = 192.168.0.21/255.255.255.0#这里注释掉,允许谁访问如果多台就空格加192.168.0.22/255.255.255.0auth users= www1secrets file= /etc/www1.pwdEOFecho"www1:123">> /etc/www1.pwdchmod600 /etc/www1.pwdecho"Welcome to use the rsync services">> /var/rsyncd.motd/usr/bin/rsync--daemonecho"/usr/bin/rsync --daemon">> /etc/rc.local


11

下面关键的时刻到了

1service iptables start

2台服务器都运行

12/usr/bin/rsync--address=192.168.0.22 --port=998 --daemon  他自己的端口/usr/bin/rsync--address=192.168.0.21 --port=998 --daemon  更新源端口

180101957.jpg

这里就手动指定端口了


服务端192.168.0.21的配置

12345678910111213141516171819202122#客户端192.168.0.21(单台源)yum install-y rsynctouch/root/rsync.shcat>/root/rsync.sh<<EOF#!/bin/baship=$(cat/root/ip.txt)#这里的ip.txt 是ip地址就是你想把文件更新到那几台服务器上src=/home/www/date=`date+%Y-%m-%d`des1=wwwuser1=www1forhost in$ipdorsync-vzrtopg --delete --port=998 --progress ${src} ${user1}@${host}::${des1} --password-file=/etc/www1.pwd&&#这里的998 是我自己指定的端口 后面有说明if[ $? -eq0 ]thenecho"${host} , $date 更新成功">> /root/rsync.log 2>&1elseecho"${host} , $date 更新失败">> /root/rsync.log 2>&1fidone123echo"123">> /etc/www1.pwdchmod600 /etc/www1.pwdchmod+x /root/rsync.sh

home/www目录权限必须是root.root 才行

下面截图 因为是真实环境 目录屏蔽了

174838688.jpg

这里192.168.0.21 是root@ceshi 主机名字

root@localhost root@abc 分别为192.168.0.22,192.168.0.20主机名字

下面开始测试

175043566.jpg


2台主机那边都是空的

这里再说下ip.txt 里面有什么内容

123[root@ceshi data]# vi /root/ip.txt192.168.0.22192.168.0.20

175459296.jpg

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748让我们运行脚本,见证奇迹sh /root/rsync.sh829 100%  809.57kB/s0:00:00 (xfer#4, to-check=126/132)wh/cq*3877 100%    3.70MB/s0:00:00 (xfer#5, to-check=125/132)wh/nohup.out1306196 100%   41.52MB/s0:00:00 (xfer#6, to-check=124/132)wh/server.ver7287 100%  229.56kB/s0:00:00 (xfer#7, to-check=123/132)wh/server.ver.ver37 100%    1.17kB/s0:00:00 (xfer#8, to-check=122/132)wh/wh*13698828 100%   14.78MB/s0:00:00 (xfer#9, to-check=121/132)whserver/wh*11259096 100%    6.72MB/s0:00:01 (xfer#10, to-check=120/132)wh/wh*21539939 100%   10.81MB/s0:00:01 (xfer#11, to-check=119/132)wh/11233/wh/abc/wh/core/wh/data/wh/data/conf_*1182 100%    1.28kB/s0:00:00 (xfer#12, to-check=111/132)wh/data/server.ver7287 100%    7.91kB/s0:00:00 (xfer#13, to-check=110/132)wh/data/server.ver.ver37 100%    0.04kB/s0:00:00 (xfer#14, to-check=109/132)wh/data/server.ver.zip3063 100%    3.32kB/s0:00:00 (xfer#15, to-check=108/132)wh/data/versions.xml564 100%    0.61kB/s0:00:00 (xfer#16, to-check=107/132)wh/data/gameconfs/wh/data/gameconfs/*6939 100%    7.51kB/s0:00:00 (xfer#17, to-check=103/132)wh/data/gameconfs/*84401 100%   91.07kB/s0:00:00 (xfer#18, to-check=102/132)wh/data/gameconfs/*106431 100%  114.59kB/s0:00:00 (xfer#19, to-check=101/132)wh/data/gameconfs/*251564 100%  269.08kB/s0:00:00 (xfer#20, to-check=100/132)wh/data/gameconfs/*1538 100%    1.65kB/s0:00:00 (xfer#21, to-check=99/132)wh/data/gameconfs/*       30796 100%   32.90kB/s0:00:00 (xfer#22, to-check=98/132)sent 19301219 bytes  received 2275 bytes  4289665.33 bytes/sectotal size is 96052675  speedup is 4.98sending incremental filelistsent 2501 bytes  received 23 bytes  5048.00 bytes/sectotal size is 96052675  speedup is 38055.73yinwe


这里的目录我都屏蔽了,公司隐秘但是技术可以分享

123[root@ceshi data]# cat /root/rsync.log192.168.0.22 , 2013-11-09 更新成功192.168.0.20 , 2013-11-09 更新成功

180743647.jpg

这里大家有没有发现问题,防火墙开着的情况下为啥能访问,其实我添加端口进去了

12345678910111213141516# 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-prohibitedCOMMIT

只允许21能访问998端口

这里实现了一起同步,如果个别同步怎么做,其实很简单,脚本已经出来了,只要修改ip.txt就行了

这里我新建个文件夹

181130742.jpg

1234#注释掉不需要的ip地址[root@ceshi whserver]# vi /root/ip.txt192.168.0.22#192.168.0.20123456[root@ceshi whserver]# sh /root/rsync.shsending incremental filelist./abc/sent 2523 bytes  received 30 bytes  5106.00 bytes/sectotal size is 96052675  speedup is 37623.45


181408988.jpg

这样的效果也达到了,安全也达到了,但是如果中心就做热备份2需要2台机子,怎么办,我们只需要在

123vi/etc/rsyncd.conf#只需要在这个配置文件上修改这段话 加入主机即可hosts allow = 192.168.0.21/255.255.255.255 192.168.0.22/255.255.255.255

防火墙也是一样的

123-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

这样既保证了安全也保证了另外的需求,也可以用监控实时监控只要移动修改删除增加,都会自动同步,但是不建议,如果你只需要更新几台服务器 就用我这种办法。实验已经完成,已经同步测试。需求达到,并且端口可以自定义

12/usr/bin/rsync--address=192.168.0.22--port=998 --daemon  他自己的端口/usr/bin/rsync--address=192.168.0.21 --port=998 --daemon 更新源端口


下载次数:10
下载地址:点击下载
本资源为程序自动采集,如有侵权请联系我们移除 admin#80vps.com 来信请将#替换为@
下一条 centos系统中网络配置相关
上一条 让CentOS32位支持大于4G内存

拥有超过10年的业务管理经验。我们是IDC服务和解决方案方面的专家

有任何疑问请不要犹豫,立即点击联系我们