rsync同步笔记

发布于 分类 Linux

更新于 2017-10-12

用法

rsync src dest

这是最简单的用法,表示同步src到dest。(即,执行之后,dest的文件与src的相同,以src的为准)

rsycn的服务端为服务器的文件接收端,rsycn的客户端为服务器的文件推动端。

示例

node1 192.168.1.101

node2 192.168.1.102

可以创建一些基础数据,用作示范

mkdir -p /www/test1/
echo node1-1 > /www/test1/node1-1.txt
echo node1-2 > /www/test1/node1-2.txt
echo node1-3 > /www/test1/node1-3.txt
echo node1-4 > /www/test1/node1-4.txt

mkdir -p /www/test2/
echo node2-6 > /www/test2/node2-6.txt
echo node2-7 > /www/test2/node2-7.txt
echo node2-8 > /www/test2/node2-8.txt
echo node2-9 > /www/test2/node2-9.txt

SSH方式

在node1下

同步node1数据到node2,所有数据

rsync -vzacu /www/test1/ root@192.168.1.102:/www/test2/ -e"ssh -p 22"

同步node1数据到node2,排除指定数据

rsync -vzacu /www/test1/ root@192.168.1.102:/www/test2/ --exclude"node1-2.txt" -e"ssh -p 22"

下载node2数据到node1,所有数据

rsync -vzacu root@192.168.1.102:/www/test2/ -e"ssh -p 22" /www/test1/

双向认证方式

将要实现的结果:在node1下执行将node2数据同步到node1

在node2下编辑

vi /etc/rsyncd.conf

写入

# /etc/rsyncd: configuration file for rsync daemon mode
# See rsyncd.conf man page for more options.
# configuration example:

uid = root
use chroot = no
max connections = 4
pid file = /var/run/rsyncd.pid
log file = /var/log/rsync.log

#模块名称
[test]
#同步目录
path =/www/test2
ignore errors
read only = true
list = false
#允许那个客户端来同步
hosts allow = 192.168.1.101
#拒绝客户端,这里表示不拒绝
hosts deny = 0.0.0.0/32
#同步文件里面的同步账户,可以随便设置,但必须和/etc/rsync.pass里的账户一致
auth users = rsync
secrets file = /etc/rsync.pass 

编辑帐号和密码

vi /etc/rsync.pass

写入帐号:密码,每行一个

rsync:123456

赋予权限

chmod 600 /etc/rsync.pass

还差一步

rsync --daemon --config /etc/rsyncd.conf

在node1下编辑

vi /etc/rsync_client.pass

写入密码,仅需输入密码

123456

赋予权限

chmod 600 /etc/rsync_client.pass

在node1下执行同步命令

rsync -avzp --password-file=/etc/rsync_client.pass rsync@192.168.1.102::test /www/test1

注意事项

真实案例1,报错:(我在node2重启后出现的该问题)

rsync: failed to connect to xx.xx.xx.xx (xx.xx.xx.xx): Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(122) [sender=3.0.9] 
执行
rsync --daemon --config /etc/rsyncd.conf

真实案例2,报错:

@ERROR: auth failed on module xxxxx
rsync error: error starting client-server protocol (code 5) at main.c(1516) [sender=3.0.9] 

检查node1,node2用户以及密码是否一致

-- The End --

本文标题: rsync同步笔记

本文地址: https://seonoco.com/blog/linux-rsync-note

本页面显示内容已针对移动端进行优化,点击查看完整版本