title: rsync同步文件
categories: Linux
tags: [rsync, 同步]
date: 2017-05-24 14:58:17
comments: false
rsync同步
安装 rsync
先略过,服务器和客户端都默认安装了rsync
启动rsync服务
-
编辑 /etc/xinetd.d/rsync 文件,将其中的 disable=yes 改为 disable=no
[root@localhost ~]# vi /etc/xinetd.d/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 }
-
重启 xinetd 服务
[root@localhost ~]# /etc/init.d/xinetd restart Stopping xinetd: [ OK ] Starting xinetd: [ OK ]
编辑 rsync 配置文件
-
创建配置文件,默认安装好 rsync 程序后,并不会自动创建rsync的主配置文件,需要手工来创建,其主配置文件为**/etc/rsyncd.conf**,创建该文件并插入如下内容:
[root@localhost ~]# vi /etc/rsyncd.conf uid=root gid=root max connections=4 log file=/var/log/rsyncd.log pid file=/var/run/rsyncd.pid lock file=/var/run/rsyncd.lock secrets file=/etc/rsyncd.passwd hosts deny=172.16.78.0/22 [www] comment= backup web path=/home/rsync_test read only = no exclude=test auth users=root
- www : 同步模块,客户端同步命令中需要制定的模块
- secrets file : 密码文件,同步命令后需要输入对应用户的密码
- path : 该模块的同步路径
-
创建 密码文件,采用这种方式不能使用系统用户对客户端进行认证,所以需要创建一个密码文件,其格式为“username:password”,用户名可以和密码可以随便定义,最好不要和系统帐户一致,同时要把创建的密码文件权限设置为600
[root@localhost ~]# vi /etc/rsyncd.passwd root:abc123 [root@localhost ~]# chmod 600 /etc/rsyncd.passwd
-
重启 xinetd 服务
-
客户端使用命令同步,然后输入密码 abc123 ,同步成功后
MacBook-Pro:~ wilker$ rsync -avz -e "ssh -p 23333" --progress --delete root@114.114.114.114::www ~/Desktop/rsync_test Password:
receiving file list …
2 files to consider
./
test.lua
13 100% 12.70kB/s 0:00:00 (xfer#1, to-check=0/2)
sent 88 bytes received 199 bytes 11.71 bytes/sec
total size is 13 speedup is 0.05
直接指定密码文件的命名
```shell
MacBook-Pro:~ wilker$ rsync -avz -e "ssh -p 23333" --progress --timeout=3000 --password-file=/Users/wilker/Desktop/rsync_pwd.lua --delete root@114.114.114.114::www ~/Desktop/rsync_test
receiving file list ...
2 files to consider
sent 60 bytes received 137 bytes 56.29 bytes/sec
total size is 13 speedup is 0.07
rsync_pwd.lua 文件内容是密码
abc123
踩到的小坑
-
模块配置不正确,报错:
@ERROR: Unknown module ‘tee_nonexists'
- 需要修改为正确的模块,如配置文件 /etc/rsyncd.conf 中的 [www] 模块,同步命令中则为 root@114.114.114.114::www
-
服务端目录不存在,报错:
@ERROR: chroot failed
- 创建同步目录 及 分配权限
-
输入 rsync 的密码错误:报错:
@ERROR: auth failed on module www
- 输入正确的密码,在配置文件 **/etc/rsyncd.passwd ** 中的 abc123
-
服务端的ssh公钥认证报错:
Permission denied (publickey,gssapi-keyex,gssapi-with-mic)
-
编辑ssh的配置文件 /etc/ssh/sshd_config,开启下面几项
[root@localhost ~]# vi /etc/ssh/sshd_config ... RSAAuthentication yes PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys # 这个为ssh公钥
-
重启ssh服务
[root@localhost ~]# /etc/init.d/sshd restart Stopping sshd: [ OK ] Starting sshd: [ OK ]
-
-
客户端使用命令指定了密码文件,报错:
password file must not be other-accessible
-
客户度的密码文件 /Users/wilker/Desktop/rsync_pwd.lua 需要 600 权限,不能被其他人访问
MacBook-Pro:Desktop wilker$ chmod 600 rsync_pwd.lua
-
参考资料
- http://man.linuxde.net/rsync
- 参数详解:http://linuxwiki.github.io/Services/rsync.html#52ssh22
- 常见错误:http://www.jb51.net/article/31920.htm
- ssh问题:http://laowafang.blog.51cto.com/251518/1364298/
- 详细配置:http://www.cnblogs.com/itech/archive/2009/08/10/1542945.html
推送、拉去
-
ssh方式
rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST # 执行“推”操作 rsync [OPTION]... [USER@]HOST:SRC [DEST] # 执行“拉”操作
-
rsync C/S 方式
rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST # 执行“推”操作 rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST # 执行“推”操作 rsync [OPTION]... [USER@]HOST::SRC [DEST] # 执行“拉”操作 ersync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST] # 执行“拉”操作
常用选项
-
-v
: Verbose (try -vv for more detailed information) # 详细模式显示 -
-e
“ssh options” : specify the ssh as remote shell # 指定ssh作为远程shell -
-a
: archive mode # 归档模式,表示以递归方式传输文件,并保持所有文件属性,等于-rlptgoD -
-r
(–recursive) : 目录递归 -
-l
(–links) :保留软链接 -
-p
(–perms) :保留文件权限 -
-t
(–times) :保留文件时间信息 -
-g
(–group) :保留属组信息 -
-o
(–owner) :保留文件属主信息 -
-D
(–devices) :保留设备文件信息 -
-z
: 压缩文件 -
-h
: 以可读方式输出 -
-H
: 复制硬链接 -
-X
: 保留扩展属性 -
-A
: 保留ACL属性 -
-n
: 只测试输出而不正真执行命令,推荐使用,特别防止--delete
误删除! -
--stats
: 输出文件传输的状态 -
--progress
: 输出文件传输的进度 -
––exclude=PATTERN
: 指定排除一个不需要传输的文件匹配模式 -
––exclude-from=FILE
: 从 FILE 中读取排除规则 -
––include=PATTERN
: 指定需要传输的文件匹配模式 -
––include-from=FILE
: 从 FILE 中读取包含规则 -
--numeric-ids
: 不映射 uid/gid 到 user/group 的名字 -
-S, --sparse
: 对稀疏文件进行特殊处理以节省DST的空间 -
--delete
: 删除DST中SRC没有的文件,也就是所谓的镜像[mirror]备份
Ubuntu16.04 的启动方式
Ubuntu 启动 rsync 服务
Ubuntu 已经安装了 rsync, 只是默认不启动
-
修改配置文件
vi /etc/default/rsync
# vi /etc/default/rsync RSYNC_ENABLE=false # 改成 RSYNC_ENABLE=true
-
拷贝一个示例配置文件到
/etc
下# cp /usr/share/doc/rsync/examples/rsyncd.conf /etc
-
不是一定要到这个目录下, 只是启动命令默认会找
rsyncd.conf
, 启动命令也可以指定--config
指定配置文件路径 -
然后修改配置文件
/etc/rsyncd.conf
, 我的配置# vi /etc/rsyncd.conf pid file=/var/run/rsyncd.pid use chroot = no max connections=1200 uid = root gid = root strict modes = yes ignore errors = no ignore nonreadable = yes transfer logging = no timeout = 600 refuse options = checksum dry-run dont compress = *.gz *.tgz *.zip *.z *.rpm *.deb *.iso *.bz2 *.tbz [Family] comment = wolegequ path = /nas/Family read only = no list = yes lock file = /var/lock/rsyncd_Family [Threejs] comment = wolegequ path = /nas/home_xuan/Threejs read only = no list = yes lock file = /var/lock/rsyncd_Threejs [Public] comment = wolegequ path = /nas/Public read only = no list = yes lock file = /var/lock/rsyncd_Public
-
-
启动 rsync 服务
# /etc/init.d/rsync start # 启动 # /etc/init.d/rsync stop # 停止
-
查看一下是否运行中
# ps auxw | grep rsync root 834 0.0 0.0 15800 944 ? S 15:30 0:00 /usr/bin/rsync --no-detach --daemon --config /etc/rsyncd.conf
-
也可以这样启动
# /usr/bin/rsync --daemon --config=/etc/rsyncd/rsyncd.conf
-
-
done
-
客户端实测一下
# rsync -avzhe "ssh -p 32888" --delete --progress -O root@192.168.2.222::Public /mnt/j/Public receiving incremental file list ./ sent 37 bytes received 131 bytes 48.00 bytes/sec total size is 4.63M speedup is 27,578.10
- 这里的
Public
就是/etc/rsyncd.conf
中的[Public]
-avzhe
中的e
是指使用的是 ssh 的方式.
- 这里的
踩坑
-
rsync: mkstemp “xxx” failed: Operation not permitted (1)
同步时本地会新建一个临时目录, 由于权限不够才报这个错. 使用命令时加上
sudo
或者直接切到超级用户roo
下操作.
切到root
操作会方便很多. -
rsync: failed to set times on “xxx”: Operation not permitted (1)
参考: https://codeday.me/bug/20170520/16939.html
如果/ foo / bar在NFS(或可能是某些FUSE文件系统)上,则可能是问题。加上
-O
参数, 如:rsync -avzhe "ssh -p 32888" --delete --progress -O root@192.168.2.222::Public /mnt/j/Public
包含 排除 文件
参考:
- rsync详解之exclude排除文件 - https://blog.51cto.com/wanwentao/582432