一、安装配置
1、安装服务端
# yum -y install rsync
# vim /etc/rsyncd.conf
# 全局设置
strict mood = yes # 是否检查secrete文件权限,若其他人可读,则无法同步
port = 873 # 默认开的端口
uid = root # 用户
gid = root # 用户组
use chroot = no # 不允许改变程序执行时所参考的根目录位置
max connections = 4 # 最大连接数
log format = %o %h [%a] %m (%u) %f %l # 指定日志记录的格式
%h 远程主机名
%a 远程IP地址
%l 文件长度字符数
%p 该次rsync会话的进程id
%o 操作类型:"send"或"recv"
%f 文件名
%P 模块路径
%m 模块名
%t 当前时间
%u 认证的用户名(匿名时是null)
%b 实际传输的字节数
%c 当发送文件时,该字段记录该文件的校验码
pid file = /var/run/rsyncd.pid # pid文件路径
lock file = /var/rsyncd.lock # look文件路径
log file = /var/log/rsyncd.log # 日志文件路径
transfer logging = yes # 开启传输日志
timeout = 900 # 连接超时时间
ignore nonreadable = yes # 忽略不可读的文件
ignore errors # I/O错误时,不删除目标文件,忽略错误
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2 # 不压缩传输
motd file =/etc/rsyncd.motd # 客户端登陆之后弹出的消息,需要创建
fake super = yes # 将rsync虚拟用户伪装成为一个超级管理员用户
# 项目设置
[test]
common = # 同步描述
path = /alidata/www/default # 源文件路径
read only = yes # yes不允许上传
write only = no # no允许下载
list = false # 是否允许客户端可以查看可用模块列表,默认为可以
hosts allow = 10.161.140.46/24 # 允许客户端地址
hosts deny= * # 拒绝客户端地址
auth users = root # 客户端用户
secrets file = /etc/rsyncd.password # 客户端密码文件,该文件权限要求为只读,建议为600,仅在设置auth users后有效
exclude = test/ # 指定目录下test目录可以不同步数据
# 模块设置
[backup]
path = /backup
[rsync]
path = /rsync
# systemctl start rsyncd
# echo "rsync:redhat" >>/etc/rsync.password
# chown -R rsync.rsync /etc/rsync.password
# chmod 600 /etc/rsync.password
2、安装客户端
# yum -y install rsync
# echo "redhat" >>/etc/rsync.password # 只需要密码即可
# chown -R rsync.rsync /etc/rsync.password
# chmod 600 /etc/rsync.password
3、同步文件
# rsync -avzP /root/test/ rsync@192.168.110.128::backup
# rsync -avzP /root/test/ rsync@192.168.110.128::backup --password-file=/etc/rsynclient.password
二、例子
推送:rsync -avrP --delete --password-file=/etc/rsyncd.password root@192.168.101.89::backup /tmp/ # backup 为模块名
拉取:rsync -avrP --delete --password-file=/etc/rsyncd.password rsync://root@192.168.101.89/test /tmp/
拷贝本地文件:rsync -a /data/ /backup
拷贝远程到本地:rsync -av root@192.168.110.128::test /opt/lsyncd
拷贝本地到远程:rsync -av /opt/lsyncd/ root@192.168.110.128::test