博主: haitianisgood
原博客网址:
rsync
简介:
rsync : a fast, versatile, remote (and local) file-copying tool
rsync无论是centos和ubuntu、debian默认都已经安装
格式:
rsync [参数] SRC DEST
SRC 源地址,DEST目标地址
rsync命令格式:
Local: rsync [OPTION...] SRC... [DEST]
Access via remote shell:
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
Push: rsync [OPTION...] SRC... [USER@]HOST:DEST
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
环境
Centos 6 / Centos 7
xinetd
服务端
rsync的主配置文件rsyncd.conf(主配置文件)、rsyncd.secrets(密码文件)、rsyncd.motd(rysnc服务器信息),三个文件默认都不存在,需要手动创建。
/etc/rsyncd.conf必须的,文件名固定。
rsyncd.secrets和 rsyncd.motd不是必须的
rsync运行模式:
1.默认使用xinted运行,相对消耗资源大(centos7中默认未启用)
2.使用–daemon参数方式
/usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
xinetd安装
注意:
Centos7 可不使用 xinetd
Centos 5/6 推荐使用xinetd
Centos中安装:
yum -y install xinetd
开启rsync服务
Centos 5/6
chkconfig rsync on
查看rsync服务是否开启:
chkconfig --list|grep rsync
rsync: on
CentOS 7
systemctl enable rsyncd.service
若不想使用 xinetd ,直接跳转到下面rsync配置步骤即可
xinetd.d
注意:Centos 7 若不想使用xineted,无需此步骤
若自定义配置文件路径,可编辑 /usr/lib/systemd/system/rsyncd.service
也可以直接修改/etc/xinetd.d/rsync配置文件
将disable = yes改为no
Centos7 默认/etc/xinetd.d/下没有rsync配置文件,只需文件放入下面即可:
rsync内容:
# default: off
# description: The rsync server is a good addition to an ftp server, as it \
# allows crc checksumming etc.
service rsync
{
disable = no
flags = IPv6
socket_type = stream
wait = no
user = root
server = /usr/bin/rsync
server_args = --daemon
log_on_failure += USERID
}
创建rsync主配置文件
mkdir -p /etc/rsyncd
touch /etc/rsyncd/rsyncd.conf
ln -s /etc/rsyncd/rsyncd.conf /etc/rsyncd.conf
无用户验证简单配置:
vim /etc/rsyncd.conf
[test_server]
path=/data/jackie-server
uid =root
gid =root
write only =yes
read only = no
重启xinetd/rsyncd
centos 5/6
service xinetd restart
centos7
已使用xinetd:
systemctl restart xinetd.service
未使用xinetd:
systemctl restart rsyncd.service
检查启动
#netstat -an | grep 873
tcp 0 0 :::873 :::* LISTEN
#ps axu|grep xinetd
root 26320 0.0 0.0 22192 1028 ? Ss 14:38 0:00 xinetd -stayalive -pidfile /var/run/xinetd.pid
客户端
在客户端执行同步
例如:
服务器IP:10.0.8.7
客户端目录:/data/test_ client
查看服务器可同步的目录(这一步需要服务器端rsync配置中开启权限,默认是开启):
rsync --list-only root@10.0.8.7::
将本地文件同步到远程服务器端:
rsync --progress --bwlimit 2000 -v -r -topg -az /data/test_client 10.0.8.7::test_server
保持服务器端与客户端完全一致:
rsync --progress --bwlimit 2000 -v -r -topg -az --delete /data/test_client 10.0.8.7::test_server
会从test_server删除/data/test_client中不存在的目录及文件
常用参数:
-r 是递归
-l 是链接文件,意思是拷贝链接文件;
-p 表示保持文件原有权限
-t 保持文件原有时间
-g 保持文件原有用户组
-o 保持文件原有属主
-z 传输时压缩;
-P 传输进度(可实现断点续传)
-v 传输时的进度等信息,和-P有点关系
-D 相当于块设备文件
-e ssh的参数建立起加密的连接。
-u 只进行更新,防止本地新文件被重写,注意两者机器的时钟的同时
--progress 指显示出详细的进度情况
--delete 从DEST中删除SRC中不存在的文件,保持SRC和DEST真正的一致
--password-file = /password/path/file 来指定密码文件,里面只含用户密码。这样就可以在脚本中使用而无需交互式地输入验证密码了,这里需要注意的是这份密码文件权限属性要设得只有属主可读。
--bwlimit 限制每秒I/O,单位KB
用户验证实例:
服务器端
创建密码文件
cd /etc/rsyncd/
touch rsyncd.secrets
chmod 600 rsyncd.secrets
vim rsyncd.secrets
rsync:jackie
注解:
1.rsyncd.secrets权限一定是 600,否则客户端将不能连接服务器
2.rsyncd.secrets格式:
用户:密码
3.密码文件中的用户不一定非要是系统用户
主配置文件:
vim /etc/rsyncd.conf
uid =root
gid =root
secrets file = /etc/rsyncd/rsyncd.secrets
[test_server]
path=/data/jackie-server
auth users = rsync
write only = yes
read only = no
客户端
同步test_client数据到rsync服务器目录中:
rsync --progress --bwlimit 2000 -v -r -topg -az /data/test_client/ rsync@10.0.8.7::test_server
主配置文件详解
此文件只在rsync服务器端配置
/etc/rsyncd.conf
# Minimal configuration file for rsync daemon
# See rsync(1) and rsyncd.conf(5) man pages for help
# This line is required by the /etc/init.d/rsyncd script
# GLOBAL OPTIONS
uid = root
gid = root
use chroot = no
read only = yes
#limit access to private LANs
hosts allow=172.16.0.0/255.255.0.0 192.168.1.0/255.255.255.0 10.0.1.0/255.255.255.0
hosts deny=*
max connections = 5
pid file = /var/run/rsyncd.pid
secrets file = /etc/rsyncd/rsyncd.secrets
#lock file = /var/run/rsync.lock
motd file = /etc/rsyncd/rsyncd.motd
#This will give you a separate log file
log file = /var/log/rsync.log
#This will log every file transferred - up to 85,000+ per user, per sync
transfer logging = yes
log format = %t %a %m %f %b
syslog facility = local3
timeout = 300
# MODULE OPTIONS
[test_server]
path = /data/jackie-server
list=yes
ignore errors
auth users = rsync
comment = jackie home
exclude = important/
全局参数说明:
pid file = /var/run/rsyncd.pid
指定进程文件/var/run/rsyncd.pid
port = 873
指定运行端口,默认是873,可以自己指定
uid = root
gid = root
服务器端传输文件时,要用哪个用户和用户组来执行,默认是nobody
use chroot = yes
用chroot,在传输文件之前,服务器守护程序在将chroot 到文件系统中的目录中,这样做的好处是可能保护系统被安装漏洞侵袭的可能。缺点是需要超级用户权限。另外你在 rsync服务器上,如果有符号链接,你在备份服务器上运行客户端的同步数据时,只会把符号链接名同步下来,并不会同步符号链接的内容。建议设置为no。
read only = yes
read only 是只读选择,不让客户端上传文件到服务器上。
write only = yes
是否允许客户下载文件。若为 yes 则不允许下载;若为 no 并且服务器目录也具有读权限则允许下载。
hosts allow =
在您可以指定单个IP,也可以指定整个网段,能提高安全性。格式是ip 与ip 之间、ip和网段之间、网段和网段之间要用空格隔开
max connections = 5
客户端最多连接数
motd file = /etc/rsyncd/rsyncd.motd
里面内容可以随意写
ignore errors
指定在 rsync 服务器上运行 delete 操作时是否忽略 I/O 错误。一般来说 rsync 在出现 I/O 错误时将将跳过-delete 操作,以防止因为暂时的资源不足或其它 I/O 错误导致的严重问题。
lock file = /var/run/rsyncd.lock
指定支持 max connections 参数的锁文件
log file = /var/log/rsync.log
rsync 服务器的日志
transfer logging = yes
传输文件的日志
log format = %t %a %m %f %b
日志格式
syslog facility = local3
指定 rsync 发送日志消息给 syslog 时的消息级别。
timeout = 300
strict modes
指定是否监测口令文件的权限。若为 true 则口令文件只能被 rsync 服务器运行身份的用户访问,其他任何用户不可以访问该文件。
模块参数说明:
[test_server]
模块它为我们提供了一个链接的名字,在本模块中链接到了/data/jackie-server目录;要用[test_server] 形式
path = /data/jackie-server
指定同步文件目录,此参数必须指定
auth users = root
认证用户是root,必须在服务器上存在的用户
list = yes
把rsync 服务器上提供同步数据的目录在服务器上模块是否显示列出来,默认是yes。如果你不想列出来,就no;如果是no是比较安全的
ignore errors
忽略IO错误
secrets file = /etc/rsyncd.secrets
指定使用的密码文件
comment = jackie data
注释说明,可以自己定义
exclude = aa/ bb/
exclude是排除的意思,也就是说,要把/data/jackie-server目录下的aa和bb排除,俩目录之间用空格分隔
参考网址
官网: