您的位置:首页 > 其它

Rsync服务器配置,安装、配置、实例以及原理详解(二)

2014-02-24 23:46 686 查看

一、什么是rsync二、rsync的功能特性三、编译安装rsync软件[b]四、rsync的应用模式[/b]五、rsync 借助ssh通道技术案例六、企业案例一:搭建远程容灾备份系统[b]练习一推送文件方式同步并排除指定的目录和文件[/b][b]练习二测试主机之间数据完全同步(无差异同步)[/b]练习三rsync多目录多模块同步配置防火墙允许rsync服务附加一:rsync服务端部署流程回顾附加二:rsync自我模拟排错思路及实战附加三rsync生产排错整理我的FAQinotify介绍企业应用案例:利用rsync+inotify搭建实时同步系统



===================企业案例一:搭建远程容灾备份系统 =======================

学习思想:重思路、重实践、重总结。练习一:===========推送文件方式同步并排除指定的目录和文件=============[root@c-server python]# ls1.txt ex1.py ex2.py ex3_1.py ex3.py ex4_1.py ex4.py[root@c-server python]#[root@c-server python]# tar zcvf a.tar.gz ./ --exclude=ex4.py # 打包当前目录所有文件,并排除ex4.py././ex4_1.py./ex1.py./ex3.py./a.tar.gz./ex3_1.py./1.txt./ex2.py[root@c-server python]# ls1.txt ex1.py ex3_1.py ex4_1.pya.tar.gz ex2.py ex3.py ex4.py[root@c-server python]# tar ztvf a.tar.gzdrwxr-xr-x root/root 0 2013-01-08 04:57:08 ./-rw-r--r-- root/root 168 2013-01-08 04:57:08 ./ex4_1.py-rw-r--r-- root/root 186 2013-01-06 08:00:12 ./ex1.py-rw-r--r-- root/root 241 2013-01-06 12:13:09 ./ex3.py-rw-r--r-- root/root 0 2013-01-24 17:22:11 ./a.tar.gz-rw-r--r-- root/root 501 2013-01-06 12:33:00 ./ex3_1.py-rw-r--r-- root/root 48 2013-01-03 12:29:33 ./1.txt-rw-r--r-- root/root 300 2013-01-06 08:08:56 ./ex2.py[root@c-server python]#
1、我们现在在服务端操作一下[root@rsync ~]# cd /lianglab/[root@rsync lianglab]# lshtml_2013-01-03.tar.gz test[root@rsync lianglab]#[root@rsync lianglab]# tree|-- html_2013-01-03.tar.gz`-- test `-- html_2013-01-03.tar.gz5 directories, 6 files[root@rsync lianglab]#
测试推送文件方式同步并排除指定的目录和文件。把当前目录排除掉这两个目录。在推送到远端服务器上面。[root@b-server /]# cd /[root@b-server /]# mkdir lianglab[root@b-server /]# cd lianglab/[root@b-server lianglab]# mkdir a b c d[root@b-server lianglab]# touch a/1 b/2 c/3 d/4[root@b-server lianglab]# ll /lianglab/total 16drwxr-xr-x 2 root root 4096 Jan 3 11:08 adrwxr-xr-x 2 root root 4096 Jan 3 11:08 bdrwxr-xr-x 2 root root 4096 Jan 3 11:08 cdrwxr-xr-x 2 root root 4096 Jan 3 11:08 d[root@b-server lianglab]# tree.|-- a| `-- 1|-- b| `-- 2|-- c| `-- 3`-- d `-- 44 directories, 4 files[root@b-server lianglab]#[root@b-server lianglab]# rsync -avz --exclude=a --exclude=b/2 . backup@192.168.1.100::lianglab --password-file=/etc/rsync.passwordsending incremental file list./b/c/c/3d/d/4sent 164 bytes received 61 bytes 450.00 bytes/sectotal size is 0 speedup is 0.00[root@b-server lianglab]#
我们到服务端查看一下,这个操作就是指定目录和文件操作。[root@rsync lianglab]# lsb c d html_2013-01-03.tar.gz test[root@rsync lianglab]#[root@rsync lianglab]# tree.|--|-- c| `-- 3|-- d| `-- 4|-- html_2013-01-03.tar.gz`-- test `-- html_2013-01-03.tar.gz4 directories, 4 files[root@rsync lianglab]#[root@rsync lianglab]# ll /lianglab/ --time-style=full-isototal 20drwxr-xr-x 2 rsync rsync 4096 2013-01-03 11:08:25.000000000 +0800 bdrwxr-xr-x 2 rsync rsync 4096 2013-01-03 11:08:25.000000000 +0800 cdrwxr-xr-x 2 rsync rsync 4096 2013-01-03 11:08:25.000000000 +0800 d-rw-r--r-- 1 rsync rsync 183 2013-01-03 10:12:32.000000000 +0800 html_2013-01-03.tar.gzdrwxr-xr-x 2 rsync root 4096 2013-01-24 17:13:15.000000000 +0800 test[root@rsync lianglab]# date
练习二:===========测试主机之间数据完全同步(无差异同步)=============测试主机之间数据完全同步(无差异同步)要实现这种同步方式就要使用rsync的 --delete参数了,[root@b-server lianglab]# cd /mnt/[root@b-server mnt]# ls[root@b-server mnt]# cd /tmp/[root@b-server tmp]# ls[root@b-server tmp]# touch 1 2 3[root@b-server tmp]# ls1 2 3[root@b-server tmp]# cd /lianglab/[root@b-server lianglab]# mkdir null[root@b-server lianglab]# rsync -avzP --delete null/ /tmp/ #删除本地目录文件sending incremental file list./deleting .font-unix/fs7100deleting .font-unix/deleting .X11-unix/X0deleting .X11-unix/deleting .ICE-unix/deleting 3deleting 2deleting 1deleting .gdm_socketdeleting .X0-locksent 29 bytes received 15 bytes 88.00 bytes/sectotal size is 0 speedup is 0.00[root@b-server lianglab]# lsa b c d null[root@b-server lianglab]# cd null/[root@b-server null]# lltotal 0[root@b-server null]# cd /tmp/[root@b-server tmp]# lltotal 0[root@b-server tmp]#==========================风险操作=====================[root@rsync lianglab]# lsb c d html_2013-01-03.tar.gz test[root@rsync lianglab]# lltotal 20drwxr-xr-x 2 rsync rsync 4096 Jan 3 11:08 bdrwxr-xr-x 2 rsync rsync 4096 Jan 3 11:08 cdrwxr-xr-x 2 rsync rsync 4096 Jan 3 11:08 d-rw-r--r-- 1 rsync rsync 183 Jan 3 10:12 html_2013-01-03.tar.gzdrwxr-xr-x 2 rsync root 4096 Jan 24 17:13 test[root@rsync lianglab]#root@b-server lianglab]# rsync -avz --delete null/ backup@192.168.1.100::lianglab/ --password-file=/etc/rsync.passwordsending incremental file list./deleting test/html_2013-01-03.tar.gzdeleting test/deleting d/4deleting d/deleting c/3deleting c/deleting b/deleting html_2013-01-03.tar.gzsent 29 bytes received 11 bytes 80.00 bytes/sectotal size is 0 speedup is 0.00[root@b-server lianglab]#上面操作一定要谨慎,如果你推送的是空目录,就会给服务器上的目录文件干掉,我们现在到rsync服务器上面查看一下[root@rsync lianglab]# ll #服务端lianglab目录的文件都没有了。total 0[root@rsync lianglab]#我们现在做拉取操作,现在服务器lianglab目录下面是空的,我们把他拉取到本地来。我们是以tmp目录为例,如果把tmp目录换为根目录,就会把系统干掉了。这个操作要谨慎。[root@b-server lianglab]# cd /tmp/[root@b-server tmp]# ls[root@b-server tmp]# touch 1 2 3 4 5 6 7[root@b-server tmp]# ls1 2 3 4 5 6 7[root@b-server tmp]# rsync -avz --delete backup@192.168.1.100::lianglab/ /tmp/ --password-file=/etc/rsync.passwordreceiving incremental file listdeleting 7deleting 6deleting 5deleting 4deleting 3deleting 2deleting 1./sent 63 bytes received 112 bytes 116.67 bytes/sectotal size is 0 speedup is 0.00[root@b-server tmp]#[root@b-server tmp]# lltotal 0[root@b-server tmp]#结论: 执行--delete参数从rsync服务端往rsync客户端拉取数据时,一定要小心,最好不用,它比从rsync客户端带--delete参数往rsync服务端推送危险得多。客户端带--delete参数往服务端推送仅删除服务端模块下的数据,而前者有能力删除rsync客户端本地的所有数据,包括根下的所有目录。rsync无差异同步的生产场景应用: 一般是有需要两台服务器之间,必须要求数据一致,且实时性又不是很高的情况下,如两台负载均衡下面web服务器之间的同步,或者高可用双机配置之间的同步如(MySQL配置文件同步)等等。rsync无差异同步非常危险的,因此,在生产环境中用的比较少,这个参数很危险。请慎用啊。==========================风险操作=====================练习三=====================rsync多目录多模块同步====================rsync多目录多模块同步。配置多个模块路径的简单写法:[root@rsync lianglab2]# cat /etc/rsyncd.conf#rsync_config______start#created by lianglab 21:55 2013-6-13#QQ 865362365# http://hi.baidu.com/zll56281688 http://zll56281688.blog.51cto.com## rsyncd.conf start ##uid = rsyncgid = rsyncuse chroot = nomax connections = 100strict modes = yestimout = 300pid file = /var/run/rsyncd.pidlock file = /var/run/rsync.locklog file = /var/log/rsyncd.logcomment = lianglab fileignore errorsread only = nowrite only = nohost allow = *host deny = 192.168.1.111list = falseauth users =backupsecrets file = /etc/rsync.password[lianglab]path = /lianglab[lianglab2]path = /lianglab2[root@rsync lianglab2]#lianglab和lianglab2 就是对应的模块,我们是统一一个账号,下面有不同的模块.[root@rsync lianglab]# pkill rsync[root@rsync /]# mkdir lianglab2[root@rsync /]#[root@rsync /]# rsync --daemon[root@rsync /]# netstat -lnt | grep 873tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTENtcp 0 0 :::873 :::* LISTEN[root@rsync /]# cat /etc/services | grep rsync | head -2rsync 873/tcp # rsyncrsync 873/udp # rsync[root@rsync /]#我们现在到客户端测试一下.[root@b-server tmp]# rsync -avz /etc/services backup@192.168.1.100::lianglab/ --password-file=/etc/rsync.passwordsending incremental file listservicessent 99063 bytes received 27 bytes 198180.00 bytes/sectotal size is 362031 speedup is 3.65[root@b-server tmp]# rsync -avz /etc/services backup@192.168.1.100::lianglab2/ --password-file=/etc/rsync.passwordsending incremental file listservicesrsync: mkstemp ".services.nUyHxz" (in lianglab2) failed: Permission denied (13) #有一个报错,很明显是权限问题.sent 99063 bytes received 27 bytes 198180.00 bytes/sectotal size is 362031 speedup is 3.65rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6][root@b-server tmp]#[root@rsync /]# cd /lianglab[root@rsync lianglab]# lsservices[root@rsync lianglab]#[root@rsync /]# cd /lianglabdrwxr-xr-x 2 rsync rsync 4096 Jan 25 06:54 lianglabdrwxr-xr-x 2 root root 4096 Jan 25 06:50 lianglab2[root@rsync /]# chown -R rsync.rsync lianglab2[root@b-server tmp]# rsync -avz /etc/services backup@192.168.1.100::lianglab2/ --password-file=/etc/rsync.passwordsending incremental file listservicessent 99063 bytes received 27 bytes 198180.00 bytes/sectotal size is 362031 speedup is 3.65[root@b-server tmp]#

=================rsync多目录多模块同步=========================================配置防火墙允许rsync服务。=====================rsync服务允许的端口 873/tcp端口[root@rsync lianglab]# netstat -lnt | grep 873tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTENtcp 0 0 :::873 :::* LISTEN[root@rsync lianglab]# cat /etc/services | grep rsync | head -2rsync 873/tcp # rsyncrsync 873/udp # rsync[root@rsync lianglab]#rsync防火墙配置在生产环境下,可以配置如下三行允许rsync服务,加入到/etc/syscofig/iptables-A INPUT -s 192.168.1.1/24 -p tcp -m tcp --dport 873 -j ACCEPT#----允许固定办公网发布程序传输同步-A INPUT -是192.168.1.0/255.255.255.0 -p tcp -m tcp --dport 873 -j ACCEPT#---允许IDC内网网段间连接程序传输同步我们现在就停止掉防火墙=====================配置防火墙允许rsync服务。=====================

--------------------------------------附加:rsync服务端部署流程回顾:---------------------------------一、rsync服务端配置流程1、创建rsync配置文件/etc/rsyncd.conf2、创建同步的本地目录/lianglab并根据需要授权,rsync服务的用户读写/lianglab目录和/etc/rsync.password为配置文件中 path = /lianglab/参数的设置3、账号及密码文件配置echo "backup:lianglab" > /etc/rsync.passwordchmod 600 /etc/rsync.password提示:1>/etc/rsync.password 为配置文件中 secrets file = /etc/rsync.password 参数的配置2>账号backup 为/etc/rsyncd.password 为配置文件中auth users = backup参数的配置4、启动rsync服务rsync --daemonecho "/usr/bin/rsync --daemon">>/etc/rc.localcat /etc/rc.loacl | grep daemonps -ef | grep rsync###重启pkill rsyncrsync --daemonps -ef | grep rsync二、rsync 客户端配置流程echo "lianglab" >/etc/rsync.passwordchmod 600 /etc/rsync.password三、rsync客户端命令操作细节。推送、抓取方法:推送操作。rsync -vzrtopgP /tmp backup@192.168.1.100::lianglab/ --password-file=/etc/rsync.passwordrsync -avz -P --delete /tmp backup@192.168.1.100::lianglab/ --password-file=/etc/rsync.password抓取操作。rsync -zvz backup@192.168.1.100::lianglab/ /tmp --password-file=/etc/rsync.password--------------------------------------附加:rsync服务端部署流程回顾:-----------------------------------------------------------------------附加:rsync自我模拟排错思路及实战:---------------------------------------rsync服务端排错思路1、查看rsync服务配置文件路径是否正确、正确的默认路径为/etc/rsyncd.conf2、查看配置文件里host allow。 host deny,允许的ip网段是否允许客户端访问的ip网段。3、查看配置文件中path参数的路径是否存在,权限是否正确(正常应为配置文件中的UID参数对应的属主和组)4、查看rsync服务是否启动,查看命令为: ps -f | grep rsync 端口是否存在 netstat -lnt | grep 8735、查看iptables防火墙和selinux是否开启允许rsync服务通过,也可考虑关闭。6、查看服务端rsync配置的密码文件是否为600权限,密码文件格式是否正确。7、如果是推送数据,要查看下,配置rsyncd.conf文件中用户是否对模块下目录有可读写的权限。rsync客户端排错思路1、查看客户端rsync配置的密码文件是否600的权限,密码文件格式是否正确,注意:仅需要有密码,并且和服务端的密码一致。2、用telnet连接rsync服务器ip地址873端口,查看服务是否启动(可测试服务端防火墙是否阻挡)。telnet 192.168.1.1 8733、客户端执行命令时 rsync -avzP rsync_backup@192.168.1.1::lianglab/test /test// --password-file=/etc/rsync.password此命令细节:尤其192.168.1.1::lianglab/test/处的双冒号及随其后的lianglab为模块名称--------------------------------------附加:rsync自我模拟排错思路及实战:---------------------------------------

--------------------------------rsync生产排错整理我的FAQ--------------------------------练习中----自我模拟排除,修改配置文件时, 不按正确方法走。看看有什么错误。[root@b-server tmp]# rsync -avz /etc/services backup@192.168.1.100::lianglab2/ --password-file=/etc/rsync.password@ERROR: auth failed on module lianglab2rsync error: error starting client-server protocol (code 5) at main.c(1530) [sender=3.0.6] 解决:这是因为密码设置错误,无法登陆,检查下两端的/etc/rsync.password设置的密码是否一致。2、password file must not be other-accessiblecontinuing without password filepassword: 解决:/etc/rsync.password 文件权限属性不对,应该为600,执行chmod 600 /etc/rsync.password进行设置。3、@ERROR:chroot failed rsync:connection unexpectedly closed (75 bytes read so for) rsync:error:error in rsync protocol data stream (code 12) at io.c(150) 解决:通常是服务器端的rsyncd.conf中的path参数所设置的备份目录不存在导致,要先用mkdir 建立好目录,在授权 chmod -R 777 /lianglab问题错误一:no route to host问题对方没开机、防火墙阻挡、通过的网络上有防火墙阻挡,都有可能。解决方法:关闭防火墙,或者把防火墙的tcp udp 的873端口打开,允许rsync通过,他们可以使用telnet IP 873检查。

--------------------------------rsync生产排错整理我的FAQ--------------------------------附录一、rsync命令常用参数常用参数说明:-v --verbose 详细模式输出,传输时的进度等信息。-z --compress 对备份的文件在传输时进行压缩处理。-r --recusive 对子目录以递归模式处理-t --time 保持稳健时间信息-o --owner 保持稳健属主信息-p --perms 保持文件权限-g --group 保持文件属组信息--progress 显示备份过程-a --archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rtopsD1-e --rsh=COMMAND 使用的信道协议,指定替代rsh的shell程序,例如:ssh--exclude=PATTERN 指定排除不需要传输的文件模式-D --devices 保持设备文件信息-l ----links 保留软连接-u --update

==============================================参考文献:老男孩51CTO视频教程http://edu.51cto.com/course/course_id-875.html
南非蚂蚁《高性能Linux服务器构建实战》一书
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息