安装
- CentOS7
rpm -ivh rsync-3.1.2-10.el7.x86_64.rpm - CentOS6
rpm -ivh rsync-3.0.6-12.el6.x86_64.rpm
配置
配置文件是 /etc/rsyncd.conf
服务器
建立要同步的目录
mkdir /home/rsync_s设置访问用户和口令
echo "rsyncuser:every123098" >> /etc/rsync.password
chmod 600 /etc/rsync.passwordvi /etc/rsyncd.conf
# 运行RSYNC守护进程的用户
uid = root
# 运行RSYNC守护进程的组
gid = root
# 默认端口
# port = 873
# 服务器IP地址
# address = 10.28.204.65
# 进程启动后,进程号存放路径
# pid file = /var/run/rsyncd.pid
# 指定rsync的日志文件
log file = /var/log/rsyncd.log
日志会非常大,如果不想输出,可以更改为:
log file = /dev/null
# 不使用 chroot
use chroot = no
# yes 只读,不让客户端上传文件到服务器
read only = no
# 将传输操作记录到传输日志文件
transfer logging = yes
# 允许哪些主机访问(多个以空格隔开)
# hosts allow = 10.28.204.66
hosts allow = *
# 拒绝哪些主机访问
# hosts deny= *
# 最大连接数
max connections = 100
# 登陆欢迎信息(生产环境不建议)
# motd file = /etc/rsyncd.motd
# 指定日志记录的格式
log format = %t %a %m %f %b
# 消息级别
syslog facility = local3
# 会话超时时间
timeout = 600
# 模块的名称,可以自定义
[test_1]
# 需要同步的目录
path = /home/rsync_s
# 是否允许用户列出文件,默认为 true
list=yes
# 忽略错误信息
ignore errors
# 不同步的目录(多个以空格隔开)
# exclude = myrenwole/
# 注释内容,任意
comment = test rsync
# 那些用户才允许连接该模块,多个以,隔开
auth users = rsyncuser
# 认证时所需的密码文件
secrets file = /etc/rsync.password
- 放开防火墙
CentOS7
firewall-cmd --add-port=873/tcp --permanent
firewall-cmd --add-port=873/udp --permanent
firewall-cmd --reloadCentOS6
iptables -I INPUT -p tcp --dport 873 -j ACCEPT
iptables -I INPUT -p udp --dport 873 -j ACCEPT
/etc/rc.d/init.d/iptables save
- 关闭 selinux
vi /etc/sysconfig/selinux
SELINUX=permissive
客户端
建立要同步的目录
mkdir -p /home/rsync_c/a设置口令
echo "every123098" >> /etc/rsync_client.pass
chmod 600 /etc/rsync_client.pass
启动
默认会占用 873 端口
服务器端
CentOS7
systemctl start rsyncdCentOS6
/usr/bin/rsync --daemon
客户端
推送
rsync -avz --password-file=/etc/rsync_client.pass /home/rsync_c/ rsyncuser@<服务端 IP>::test_1拉取
rsync -avzrtopg --progress --delete --password-file=/etc/rsync_client.pass rsyncuser@<服务端 IP>::test_1 /home/rsync_c添加计划任务
crontab -e
* * * * * rsync -avz --password-file=/etc/rsync_client.pass /home/rsync_c/ rsyncuser@<服务端 IP>::test_1
例如:
* * * * * flock -xn /var/lock/emails.lock -c "rsync -avz --password-file=/etc/rsync_client.pass /home/mails/emails rsyncuser@192.168.1.242::mails_emails"