Rsync 简单配置
rsync同步文件
rsync,remote synchronize 一款优秀的文件同步软件,最初是用来替换rcp的,相比rcp,rsync还具有一些更吸引人的特性,如支持链接复制、保持文件属主权限、支持SSH等,rsync通过一种高效的算法检测哪些文件有更新,只传输已更新的数据,大大降低了系统IO和传输带宽。
目前大多数linux发行版本都已安装了rsync,如果没有安装,可以去官方网站下载最新版本编译安装
为便于理解,可将rsync为c/s结构,分为发送端(服务端)和接收端(客户端),由客户端发送同步请求,服务端检查客户端权限信息,认证通过后开始处理客户端请求。
发送端配置
Redhat系发发行版的rsync一般用xinet来运行,默认rsync服务是没有启用的,将服务改成自动启动
#chkconfig rsync on
或编辑/etc/xinet.d/rsync文件,将disable项值改成’no’
service rsync
{
disable = no
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = –daemon
log_on_failure += USERID
}
编辑配置文件/etc/rsyncd.conf
uid = username
gid = grpname
max connections = 2
use chroot = no
log file = /var/log/rsyncd.log
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsyncd.lock
secrets file = /etc/rsyncd.secret[modules-1]
path = /home/somepath1
comment = some comment
ignore errors
read only = no
list = no
auth users = user1[modules-2]
……
rsync默认监听tcp 873端口,可用port配置项来更改服务端口,其配置文件一般可分为全局区和模块区,全局区对整个daemon有效,模块区细分至每个传输模块
[module_name]:模块名称指定,客户端请求时可指定模块名
uid:当服务程序以root身份运行时文件的属主,结合gid决定了文件权限,默认的uid和gid都是-2,即nobody
max connections: 请求端的最大并发连接数量
use chroot: 默认为true,rsync daemon在响应请求端文件传输请求前先chroot至指定目录
log file:日志文件
pid file:进程id文件
lock file:与max conncetions有关
path:指定本模块传输目录
comment:注释
read only:决定请求端能否上传文件,默认true
list:指示本模块是否响应请求端的list请求,决定是否隐藏模块
auth users:指定chap用户名,多个可用逗号空格分隔,这里的用户可以不是系统用户,rsync默认不作用户验证,密码文件由secrets file指定,这个文件必须是只有root可读写
由于配置了用户认证,这里还得生成一个密码文件(由secrets file指定),vi /etc/rsyncd.secrets 加入用户密码,格式如下
user1:userpass1
user2:userpass2
启动rsync服务 /etc/init.d/xined restart
接收端
接收端只需要执行同步命令就可以了,对于配置了认证的服务端还需要发送用户名密码,常用命令如下:
export RSYNC_PASSWORD=userpass1
rsync -a –delete rsync://user1@rsync_server/module_1 /home/mypath1
export RSYNC_PASSWORD=userpass2
rsync -a –delete rsync://user1@rsync_server/module_2 /home/mypath2
其中-a是linux版本rsync特有选项,相当于-rlptgoD,recursively,symlink复制,permission保留,时间戳保留,属主、组信息保留,重建设备文件和特殊文–devices –special
–delete 是删除接收端的文件,这些文件在发送端不存在
user1为服务端配置的模块module_1的认证用户名,密码在环境变量RSYNC_PASSWORD中读取,/home/mypath1这个是发送端指定模块同步到接收端后的存放目录,可以是不存在的,rsync会自动建立
rsync配置比较简单,其它选项可参考man 5 rsyncd.conf,实际应用中rsync多与crontab结合,以实现定时自动同步,实现效果与商业备份软件差不多
指定同步的rsync端口
(1)rsync -Cavz –port=port user@host::modules/src /dest
(2)rsync -Cavz rsync://user@host:port/modules/src /dest
第一种方法用–port参数来指定端口,远程路径使用::后接rsync 模块名
第二种方法用rsync://user@host:port来指定端口,远程路径使用/后接rsync 模块名
rsync同步方法:
(1)使用rsync同步,但实际用的是scp同步
Access via remote shell:
Pull: rsync [OPTION…] [USER@]HOST:SRC… [DEST]
Push: rsync [OPTION…] SRC… [USER@]HOST:DEST
单:表示用scp端口来同步,:后接绝对路径;此处的user为linux user
(2)使用rsync守护进程来同步
Access via rsync daemon:
Pull: rsync [OPTION…] [USER@]HOST::SRC… [DEST]
rsync [OPTION…] rsync://[USER@]HOST[:PORT]/SRC… [DEST]
Push: rsync [OPTION…] SRC… [USER@]HOST::DEST
rsync [OPTION…] SRC… rsync://[USER@]HOST[:PORT]/DEST
这两种方法的不同在于:
i:命令操作不同,rsync://
ii:第一种方法使用::来跟模块名,第二种方法使用/来跟模块名
iii:第一种方法指定端口需要加参数–port,第二种方法使用rsync://user@host:port来指定端口
提示:rsync进行同步时,使用scp端口的同步需要加绝对路径,用户是linux user;使用rsync端口同步时需要加模块名,用户是rsync定义的user