rsync

特性

  • 可以镜像保存整个目录树和文件系统。

  • 可以很容易做到保持原来文件的权限、时间、软硬链接等等。

  • 无须特殊权限即可安装。

  • 快速:第一次同步时 rsync 会复制全部内容,但在下一次只传输修改过的文件。rsync 在传输数据的过程中可以实行压缩及解压缩操作,因此可以使用更少的带宽。

  • 安全:可以使用 scp、ssh 等方式来传输文件,当然也可以通过直接的 socket 连接。

  • 支持匿名传输,以方便进行网站镜象。

语法

  • rsync 有六种不同的工作模式。

rsync [OPTION]... SRC DEST 
rsync [OPTION]... SRC [USER@]host:DEST 
rsync [OPTION]... [USER@]HOST:SRC DEST 
rsync [OPTION]... [USER@]HOST::SRC DEST 
rsync [OPTION]... SRC [USER@]HOST::DEST 
rsync [OPTION]... 
rsync://[USER@]HOST[:PORT]/SRC [DEST]
  • 拷贝本地文件。当 SRC 和 DES 路径信息都不包含有单个冒号 ":" 分隔符时就启动这种工作模式。

rsync -a /data /backup
  • 使用一个远程 shell 程序(如 rsh、ssh)来实现将本地机器的内容拷贝到远程机器。当 DST 路径地址包含单个冒号 ":" 分隔符时启动该模式。

rsync -avz *.c foo:src
  • 使用一个远程 shell 程序(如 rsh、ssh)来实现将远程机器的内容拷贝到本地机器。当 SRC 地址路径包含单个冒号 ":" 分隔符时启动该模式。

rsync -avz foo:src/bar /data
  • 从远程 rsync 服务器中拷贝文件到本地机。当 SRC 路径信息包含 "::" 分隔符时启动该模式。

rsync -av root@192.168.78.192::www /databack
  • 从本地机器拷贝文件到远程 rsync 服务器中。当 DST 路径信息包含 "::" 分隔符时启动该模式。

rsync -av /databack root@192.168.78.192::www
  • 列远程机的文件列表。这类似于 rsync 传输,不过只要在命令中省略掉本地机信息即可。

rsync -v rsync://192.168.78.192/www

参数选项

参数选项 说明
-v,--verbose 详细模式输出。
-q,--quiet 精简输出模式。
-c,--checksum 打开校验开关,强制对文件传输进行校验。
-a,--archive 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于 -rlptgoD。
-r,--recursive 对子目录以递归模式处理。
-R,--relative 使用相对路径信息。
-b,--backup 创建备份,也就是对于目的已经存在有同样的文件名时,将老的文件重新命名为 ~filename。可以使用 --suffix 选项来指定不同的备份文件前缀。
--backup-dir 将备份文件(如 ~filename)存放在在目录下。
-suffix=SUFFIX 定义备份文件前缀。
-u,--update 仅仅进行更新,也就是跳过所有已经存在于 DST,并且文件时间晚于要备份的文件,不覆盖更新的文件。
-l,--links 保留软链结。
-L,--copy-links 像对待常规文件一样处理软链结。
--copy-unsafe-links 仅仅拷贝指向 SRC 路径目录树以外的链结。
--safe-links 忽略指向 SRC 路径目录树以外的链结。
-H,--hard-links 保留硬链结。
-p,--perms 保持文件权限。
-o,--owner 保持文件属主信息。
-g,--group 保持文件属组信息。
-D,--devices 保持设备文件信息。
-t,--times 保持文件时间信息。
-S,--sparse 对稀疏文件进行特殊处理以节省DST的空间。
-n,--dry-run 现实哪些文件将被传输。
-w,--whole-file 拷贝文件,不进行增量检测。
-x,--one-file-system 不要跨越文件系统边界。
-B,--block-size=SIZE 检验算法使用的块尺寸,默认是 700 字节。
-e,--rsh=command 指定使用 rsh、ssh 方式进行数据同步。
--rsync-path=PATH 指定远程服务器上的rsync命令所在路径信息。
-C,--cvs-exclude 使用和 CVS 一样的方法自动忽略文件,用来排除那些不希望传输的文件。
--existing 仅仅更新那些已经存在于 DST 的文件,而不备份那些新创建的文件。
--delete 删除那些 DST 中 SRC 没有的文件。
--delete-excluded 同样删除接收端那些被该选项指定排除的文件。
--delete-after 传输结束以后再删除。
--ignore-errors 及时出现 IO 错误也进行删除。
--max-delete=NUM 最多删除 NUM 个文件。
--partial 保留那些因故没有完全传输的文件,以是加快随后的再次传输。
--force 强制删除目录,即使不为空。
--numeric-ids 不将数字的用户和组id匹配为用户名和组名。
--timeout=time ip 超时时间,单位为秒。
-I,--ignore-times 不跳过那些有同样的时间和长度的文件。
--size-only 当决定是否要备份文件时,仅仅察看文件大小而不考虑文件时间。
--modify-window=NUM 决定文件是否时间相同时使用的时间戳窗口,默认为 0。
-T --temp-dir=DIR 在 DIR 中创建临时文件。
--compare-dest=DIR 同样比较 DIR 中的文件来决定是否需要备份。
-P 等同于 --partial。
--progress 显示备份过程。
-z,--compress 对备份的文件在传输时进行压缩处理。
--exclude=PATTERN 指定排除不需要传输的文件模式。
--include=PATTERN 指定不排除而需要传输的文件模式。
--exclude-from=FILE 排除 FILE 中指定模式的文件。
--include-from=FILE 不排除 FILE 指定模式匹配的文件。
--version 打印版本信息。
--address 绑定到特定的地址。
--config=FILE 指定其他的配置文件,不使用默认的 rsyncd.conf 文件。
--port=PORT 指定其他的 rsync 服务端口。
--blocking-io 对远程 shell 使用阻塞 IO。
-stats 给出某些文件的传输状态。
--log-format=formAT 指定日志文件格式。
--password-file=FILE 从FILE中得到密码。
--bwlimit=KBPS 限制I/O带宽,KBytes per second。
-h,--help 显示帮助信息。

使用方式

无密码同步

  • 服务端修改配置文件 /etc/rsyncd.conf

#This is the rsync daemon configuration #global settings pid file = /var/run/rsyncd.pid
port = 873lock file = /var/run/rsyncd.lock
log file = /var/log/rsync.log
gid = root
uid = root#module settings [share_data]
path = /web/rsync/share_datause chroot = nomax connections = 15read only = yeswrite only = nolist = noignore errors = yestimeout = 120
/usr/bin/rsync --daemon
mkdir -p /web/rsync/share_data
  • 客户端同步

rsync -avz --progress root@192.168.1.98::share_data /home/hadoop/share_data
  • 客户端限制流量同步

rsync -avz --bwlimit=50 --progress root@192.168.1.98::share_data /home/hadoop/share_data

有密码同步

  • 服务端修改配置文件

#global settings pid file = /var/run/rsyncd.pid
port = 873lock file = /var/run/rsyncd.locklog file = /var/log/rsync.loggid = root
uid = root #module settings 
[auth_data]
path = /web/rsync/auth_data
use chroot = no
max connections = 15read only = yes
write only = nolist = no
ignore errors = yes
timeout = 120auth users = hadoop
secrets file = /etc/rsyncd.passwd
echo "hadoop:password123" > /etc/rsyncd.passwd 
chmod 600 /etc/rsyncd.passwd
mkdir -p /web/rsync/auth_data
  • 客户端同步

echo "password123" > /home/hadoop/rsyncd.passwd 
chmod 600 /home/hadoop/rsyncd.passwd 
rsync -avz --progress  --password-file=/home/hadoop/rsyncd.passwd  hadoop@192.168.1.98::auth_data /home/hadoop/auth_data

写入同步

  • 服务端修改配置文件

#global settings pid file = /var/run/rsyncd.pid
port = 873lock file = /var/run/rsyncd.locklog file = /var/log/rsync.loggid = root
uid = root #module settings 
[write_data]
path = /web/rsync/write_data
use chroot = no
max connections = 15read only = nolist = no
ignore errors = yes
timeout = 120auth users = hadoop
secrets file = /etc/rsyncd.passwd
mkdir -p /web/rsync/write_data
  • 客户端同步

echo "123" > /home/hadoop/write_fileexport RSYNC_PASSWORD="password123"rsync -avz --progress --delete /home/hadoop/write_file hadoop@192.168.1.98::write_data
  • 限定 IP 或者网段

#global settings pid file = /var/run/rsyncd.pid
port = 873lock file = /var/run/rsyncd.locklog file = /var/log/rsync.loggid = root
uid = root #module settings 
[write_data]
path = /web/rsync/write_data
use chroot = no
max connections = 15read only = nolist = no
ignore errors = yes
timeout = 120auth users = hadoop
secrets file = /etc/rsyncd.passwd
hosts allow = 192.168.2.32  192.168.1.0/24

配置说明

配置 说明
pid file = <FILE> 进程写入文件。
port = 指定端口,默认是 873。
address = 指定服务器 IP 地址。
uid = 服务器端传输文件时,指定用户执行,默认 nobody。
gid = 服务器端传输文件时,指定用户组执行,默认 nobody。
use chroot = 服务器守护程序将 chroot 到文件系统的目录中,好处在于保护系统不被安装漏洞侵袭。缺点是需要超级用户权限。
read only = yes|no 只读选择,不让客户端上传文件到服务器上。
write only = yes|no 写入选择。
hosts allow = 限制 IP 或网段,可以指定单个 IP,也可以指定整个网段,提高安全性。格式是空格间隔。
max connections = 客户端最多连接数。
motd file = <FILE> 用于定义服务器信息,当用户登录时会看到文件中的内容信息。
log file = <FILE> 服务器的日志。
transfer logging = yes|no 是否打印传输日志
log format = 日志格式(%t %a %m %f %b)
lock file = <FILE> 加锁文件。
path = <DIR> 指定同步文件目录所在位。
auth users = 认证用户,必须在服务器上存在的用户。
list = yes|no 是否把 rsync 服务器上提供同步数据的目录在服务器上模块显示出来。默认为 yes。
ignore errors 忽略 IO 错误。
secrets file = <FILE> 密码存放文件,有密码登录时用到。
comment = 注释可自定义。
exclude = 排除目录或文件,目录之间可用空格分开。
  • rsync 的主要有三个配置文件 rsyncd.conf(主配置文件)、rsyncd.passwd(密码文件)、rsyncd.motd(rysnc 服务器信息)。

  • 将 rsyncd.passwd 密码文件的文件属性设为 root 拥有,且权限要设为 600,否则无法备份成功。

  • 出于安全目的,文件的属性必需是只有属主可读。

  • 密码文件格式为:

用户名:密码

常见错误

  • 可以通过 rsyncd.log 或者 .err 文件查看错误信息。

chroot failed

@ERROR: chroot failed 
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
  • 服务器端的目录不存在或无权限,创建目录并修正权限可解决问题。

auth failed on module xxx

@ERROR: auth failed on module xxx
rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
  • 密码或用户名输入错误。

  • secrets file = /etc/rsync.password 指定的密码文件和实际密码文件名称不一致。

  • /etc/rsync.password 文件权限不是600

  • share:111111 密码配置文件后面不要有空格。

  • rsync 客户端密码文件中只输入密码信息即可,不需要输入虚拟认证用户名称。

Unknown module 'xxx'

@ERROR: Unknown module 'xxx' rsync error: error starting client-server protocol (code 5) at main.c(1522) [receiver=3.0.3]
  • 服务器不存在指定模块。提供正确的模块名或在服务器端修改成你要的模块以解决问题。

  • 配置文件中网段限制不对。

No space left on device

rsync: write failed on "/home/rsync/xxx": No space left on device (28) 
rsync error: error in file IO (code 11) at receiver.c(302) [receiver=3.0.7] 
rsync: connection unexpectedly closed (2721 bytes received so far) [generator] 
rsync error: error in rsync protocol data stream (code 12) at io.c(601) [generator=3.0.7]
  • 磁盘空间不足。

Permission denied

rsync: opendir "/xxx" (in dtsChannel) failed: Permission denied (13)
  • 查看同步的目录权限是否为 755。

  • 共享目录的属主和属组不正确,不是 rsync。

Connection timed out

rsync: failed to connect to 192.168.102.31: Connection timed out (110) 
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
  • 检查服务器的端口 netstat -tunlp,远程 telnet 测试。 可能因为客户端或者服务端的防火墙开启导致无法通信,可以设置规则放行 rsync(873端口) 或者直接关闭防火墙。

Connection refused

rsync: failed to connect to 192.168.102.31: Connection refused (111) 
rsync error: error in socket IO (code 10) at clientserver.c(124) [receiver=3.0.5]
  • rsync 服务没开启

  • 启动服务 rsync --daemon --config=/etc/rsyncd.conf

No route to host

rsync: failed to connect to 192.168.102.31: No route to host (113) 
rsync error: error in socket IO (code 10) at clientserver.c(104) [receiver=2.6.9]
  • 防火墙问题导致,先彻底关闭防火墙,无论是 S 还是 C,还有 ignore errors 选项问题也会导致。

access denied to www from unknown

@ERROR: access denied to www from unknown (192.168.102.31)
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in rsync protocol data stream (code 12) at io.c(359)
  • 配置选项 host allow 的问题。

received SIGINT

rsync error: received SIGINT, SIGTERM, or SIGHUP (code 20) at rsync.c(244) [generator=2.6.9]rsync error: received SIGUSR1 (code 19) at main.c(1182) [receiver=2.6.9]
  • 多半是服务端服务没有被正常启动,服务器上查看服务是否有启动,查看 /var/run/rsync.pid 文件是否存在。

Connection reset by peer

rsync: read error: Connection reset by peer (104)
rsync error: error in rsync protocol data stream (code 12) at io.c(604) [sender=2.6.9]
  • 原数据目录里没有数据存在。

The remote path must start with a module name not a /

ERROR: The remote path must start with a module name not a /
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
  • rsync 命令语法理解错误,::/share 是错误的语法,应该为::share(rsync 模块)

chdir failed

@ERROR: chdir failed
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
  • 备份存储目录没有建立。

  • 建立的备份存储目录和配置文件定义不一致。

invalid uid rsync

@ERROR: invalid uid rsync
rsync error: error starting client-server protocol (code 5) at main.c(1503) [sender=3.0.6]
  • rsync 服务对应 rsync 虚拟用户不存在。

--passwd-file=/etc/rsync.passwd: unknown option

rsync: --passwd-file=/etc/rsync.passwd: unknown option
rsync error: syntax or usage error (code 1) at main.c(1422) [client=3.0.6]
  • rsync 服务没有开启。

  • 防火墙是否允许。

  • SELinux 是否开启允许。

  • sshd 传输受限,限制了传输的 ip。

rsync 服务端排错思路

  • 查看 rsync 服务配置文件路径是否正确 /etc/rsyncd.conf。

  • 查看配置文件用例 host allow,host deny,允许的 ip 网段是否是允许客户端访问的 ip 网段。

  • 查看配置文件中 path 参数里的路径是否存在,权限是否正确(正常应为配置文件中的 UUID 参数对应的属主和组)。

  • 查看 rsync 服务是否启动,端口是否存在 ps -ef netstat -lntup。

  • 查看 iptables 防火墙和 SELinux 是否开启允许 rsync 服务通过。

  • 查看服务端 rsync 配置文件里的密码权限是否为 600,密码文件格式是否正确,正确格式(用户名:密码)文件路径和配置文件里的secrect files 参数对应。

  • 如果是推送数据,查看配置 rsyncd.conf 文件中用户是否对模块下目录有可读的权限。

rsync 客户端排错思路

  • 查看客户端 rsync 配置的密码文件是否为 600 权限,密码文件格式是否正确,注意:仅需要有密码并且与服务端的密码一致。

  • telnet 连接 rsync 服务器 ip 地址 873 端口,查看服务是否启动(可测试服务端防火墙是否阻挡 telnet10.0.0.100 873)。

  • 客户端执行命令双引号随后为模块名称。

转: https://www.jianshu.com/p/258ceb7b2223

rsync 参数配置说明[转]的更多相关文章

  1. rsync 参数断点续传

    断点续传是使用大写P参数,-P这个参数是综合了--partial --progress两个参数 rsync -avzP /home/hadoop/jdk1..0_73.tar.gz root@10.2 ...

  2. [转]RSYNC 参数中文详解

    FROM : http://www.qiansw.com/rsync-cn.html rsync是一款好用的*nux文件同步工具.下面是其参数的中文解释. Rsync 参数选项说明 -v, --ver ...

  3. C3P0连接池参数配置说明

    C3P0连接池参数配置说明 created by cjk on 2017.8.15 常用配置 initialPoolSize:连接池初始化时创建的连接数,default : 3(建议使用) minPo ...

  4. Rsync参数介绍

    Rsync参数介绍   一.Rsync Rsync是一款开源的.快速的.多功能的.可实现全量及增量的本地或远程数据镜像同步备份的优秀工具.rsync适用于unix/linux/windows等多种操作 ...

  5. rsync参数详解

    Rsync的参数详细解释 -v, --verbose 详细模式输出-q, --quiet 精简输出模式-c, --checksum 打开校验开关,强制对文件传输进行校验-a, --archive 归档 ...

  6. spark-submit提交spark任务的具体参数配置说明

    spark-submit提交spark任务的具体参数配置说明 1.spark提交任务常见的两种模式 2.提交任务时的几个重要参数 3.参数说明 3.1 executor_cores*num_execu ...

  7. Rsync 详细配置说明

    rsync是类unix系统下的数据镜像备份工具. 它的特性如下:可以镜像保存整个目录树和文件系统.可以很容易做到保持原来文件的权限.时间.软硬链接等等.无须特殊权限即可安装.快速:第一次同步时 rsy ...

  8. rsync参数及通信

    rsync 支持:  本机数据 <-------> 远程数据/本地数据 意义:  支持增量拷贝 --> 备份,节省带宽,时间   rsync -avL 一.常用选项  ******* ...

  9. Spark参数配置说明

    1  修改$SPARK_HOME/conf目录下的spark-defaults.conf文件 添加以下配置项 spark.sql.hive.convertMetastoreParquet       ...

随机推荐

  1. Java静态变量的用法:伪单例

    这几天遇到一个问题,一个Service里有一个map,但是这个Service有别的继承,于是每一个Service都会创建一个map,但是这个map应该是公用的,于是就有问题了...(按结构说Servi ...

  2. 搜索引擎Hoot的源码阅读(提供源码)

    开门见山,最近阅读了一下一款开源引擎的源码,受益良多(学到了一些套路).外加好久没有写博客了(沉迷吃鸡,沉迷想念姑娘),特别开一篇.Hoot 的源码地址, 原理介绍地址.外加我看过之后的注释版本,当然 ...

  3. Winform中的DatagridView显示行号

    1.设置 RowPostPaint 为true 2.启用RowPostPaint事件 /// <summary> /// DataGridView显示行号 /// </summary ...

  4. Android日期时间选择器DatePicker、TimePicker日期时间改变事件响应(Android学习笔记)

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  5. bitest(位集合)------c++程序设计原理与实践(进阶篇)

    标准库模板类bitset是在<bitset>中定义的,它用于描述和处理二进制位集合.每个bitset的大小是固定的,在创建时指定: bitset<4> flags; bitse ...

  6. Kotlin when 流程判断

    如果学过C或者java C#等语言. 一定熟悉SWITCH这个流程判断 但是在kotlin中却没有这个.而是 使用了When来代替. 当什么时候. 下面我觉一个简单的例子: import java.u ...

  7. NSUserDefaults数据存储

    前言 用来保存应用程序设置和属性.用户保存的数据.用户再次打开程序或开机后这些数据仍然存在. 如果往 userDefaults 里存了一个可变数组,取出来的时候这个数组对象就变成了不可变的. NSUs ...

  8. MySQL参数log_bin_trust_function_creators

    问题:执行创建函数的sql文件报错如下: [Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA ...

  9. time、random以及序列化模块

    一. time模块 在Python中,通常有这几种方式来表示时间: 时间戳(timestamp):通常来说,时间戳表示的是从1970年1月1日00:00:00开始按秒计算的偏移量.我们运行“type( ...

  10. 洛谷 P2677 超级书架 2 题解

    传送门 题目描述 Farmer John最近为奶牛们的图书馆添置了一个巨大的书架,尽管它是如此的大,但它还是几乎瞬间就被各种各样的书塞满了.现在,只有书架的顶上还留有一点空间. 所有N(1 <= ...