rsync常用选项
-a 包含-rtplgoD
-r 同步目录时要加上,类似cp时的-r选项
-v 同步时显示一些信息,让我们知道同步的过程
-l 保留软连接
-L 加上该选项后,同步软链接时会把源文件给同步
-p 保持文件的权限属性
-o 保持文件的属主
-g 保持文件的属组
-D 保持设备文件信息
-t 保持文件的时间属性
--delete 删除DEST中SRC没有的文件
--exclude 过滤指定文件,如--exclude “logs”会把文件名包含logs的文件或者目录过滤掉,不同步
-P 显示同步过程,比如速率,比-v更加详细
-u 加上该选项后,如果DEST中的文件比SRC新,则不同步
-z 传输时压缩
--progress:显示数据传输的进度信息
--password-file=FILE:指定密码文件,将密码写入文件,实现非交互式数据同步,这个文件名也需要修改权限为600
--delete:删除那些仅在目标路径中存在的文件(源路径中不存在),在脚本中的数据同步经常加上此参数
--list-only:仅列出服务器模块列表,需要rsync服务器设置list=true
rsyncd.conf样例:
port=873
log file=/var/log/rsync.log
pid file=/var/run/rsyncd.pid
address=192.168.36.130
[test]
path=/tmp/rsync
use chroot=true
max connections=4
read only=no
list=true
uid=root
gid=root
auth users=test
secrets file=/etc/rsyncd.passwd
hosts allow=192.168.36.131 (多个ip以空格隔开,也可以写ip段:192.168.36.0/24)
rsyncd.conf配置文件详解
port:指定在哪个端口启动rsyncd服务,默认是873端口。
log file:指定日志文件。
pid file:指定pid文件,这个文件的作用涉及服务的启动、停止等进程管理操作。
address:指定启动rsyncd服务的IP。假如你的机器有多个IP,就可以指定由其中一个启动rsyncd服务,如果不指定该参数,默认是在全部IP上启动。
[]:指定模块名,里面内容自定义。
path:指定数据存放的路径。
use chroot true|false:表示在传输文件前首先chroot到path参数所指定的目录下。这样做的原因是实现额外的安全防护,但缺点是需要以roots权限,并且不能备份指向外部的符号连接所指向的目录文件。默认情况下chroot值为true,如果你的数据当中有软连接文件,阿铭建议你设置成false。
max connections:指定最大的连接数,默认是0,即没有限制。
read only ture|false:如果为true,则不能上传到该模块指定的路径下。
list:表示当用户查询该服务器上的可用模块时,该模块是否被列出,设定为true则列出,false则隐藏。
uid/gid:指定传输文件时以哪个用户/组的身份传输。
auth users:指定传输时要使用的用户名。
secrets file:指定密码文件,该参数连同上面的参数如果不指定,则不使用密码验证。注意该密码文件的权限一定要是600。格式:用户名:密码
hosts allow:表示被允许连接该模块的主机,可以是IP或者网段,如果是多个,中间用空格隔开。
当设置了auth users和secrets file后,客户端连服务端也需要用用户名密码了,若想在命令行中带上密码,可以设定一个密码文件
rsync -avL test@192.168.36.130::test/test1/ /tmp/test8/ --password-file=/etc/pass
其中/etc/pass内容就是一个密码,权限要改为600
rsync -av test1/ test@192.168.36.130:test/
二. 主配置文件说明
vim /etc/rsyncd.conf
motd file = /etc/rsyncd.motd #设置服务器信息提示文件,在该文件中编写提示信息
transfer logging = yes #开启rsync数据传输日志功能
log file = /var/log/rsyncd.log #设置日志文件名,可通过log format参数设置日志格式
pid file = /var/run/rsyncd.log #设置rsync进程号保存文件名称
lock file = /var/run/rsync.lock #设置锁文件名称
port = 873 #设置服务器监听的端口号,默认是873
address = 192.168.0.230 #设置本服务器所监听网卡接口的ip地址
uid = nobody #设置进行数据传输时所使用的帐户名或ID号,默认使用nobody
gid = nobody #设置进行数据传输时所使用的组名或GID号,默认使用nobody
#若为yes, rsync会首先进行chroot设置,将根映射在下面的path参数路径下,对客户端而言,系统的根就是path参数指定的路径。但这样做需要root权限,并且在同步符号连接资料时只会同步名称,不会同步内容。
use chroot = no
read only = yes #是否允许客户端上传数据,yes表示不允许
max connections =10 #设置并发连接数,0表示无限制
[common] #自定义模块名,rsync通过模块定义同步的目录,可定义多个
comment = web content #定义注释说明字串
path = /common #同步目录的真是路径通过path指定
ignore errors #忽略一些IO错误
#exclude = test/ #exclude指定common目录下某个目录可以不同步数据
auth users = tom, jerry #设置允许连接服务器的账户,此账户可以是系统中不存在的用户
secrets file = /etc/rysncd.secrets #密码验证文件名,该文件权限要求为只读,建议为600,仅在设置auth users后有效
hosts allow = 192.168.0.0/255.255.255.0 #设置哪些主机可以同步数据,多ip和网段之间使用空格分隔
hosts deny=* #除了hosts allow定义的主机外,拒绝其他所有
list = false #客户端请求显示模块列表时,本模块名称是否显示,默认为true
下面贴一个实际应用的案例:
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:
# uid = nobody
# gid = nobody
# use chroot = yes
# max connections = 4
# pid file = /var/run/rsyncd.pid
# exclude = lost+found/
# transfer logging = yes
# timeout = 900
# ignore nonreadable = yes
# dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# [ftp]
# path = /home/ftp
# comment = ftp export area
uid = root
gid = root
use chroot = no
max connections = 12
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
secrets file = /etc/rsyncd.passwd
hosts allowd = 192.168.16. 服务器ip(也可这么写)
hosts allowd = 服务器ip
hosts allowd = 服务器ip
#aliyun_backup
#backup for uv prod services on aliyun platform
#ip_range:172.17.96.3,172.17.96.4,172.17.96.5,172.17.96.6,172.17.96.7,172.17.96.8
#web_html,conf_files,api_pkgs,mysql_bakfiles,redis etc.
#aliyun_backup
[aliyun]
comment=aliyun
path=/data/backup/aliyun
read only = no
auth users=backup
[wangsu]
comment=wangsu
path=/data/backup/wangsu
read only = no
auth users=backup
#localnet
#backup for local servers
#ip_range:126,204,206,209,210,216,218,220 etc.
#git,ipub,zentao,mall,jks,vpn,mysql,jira,vpn etc.
#localnet
[git]
comment = git backup
path = /data/backup/localnet/git
read only = no
auth users = backup
rsync常见问题及解决办法
错误一:
password file must not be other-accessible
continuing without password file
Password:
rsync客户端路径是否写错,权限设置不对,需要再次输入密码,客户端和服务端的密码文件都应该是600的权限才可以
如上图:文件就是/etc/rsyncd.passwd
错误二:
@ERROR: Unknown module ‘bak’
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver= 3.0.3]
服务端server的配置中的[bak]名字和客户端client的10.10.10.10::bak不符
错误三:
rsync: failed to connect to 10.10.10.10: Connection timed out (110)
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.6]
检查服务端server服务是否正常启动,检查端口防火墙,iptables打开873端口
如果服务端是windows server则在防火墙入站规则中增加873端口
如果服务端是Linux则先检查服务是否启动#ps aux | grep rsync
然后开启873端口#iptables -A INPUT -p tcp --dport 873 -j ACCEPT开启873端口
附:
安装rsync yum install rsync
启动服务/usr/bin/rsync --daemon
启动服务错误failed to create pid file /var/rsyncd.pid: File exists
看看提示服务错误的路径(这个路径不一定就是这个,看自己的报错路径)这里是/var/rsyncd.pid所以
rm -rf /var/rsyncd.pid;再重新启动Rsync服务
此时在看一下ps aux | grep rsync启动成功
错误四:
@ERROR: access denied to gmz88down from unknown (10.10.10.10)
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]
看看是不是服务端server hosts allow限制了IP,把这里的IP加入到服务端server的hosts allow白名单中,windows rsync不能写多个allow,可以在一个allow中加多个IP,例:hosts allow=10.10.10.10 20.20.20.20
错误五:
@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]
服务端server的目录不存在或者没有权限(要同步的那个文件路径),安装windows rsync时候会创建一个SvcCWRSYNC用户,这个用户对要拷贝的目录没有权限,方法一,将这个用户给权限加入到目录中,方法二,修改这个用户隶属于的组,修改后要在管理中重启服务
错误六:
rsync error: error starting clie
nt-server protocol (code 5) at main.c(1524) [Receiver= 3.0.7 ]