码迷,mamicode.com
首页 > 其他好文 > 详细

rsync

时间:2016-02-28 01:03:39      阅读:238      评论:0      收藏:0      [点我收藏+]

标签:rsync

  rsync(remote sync)是类ulinux系统中的一款远程数据同步工具,使用所谓的“rsync算法”来使本地和远程主机之间的文件达到同步(也可在同一主机内部实现数据同步),这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度很快。

  rsync同步数据时,需要扫描所有文件后对比,进行差量传输,因此不适宜同步海量小文件,因为这个扫描对比的过程将会非常耗时。


1、rsync的特性:

  ①可以镜像保存整个目录树或文件系统;

  ②较高的数据传输效率;

  ③可以借助于ssh实现安全数据传输;

  ④支持匿名传输;


2、rsync命令的工作模式:

  ⑴shell模式,也称作本地模式,同一主机内实现数据同步

    rsync [OPTION...] SRC... [DEST]

  ⑵远程shell模式,可以利用ssh协议承载其远程传输过程;

    rsync [OPTION...] SRC... [USER@]HOST:DEST

    rsync [OPTION...] [USER@]HOST:SRC... [DEST]

  ⑶列表模式,仅列出源中的内容,-nv

  ⑷服务模式,此时rsync工作为守护进程,能接收客户端的数据同步请求;

   ①从服务端拉取数据(pull):

       rsync [OPTION...] [USER@]HOST::SRC... [DEST]   #守护进程模式中服务器的路径以模块名打头

       rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]

   ②向服务端推送数据(push):

       rsync [OPTION...] SRC... [USER@]HOST::DEST

       rsync [OPTION...] rsync://[USER@]HOST[:PORT]/SRC... [DEST]       

  注意:远程shell模式使用单冒号,守护进程模式使用双冒号


3、rsync命令的选项:

    -n:同步测试,不执行真正的同步过程;

    -v:详细输出模式

    -q:静默模式

    -c:checksum,开启校验功能

    -r:递归复制  #源路径若为目录,需要指定该选项(或-a)

    -a:归档,保留文件的原有属性;相当于-rptlgoD

    -p:保留文件的权限

    -t:保留文件的时间戳

    -l:保留符号链接

    -g:保留属组

    -o:保留属主

    -D:保留设备文件

    -e ssh:使用ssh作为传输承载,更安全

    -z:压缩后传输

    --delete:删除那些DST中SRC没有的文件;常用于快速删除大量数据

    --existing:仅仅更新那些已经存在于DST的文件

    --exclude=PATTERN:指定排除不需要传输的文件模式

    --progress:显示进度条

    --stats:显示如何执行压缩和传输

    说明:

      ①rsync命令中,如果源路径是目录,且其末尾有“/”,则会复制目录中的内容,而非目录本身;如果末尾没有“/”,则会同步目录本身及目录中的所有文件;目标路径末尾是否有“/”无关紧要;

      ②文件访问时间等属性、读写等权限、文件内容等有任何变动,都会被认为修改

      ③目标目录下如果文件比源目录还新,则不会同步

[root@node2 ~]# rpm -ql rsync
/etc/xinetd.d/rsync   #rsync若工作于守护进程,则受超级守护进程管理
/usr/bin/rsync
/usr/share/doc/rsync-3.0.6
/usr/share/doc/rsync-3.0.6/COPYING
/usr/share/doc/rsync-3.0.6/NEWS
/usr/share/doc/rsync-3.0.6/OLDNEWS
/usr/share/doc/rsync-3.0.6/README
/usr/share/doc/rsync-3.0.6/support
...
/usr/share/man/man5/rsyncd.conf.5.gz   #可参考该文档创建配置文件[root@node2 ~]# ls /etc/httpd
conf  conf.d  logs  modules  run  ssl
[root@node2 ~]# rsync /etc/httpd/ /tmp/test
skipping directory .   #源路径若为目录,需要指定-r或-a选项,否则不予同步
[root@node2 ~]# rsync -av /etc/httpd/ /tmp/test
sending incremental file list
./
logs -> ../../var/log/httpd
modules -> ../../usr/lib64/httpd/modules
run -> ../../var/run/httpd
conf.d/
conf.d/README
conf.d/php.conf
conf.d/ssl.conf
conf.d/welcome.conf
conf.d/welcome.conf.back
conf/
conf/.htpasswd
conf/httpd.conf
conf/magic
ssl/
ssl/httpd.crt
ssl/httpd.csr
ssl/httpd.key

sent 67856 bytes  received 245 bytes  136202.00 bytes/sec
total size is 66998  speedup is 0.98
[root@node2 ~]# ls /tmp/test
conf  conf.d  logs  modules  run  ssl
[root@node2 ~]# touch /etc/httpd/abc
[root@node2 ~]# rsync -av /etc/httpd/ /tmp/test
sending incremental file list
./
abc   #只会复制变动的部分

sent 481 bytes  received 37 bytes  1036.00 bytes/sec
total size is 66998  speedup is 129.34
[root@node2 ~]# rsync -av /etc/httpd /tmp/test
sending incremental file list
httpd/
httpd/abc
httpd/logs -> ../../var/log/httpd
httpd/modules -> ../../usr/lib64/httpd/modules
httpd/run -> ../../var/run/httpd
httpd/conf.d/
httpd/conf.d/README
...

sent 67924 bytes  received 265 bytes  136378.00 bytes/sec
total size is 66998  speedup is 0.98
[root@node2 ~]# ls /tmp/test
abc  conf  conf.d  httpd  logs  modules  run  ssl
[root@node2 ~]# mkdir /tmp/empty   #创建一个空目录
[root@node2 ~]# rsync -av --delete /tmp/empty/ /tmp/test   #这种用法可用于快速清除大量数据
sending incremental file list
./
deleting ssl/httpd.key
deleting ssl/httpd.csr
deleting ssl/httpd.crt
deleting ssl/
deleting httpd/ssl/httpd.key
...
total size is 0  speedup is 0.00
[root@node2 ~]# ls /tmp/test   #test目录已被清空
[root@node2 ~]# rsync /etc/fstab root@192.168.30.10:/tmp/ceshi
root@192.168.30.10‘s password:
[root@node2 ~]# rsync -a tesla@192.168.30.10:/etc/selinux/config /tmp/test
tesla@192.168.30.10‘s password: 
[root@node2 ~]# ls /tmp/test
config
[root@node1 ~]# ls /tmp/ceshi
fstab

4、rsync的服务模式:

  ⑴为rsync提供配置文件

   /etc/rsyncd.conf   #修改该配置文件后无须重启守护进程,每一次客户端连接都会读取该文件

   配置文件分两段:

     全局配置段:一个

     模块配置段:多个

       [SHARE_NAME]

   配置示例:

     # Global Settings

     uid = nobody   #以什么身份运行守护进程

     gid = nobody

     use chroot = no  #chroot为yes时必须使用root身份

     max connections = 10

     timeout = 300  #该选项可以确保rsync服务器不会永远等待一个崩溃的客户端

     strict modes = yes

     pid file = /var/run/rsyncd.pid

     log file = /var/log/rsyncd.log

     log format = %t %a %m %f %b

     motd file = /etc/rsyncd/rsyncd.motd   #该文件定义客户端连接时所看到的信息,非必须


     # Directory to be synced

     [tools]

     path = /data   #需要做镜像的目录

     comment = some important software  #可给模块指定描述信息

     ignore errors = yes

     read only = no

     write only = no

     hosts allow = 172.16.0.0/16   #可以是ip、网段、可解析的主机名、域名

     hosts deny = *  #表示所有

     list = false(或no)  #当客户请求列出可以使用的模块列表时,该模块是否应该被列出

     uid = root  #进程以什么身份对该模块进行存取;若不指定,则继承全局配置段中的设定

     gid = root

     exclude = DIR1 DIR2 FILE1...  #排除不要同步的文件或目录,多个文件或目录用空格隔开

  ⑵启动服务

     chkconfig rsync on(或编辑/etc/xinetd.d/rsync文件,将disable 改为 no)

     service xinetd start   #默认监听于873/tcp

   有时还需要在防火墙上开放端口:

     iptables -A INPUT -p tcp -m state --state NEW -m tcp --dport 873 -j ACCEPT


5、服务端可启用用户认证的功能

  ①在全局或模块配置段中添加:

     auth users = USERNAME LIST

     secrets file = /etc/rsyncd.passwd

     说明:USERNAME LIST为以逗号分隔的在rsyncd.passwd中存在的用户名的列表

  ②创建密码文件/etc/rsyncd.passwd

     格式:username:password

     这里的用户名和密码与操作系统的用户名密码无关,可任意指定

     此文件不能允许其它用户有访问权限(将其权限设置为600),且密码不能超过8个字符

[root@node2 ~]# vim /etc/rsyncd.conf

# Global Settings
uid = nobody
gid = nobody
use chroot = no
max connections = 10
strict modes = yes
timeout = 300
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log

# Directory to be synced
[tools]
path = /data
ignore errors = yes
read only = no
write only = no
hosts allow = 192.168.30.0/24
hosts deny = *
list = false
auth users = tom, jerry
secrets file = /etc/rsyncd.passwd
[root@node2 ~]# vim /etc/rsyncd.passwd

tom:magedu
jerry:linux
rose:titanic
[root@node2 ~]# chmod 600 /etc/rsyncd.passwd
[root@node2 ~]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
[root@node2 ~]# ss -tnl   #873端口已监听
...
LISTEN      0      64                             :::873                             :::*...
[root@node1 ~]# rsync -a tom@192.168.30.20::tools/test.txt /tmp/ceshi   #从rsync服务器拉取数据
Password: 
[root@node1 ~]# ls /tmp/ceshi
test.txt

  测试中遇到一个问题,客户端向服务器端推送数据不成功,检查发现配置文件的模块配置段中未指定uid,因此直接继承了全局配置段中的uid设定,而全局配置段中的uid = nobody,它对模块目录/data没有写入权限,从而造成客户端无法推送数据

[root@node1 ~]# rsync -a iptables.nat jerry@192.168.30.20::tools/   #提示向服务器推送数据不成功
Password: 
rsync: mkstemp ".iptables.nat.6elLqN" (in tools) failed: Permission denied (13)
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1039) [sender=3.0.6]
[root@node2 ~]# ls -ld /data
drwxrwxr-x+ 2 root root 4096 Nov 24 22:17 /data
[root@node2 ~]# vim /etc/rsyncd.conf
...
[tools]
...
uid = root
gid = root
[root@node2 ~]# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
[root@node1 ~]# rsync -a iptables.nat jerry@192.168.30.20::tools/
Password:
[root@node2 ~]# ls /data
fstab  iptables.nat  test.txt


6、rsync+inotify 实现数据实时同步

  rsync不能实时地去监测、同步数据,虽然它可以通过crontab(周期性任务计划)方式触发同步,但是两次触发动作之间必然会有时间差,这样就可能导致客户端和服务端数据不一致,无法在应用故障时完全的恢复数据。而rsync+inotify就能够很多的解决这个问题

rsync

标签:rsync

原文地址:http://9124573.blog.51cto.com/9114573/1745668

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!