Rsync 笔记

安装

1
yum install rsync

服务端配置

编辑配置文件 /etc/rsyncd.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 配置用户和用户组
uid = 0
gid = 0
# 允许chroot,提升安全性,客户端连接模块,首先chroot到模块path参数指定的目录下,chroot为yes时必须使用root权限,且不能备份path路径外的链接文件
use chroot = yes
# 设定白名单,可以指定IP段(172.18.50.1/255.255.255.0),各个Ip段用空格分开
# 例: hosts allow = 172.18.50.110 172.18.50.111 20.0.0.0/24
hosts allow = 10.10.20.102
hosts deny = *
# 端口号配置可不配默认873
port 873
# 允许的客户端最大连接数
max connections = 4
# 记录传输文件日志
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

[gitlab]
# 模块的根目录,同步目录,要注意权限
path = /data/gitlab/logs/
# 配置权限 (no=写,yes=只读)
read only = no
# 是否允许列出模块内容
list = no
# 忽略错误
ignore errors
# 添加注释
comment = ftp export area
# 模块验证的用户名称,可使用空格或者逗号隔开多个用户名
auth users = gitlab
# 模块验证密码文件 可放在全局配置里
secrets file = /etc/rsync/rsyncd.secrets
# 剔除某些文件或目录,不同步
include=huifu*.txt
exclude=*

配置密码文件

模块验证密码文件 可放在全局配置里 (注意:密码里的用户名要与主配置文件里的用户匹配)

secrets file = /etc/rsync/rsyncd.secrets

1
2
echo "gitlab:password" >> /etc/rsync/rsyncd.secrets
chmod 600 /etc/rsync/rsyncd.secrets

启动 rsync 服务

1
systemctl start rsyncd && systemctl enable rsyncd

客户端使用

只读取 rsync 内容不同步

1
rsync --list-only rsync@192.168.1.1::xuexi

使用密码文件同步

1
rsync --list-only rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps

windows 下 rsync 的路径表示

1
/cygdrive/c/windows/system

rsync 拉取到本地

1
rsync -auv rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps /cygdrive/c/windows/system

rsync 推送到远程

1
rsync -auv /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps 

二终端是否保持一致

–delete

1
rsync -auv --delete /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps 

排除文件及文件夹

–exclude 多个排除可以用 {‘排除一’,’排除二’,’排除三’}
–exclude-from ‘/home/backup/exclude.txt’

1
2
3
4
rsync -auv --exclude={'.svn','zingnext-srm/site'} /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps 

# exclude.txt 每行一个排除项
rsync -auv --exclude-from '/home/backup/exclude.txt' /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps

指定要同步的文件及文件夹

–include

1
rsync -auv --include="system*.txt" --exclude="*" /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps 

显示同步进度

–progress

1
rsync -auv --progress /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps

指定同步端口

1
rsync -auv --port=8730 /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps

不要在目标终端上新建文件–existing

1
rsync -auv --existing /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps

常用同步项

1
2
3
4
5
6
7
8
9
10
#-a 代表:递归模式  保存符号链接  保留权限  保留时间戳  保存所有者和组
#-u 同步时不覆盖目的文件,只做更新
#-v 显示详细信息
#-z 传输时启用压缩
#-W 按块传输整个文件,传送小文件会很合适。

rsync -auv /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps

#-d 仅同步目录权
rsync -auv -d /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps

指定传输文件大小

–max-size=”50K” 使 rsync 仅传输小于或等于 50K 的文件。您可以将 M 表示为兆字节,G 表示千兆字节。

1
rsync -auv --max-size="50K" /cygdrive/c/windows/system rsync@192.168.1.1::xuexi --password-file=d:\ttt\rsync.ps

附件

cwrsync 服务端和客户端