Remote Synchronize ,是Linux默认安装的一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。
Rsync可以通过rsh或ssh使用,也能以daemon模式去运行。
在以daemon方式运行时Rsync server会打开一个873 端口,等待客户端去连接。连接时,Rsync server会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,以后则就只需进行增量备份。
1)支持拷贝特殊文件如链接文件,设备等
2)可以有排除指定文件或者目录同步的功能,相当于打包命令tar的排除功能。
3)可以做到保持原文件或者目录的权限,时间,软硬链接,属组,主等所有属性均不改变
4)可以实现增量备份,既只同步发生变化的数据
5)可以使用rcp,rsh,ssh等方式来配合传输文件
6)可以通过socket传输文件和数据
7)支持匿名的认证模式传输
Linux默认安装了的,没有的话就使用yum命令安装。
# rpm -qa | grep rsync
rsync-3.0.6-12.el6.x86_64
# rsync --version
rsync version 3.0.6 protocol version 30
Copyright (C) 1996-2009 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, ACLs, xattrs, iconv, symtimes
rsync comes with ABSOLUTELY NO WARRANTY. This is free software, and you
are welcome to redistribute it under certain conditions. See the GNU
General Public Licence for details.
配置文件是rsyncd.conf,路径在/etc/目录下,没有的话,vim建一个。
启动rsync会有问题,要掉下文中的#备注。
# vim /etc/rsyncd.conf
# /etc/rsyncd:configuration file for rsync daemon mode.
# see rsyncd.conf man page for more options.
# configuration example:
port = 873 #监听端口,默认是873
address = 192.168.10.0/24 #服务器监听的地址,可省略
uid = root #守护进程所属的uid,默认是nobody,为了没有文件或目录的权限问题,设置为root
gid = root #守护进程的gid
fake super = yes
use chroot = no #chroot,改变程序执行时所参考的根目录位置,在传输文件之前,
#服务器守护程序在将chroot 到文件系统中的目录中
max connections = 3000 #客户端最大连接数
log file = /var/log/rsyncd.log #指定rsync守护进程的日志文件,而不是将日志发给syslog
pid file = /var/run/rsyncd.pid #指定进程的pid文件
lock file = /var/run/rsyncd.lock #指定进程的锁文件存放路径
log format = %t %a %m %f %b #日志格式
incoming chmod = Du=rwx,Dog=rx,Fu=rw,Fgo=r #客户端在服务器上的访问权限
syslog facility = local3 #指定rsync发送日志消息给syslog时的消息级别
timeout = 1200 #超时时间
list = no #是否可查看服务器所提供的同步目录
&include /etc/rsyncd.d #更多的rsync配置文件
#模块定义,主要是定义服务器哪个目录要被同步
#【backup】 #要同步的目录
#comment = this is module for backup #注释
#path = /backup #目录路径
#ignore errors #忽略I/O错误
#read only = no #默认为ture,不让客户端上传文件到服务器上.
#list = no
#auth users = zhu #虚拟用户
#secrets file = /etc/rsyncd.d/pass.server #虚拟用户密码存放地址
# mkdir /etc/rsyncd.d/ # vim /etc/rsysd.d/pass.server zhu:111 test:111
# chmod 600 /etc/rsyncd.d/pass.server
# /usr/local/bin/rsync --daemon --config=/etc/rsyncd.conf
查看服务进程和端口:
# ps -ef | grep rsync | grep -v grep root 12330 1 0 01:21 ? 00:00:00 /usr/local/bin/rsync --daemon --config=/etc/rsyncd.conf # netstat -luntup | grep rsync tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 12330/rsync
# echo "/usr/local/bin/rsync --daemon --profix=/etc/rsyncd.conf" >> /etc/rc.local # # tail -1 /etc/rc.local /usr/local/bin/rsync --daemon --profix=/etc/rsyncd.conf #
[root@kickstart ~]# telnet 192.168.10.17 873 Trying 192.168.10.17... Connected to 192.168.10.17. Escape character is ‘^]‘. @RSYNCD: 31.0 @ERROR: protocol startup error Connection closed by foreign host.
这样说明服务端的rsync服务开启,端口开放,正常可连接。
[root@kickstart ~]# cat /etc/rsync.passwd 111 [root@kickstart ~]# ll /etc/rsync.passwd -rw-------. 1 root root 4 Sep 4 01:59 /etc/rsync.passwd [root@kickstart ~]#
[root@kickstart ~]# rsync -avz test@192.168.10.17::backup /root/test/ --password-file=/etc/rsync.passwd receiving incremental file list 1.txt 2.txt 3.txt sent 80 bytes received 205 bytes 570.00 bytes/sec total size is 11 speedup is 0.04 [root@kickstart ~]#
[root@kickstart ~]# ll test/
total 4
-rw-r--r--. 1 root root 0 Sep 4 00:46 1.txt
-rw-r--r--. 1 root root 0 Sep 4 00:46 2.txt
-rw-r--r--. 1 root root 11 Sep 4 02:02 3.txt
原文:https://www.cnblogs.com/zwj-linux/p/11460386.html