1 服务端配置
1.ubuntu 16.04默认已安装rsync,rsync服务默认不是启动的,我们要修改rsync文件
sudo vim /etc/default/rsync
修改配置项:
RSYNC_ENABLE=true #false改true
2.创建配置文件
默认示例配置文件:/usr/share/doc/rsync/examples/rsyncd.conf,经测试发生很多错误,可以用下面的配置:/etc/rsyncd.conf
uid = nobody
gid = nogroup
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors = no
read only = false
list = false
auth users = star
secrets file = /etc/rsyncd.secrets
log file = /var/log/rsyncd.log
[public]
comment = welcome to oldboyedu backup!
path = /tmp/public
注意: 主机访问控制,allow 比 deny 优先级高,也就是说 rsync 不关注 allow 和 deny 的顺序。如果主机既不在 hosts allow 也不在 hosts deny,则根据默认策略,允许连接
read only = true | false 如果为true,不能上载到服务端存储
write only = true | false 如果为true,则不能下载到客户端
3.创建/etc/rsyncd.secrets
文件,添加如下内容(每行一个用户,可添加多个):
star:123456
4.修改rsyncd.secrets
文件权限,必不可少步骤
sudo chmod 600 /etc/rsyncd.secrets
5.设置开机启动
sudo systemctl enable rsync
6.启动rsync
sudo systemctl start rsync
7.检查端口,如果端口未被监听,可尝试重启系统(没有netstat
工具,可使用sudo apt install net-tools
安装):
netstat -anop | grep 873
2 客户端
客户端不需要进行特殊的配置,直接同步即可
rsync -vzrtopg --progress 172.16.29.10::ftp ./
#./为当前目录,在服务端touch一个文件,同步后就会出现,
#当然你也可以指定一个位置比如/databk,需注意所建立的权限!
注意:
如果你的配置文件中配置了auth users,客户端同步文件命令需指定账号,如下:
rsync -vzrtopg –progress 账号@172.16.29.10::ftp ./
3 配置文件说明
字段解释:
# 最大连接数
max connections = 10
# pid文件的存放位置
pidfile = /var/run/rsyncd.pid
# 支持max connections参数的锁文件
lock file = /var/lock/rsyncd.lock
# rsync启动时欢迎信息页面文件位置(文件内容自定义),非必须
# motd file = /etc/rsyncd.motd
# 用户认证配置文件,里面保存用户名称和密码,后面会创建这个文件。这个文件也可以不要,但是安全性上可能会有些问题。
secrets file = /etc/rsyncd.secrets
# 日志文件位置,启动rsync后自动产生这个文件,无需提前创建
log file = /var/log/rsyncd.log
# 记录传输文件日志
transfer logging = yes
# 日志文件格式
log format = %t %a %m %f %b
# 剔除某些文件或目录,不同步
# exclude = lost+found/
# 超时时间
timeout = 300
# 某些情况下避免中文乱码
# charset = GB18030
# 默认为yes,修改为no时增加对目录文件软连接的备份,允许 chroot,提升安全性,客户端连接模块,首先 chroot 到模块 path 参数指定的目录下,chroot 为 yes 时必须使用 root 权限,且不能备份 path 路径外的链接文件
use chroot = yes
# 设置不需要压缩的文件
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
# 设置rsync运行属主权限为root,此权限应该是模块名称对应目录的写入用户
uid = nobody
# 设置rsync运行属组权限为nogroup,此权限应该是模块名称对应目录的写入用户
gid = nogroup
# 模块名
[docker]
# 注释
comment = Mini Docker
# rsync服务端数据目录路径
path = /home/docker
# 白名单,允许的主机列表
hosts allow = 10.6.6.0/24
# 黑名单,禁止的主机列表
hosts deny = *
# 设置rsync服务端文件是否只读
read only = no
# 是否显示rsync服务端资源列表
list = yes
# 模块验证的用户名称,可使用空格或者逗号隔开多个用户名,必须和 rsyncd.secrets中的用户名对应
auth users = sworld
# 忽略错误
# ignore errors
日志文件格式选项说明:
%h: 远程主机名
%a: 远程IP地址
%l: 文件长度字符数
%p: 该次rsync会话的进程id
%o: 操作类型:"send"或"recv"、”del.”
%f: 文件名
%P: 模块路径
%m: 模块名
%t: 当前时间
%u: 认证的用户名(匿名时是null)
%b: 实际传输的字节数
%c: 当发送文件时,该字段记录该文件的校验码