Rsync 配置和使用

Rsync 远程同步主要有两种方式:使用远程shell(ssh或rsh) 或使用rsync的daemon方式

常用参数

-v :展示详细的同步信息
-a :归档模式,相当于 -rlptgoD
    -r :递归目录
    -l :同步软连接文件
    -p :保留权限
    -t :将源文件的"modify time"同步到目标机器
    -g :保持文件属组
    -o :保持文件属主
    -D :和--devices --specials一样,保持设备文件和特殊文件
-z :发送数据前,先压缩再传输
-H :保持硬链接
-n :进行试运行,不作任何更改
-P same as --partial --progress
    --partial :支持断点续传
    --progress :展示传输的进度
--delete :如果源文件消失,目标文件也会被删除
--delete-excluded :指定要在目的端删除的文件
--delete-after :默认情况下,rsync是先清理目的端的文件再开始数据同步;如果使用此选项,则rsync会先进行数据同步,都完成后再删除那些需要清理的文件。
--exclude=PATTERN :排除匹配PATTERN的文件
--exclude-from=FILE :如果要排除的文件很多,可以统一写在某一文件中
-e ssh :使用SSH加密隧道传输

本地文件同步

[root@sync opt]# rsync -avH /opt/resource/ /tmp/desc/
# 如果没有desc目录,会自动创建

远程文件同步–shell方式

# 从本地传到远端,目标文件会被写成ssh登录用户的属组和属主(如下 www)
[root@sync opt]# rsync -avH /opt/nginx-1.12.1/ www@172.18.50.125:/tmp/nginx/
# 使用 ssh 加密隧道方式传输,保障数据的安全性
[root@sync opt]# rsync -avHe ssh /opt/nginx-1.12.1/ www@172.18.50.125:/tmp/nginx/

# 从远端传到本地,只要对目标文件有读的权限,就可以同步到本地
[root@sync opt]# rsync -avH www@172.18.50.125:/tmp/nginx/ /tmp/nginx/

# 如果远程服务器ssh端口不是默认的22
[root@sync opt]# rsync -avHe "ssh -p 11222" /opt/nginx-1.12.1/ www@172.18.50.125:/tmp/nginx/

远程文件同步–daemon方式
环境

服务器A: 172.18.50.125(daemon)
服务器B: 172.18.50.110
系统: Centos 7
Rsync版本:3.0.9

创建 rsync 服务的目录和配置文件

[root@50_125 ~]# mkdir /etc/rsync 
[root@50_125 ~]# cd /etc/rsync
[root@50_125 rsync]# touch rsyncd.conf
[root@50_125 rsync]# touch rsyncd.secrets
[root@50_125 rsync]# touch rsyncd.motd
[root@50_125 rsync]# chmod 600 rsyncd.secrets 

创建 rsync 服务的目录和配置文件

[root@50_125 ~]# mkdir /etc/rsync 
[root@50_125 ~]# cd /etc/rsync
[root@50_125 rsync]# touch rsyncd.conf
[root@50_125 rsync]# touch rsyncd.secrets
[root@50_125 rsync]# touch rsyncd.motd
[root@50_125 rsync]# chmod 600 rsyncd.secrets 

服务端配置
rsyncd.conf

### rsyncd.conf 文件的配置
[root@50_125 rsync]# vim rsyncd.conf
# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# 传输文件使用的用户和用户组,如果是从服务器=>客户端,要保证www用户对文件有读取的权限;如果是从客户端=>服务端,要保证www对文件有写权限。
uid = www
gid = www
# 允许chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下,chroot为yes时必须使用root权限,且不能备份path路径外的链接文件
use chroot = yes
# 只读
read only = no
# 只写
write only = no
# 设定白名单,可以指定IP段(172.18.50.1/255.255.255.0),各个Ip段用空格分开
hosts allow = 172.18.50.110 172.18.50.111
hosts deny = *
# 允许的客户端最大连接数
max connections = 4
# 欢迎文件的路径,非必须
motd file = /etc/rsync/rsyncd.motd
# pid文件路径
pid file = /var/run/rsyncd.pid
# 记录传输文件日志
transfer logging = yes
# 日志文件格式
log format = %t %a %m %f %b
# 指定日志文件
log file = /var/log/rsync.log
# 剔除某些文件或目录,不同步
exclude = lost+found/
# 设置超时时间
timeout = 900
ignore nonreadable = yes
# 设置不需要压缩的文件
dont compress   = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2

# 模块,可以配置多个,使用如: sate@172.18.50.125::125to110
[125to110]
# 模块的根目录,同步目录,要注意权限
path = /tmp/nginx
# 是否允许列出模块内容
list = no
# 忽略错误
ignore errors
# 添加注释
comment = ftp export area
# 模块验证的用户名称,可使用空格或者逗号隔开多个用户名
auth users = sate
# 模块验证密码文件 可放在全局配置里
secrets file = /etc/rsync/rsyncd.secrets
# 剔除某些文件或目录,不同步
exclude = lost+found/ conf/ man/

rsyncd.secrets

### rsyncd.secrets 文件的配置
[root@50_125 rsync]# cat rsyncd.secrets 
# 用户名:密码
sate:111111

rsyncd.motd

### rsyncd.motd  文件配置
[root@50_125 rsync]# cat rsyncd.motd 
++++++++++++++++++
sate zheng : rsync start
++++++++++++++++++

启动

[root@50_125 rsync]# rsync --daemon --config=/etc/rsync/rsyncd.conf

客户端配置
从 服务端=>客户端 同步数据

# 会提示输入密码
[root@50_110 ~]# rsync -avzP --delete sate@172.18.50.125::125to110 /tmp/sync/

从 客户端=>服务端 同步数据

# 会提示输入密码
[root@50_110 ~]# rsync -avzP --delete /tmp/sync/ sate@172.18.50.125::125to110

注: 如果是 /tmp/sync,则同步sync目录;如果 /tmp/sync/,则同步sync目录下的文件

问题:–exclude 排除文件目录时,如果有多个同名目录的情况

# 目录结构
[root@WJ110 sync]# tree
.
├── dir1
│   └── test
│       ├── 3.file
│       ├── 4.file
│       └── 5.file
├── dir2
└── test
    ├── 1.file
    ├── 2.file
    └── 3.file

# 情况一 : 排除 /test 目录,同步其他目录(同步的是/tmp/sync/ 下边的文件)
[root@WJ110 sync]# rsync -avP --delete --password-file=/tmp/secrets.file --exclude=test  /tmp/sync/ sate@172.18.50.125::125to110 
sending incremental file list
./
dir1/
dir2/
# 会发现,该目录下所有 test 目录都被排除了,如果想只排除第一层目录的 test,可以如下(/ 代表所同步目录第一层):
[root@WJ110 sync]# rsync -avP --delete --password-file=/tmp/secrets.file --exclude=/test/  /tmp/sync/ sate@172.18.50.125::125to110 
sending incremental file list
./
dir1/
dir1/test/
dir1/test/3.file
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/7)
dir1/test/4.file
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=1/7)
dir1/test/5.file
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=0/7)
dir2/

sent 267 bytes  received 80 bytes  694.00 bytes/sec
total size is 0  speedup is 0.00

# 情况二 : 和情况一不同的是 同步的 /tmp/sync 这个目录(同步的是/tmp/sync 目录本身,导致 exclude 后边的参数也会变化)
[root@WJ110 sync]# rsync -avP --delete --password-file=/tmp/secrets.file --exclude=/sync/test/  /tmp/sync sate@172.18.50.125::125to110 
sending incremental file list
sync/
sync/dir1/
sync/dir1/test/
sync/dir1/test/3.file
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/7)
sync/dir1/test/4.file
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=1/7)
sync/dir1/test/5.file
           0 100%    0.00kB/s    0:00:00 (xfer#3, to-check=0/7)
sync/dir2/

sent 282 bytes  received 81 bytes  726.00 bytes/sec
total size is 0  speedup is 0.00

免密码同步

# 将密码写到文件,再通过 --password-file 指定该文件,注:该文件的权限必须是 600
[root@50_110 ~]# echo "111111" > /tmp/secrets.file
[root@50_110 ~]# chmod 600 /tmp/secrets.file
[root@50_110 ~]# rsync -avzP --delete --password-file=/tmp/secrets.file sate@172.18.50.125::125to110 /tmp/sync/

参考文件

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值