1、认识

Rsync(remote synchronize)是一个远程数据同步工具,可通过LAN/WAN快速同步多台主机间的文件。Rsync使用所谓的“Rsync算法”来使本地和远 程两个主机之间的文件达到同步,这个算法只传送两个文件的不同部分,而不是每次都整份传送,因此速度相当快

2、原理

Rsync本来是用于替代rcp的一个工具,目前由rsync.samba.org维护,所以rsync.conf文件的格式类似于samba的主配 置文件;Rsync可以通过rsh或ssh使用,也能以daemon模式去运行

在以daemon方式运行时Rsync server会打开一个873 端口,等待客户端去连接。连接时,Rsync server会检查口令是否相符,若通过口令查核,则可以开始进行文件传输。第一次连通完成时,会把整份文件传输一次,以后则就只需进行增量备份

3、特点

1、可以镜像保存整个目录树和文件系统;

2、可以很容易做到保持原来文件的权限、时间、软硬链接等;

3、无须特殊权限即可安装;

4、优化的流程,文件传输效率高;

5、可以使用rsh、ssh等方式来传输文件,当然也可以通过直接的socket连接;

6、支持匿名传输

rsync命令

1
2
3
4
5
-v		:显示rsync过程中详细信息。可以使用"-vvvv"获取更详细信息。
-a --archive :归档模式,表示递归传输并保持文件属性。等同于"-rtopgDl"。
-r --recursive :递归到目录中去。
-t --times :保持mtime属性。强烈建议任何时候都加上"-t",否则目标文件mtime会设置为系统时间,导致下次更新
-z :传输时进行压缩提高效率。

最常用的选项组合是”avz”,即压缩和显示部分信息,并以归档模式传输。

  • linux下使用rsync同步

    1
    2
    3
    4
    #新建脚本
    touch rsync.sh
    #授予执行权限
    chmod +x rsync.sh

    脚本内容如下

    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
    #!/bin/sh

    #安装rsync
    yum -y install rsync

    #修改rsync配置文件
    cat >>/etc/rsyncd.conf<<MUL
    uid = root
    gid = root
    use chroot = no
    max connections = 100
    address=127.0.0.1
    port=8173
    log file = /var/log/rsyncd.log
    pid file = /var/run/rsyncd.pid
    lock file = /var/run/rsync.lock
    MUL

    #获取本地ip,替换address为本地ip
    eth0_ip=`ip addr |grep inet |grep eth0|awk '{print $2}' |awk -F "/" '{print $1}'`
    sed -i "s/127.0.0.1/$eth0_ip/g" /etc/rsyncd.conf

    #启动rsync
    /usr/bin/rsync --daemon
    cat>>/etc/rc.local<<'MUL'
    /usr/bin/rsync --daemon
    MUL

    #定义密钥config供客户端连接
    rsync_datalogs=`grep -rn "/data/logs" /etc/rsyncd.conf |wc -l`
    if [ "$rsync_datalogs" -eq "0" ] ;then
    cat >>/etc/rsyncd.conf<<'MUL'
    [config]
    path = /data/logs/
    ignore errors = yes
    read only = no
    MUL
    fi
  • windows下使用rsync同步

    server下载地址:Rsync server
    client下载地址:Rsync client

server端可自定义用户名密码

windows server默认安装server端才可出现rsync server服务,windows使用服务来启动和停止rsync

client端直接安装即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#server端配置文件示例

use chroot = false
strict modes = false
hosts allow = *
log file = rsyncd.log
uid = 0 #不指定uid,不加这一行将无法使用任何账户
gid = 0 #不指定gid
max connections = 10 #最大连接数10
port =8173

[config]
path = /cygdrive/e/log
read only = false
transfer logging = yes
lock file = rsyncd.lock
  • 客户端拉取

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #同步语句
    rsync -av rsync://172.17.30.7:8173/config /cygdrive/e/logs

    #端口后的config对应server端的config模块并不是目录,可加入定时任务每1分钟运行
    #/cygdrive/e/logs 因为windows端为盘符,所以用/cygdrive/e/
    #rsync://ip:port/模块 /本地保存目录
    #linux可直接运行rsync拉取,windows需要进入rsync/bin目录下运行或加入环境变量
    rsync -vrt rsync://172.17.30.7:8173/config /cygdrive/e/logs
    #推荐使用-vrt拉取,如果windows同步linux的数据使用-a会保持文件属性,但windows没有root,故会报错

sersync+rsync 实现服务器文件实时双向同步

实现双向同步,不存在服务端与客户端。也可以说,是服务端的同时,也是客户端;他们是相互作用的。

所以我们需要让服务器的rsync服务即可被访问,也可以访问其他服务器的rsync服务;

我们分三步安装。

第一步 , rsync服务端配置。

第二步, rsync客户端配置。

第三步,安装 sersync

安装部署rsync服务端配置(服务端)

1.安装rsync

1
[root@k8s-work3 ~]# yum install rsync -y

2.创建用户认证文件

1
[root@k8s-work3 ~]# echo  "root:123456">/etc/rsync.servicePwd

3.设置用户认证文件权限

1
[root@k8s-work3 ~]# chmod 600 /etc/rsync.servicePwd

4.创建配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
[root@k8s-work3 ~]# vi /etc/rsyncd.conf
#Rsync server
uid = root
gid = root
port = 873
use chroot = no
max connections = 2000
timeout = 600
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log
ignore errors
read only = false
list = false
hosts allow = 192.168.233.10
hosts deny = 0.0.0.0/32
auth users = root
secrets file = /etc/rsync.servicePwd
[www]
comment = www
path = /data/

5.启动守护进程并加入开机自启:

1
2
3
[root@k8s-work3 ~]# rsync --daemon --config=/etc/rsyncd.conf
[root@k8s-work3 ~]# vi /etc/rc.local
/usr/bin/rsync --daemon

6.创建相关待同步的目录

1
2
[root@S1 ~]# mkdir -p /data   #此目录就是与配置文件中 模板www 对应的目录
[root@M1 ~]# touch /data/test.log
rsync客户端配置

1.创建客户端密码文件

1
[root@k8s-work3 ~]# echo "123456">/etc/rsync.clientPwd

2.设置密码文件权限

1
[root@k8s-work3 ~]# chmod 600 /etc/rsync.clientPwd
rsync测试

1.服务器A 作为客户端,从服务器B的模板www中 同步数据

1
2
3
4
5
6
7
[root@k8s-work2 data]# rsync -av  root@192.168.50.54::www/ /data/ --password-file=/etc/rsync.clientPwd
receiving incremental file list
qrcode/
qrcode/permanent_34_service_seniorPartner20201228102732.jpg

sent 51 bytes received 29,081 bytes 19,421.33 bytes/sec
total size is 28,854 speedup is 0.99

2.显示成功,查看

1
2
[root@k8s-work2 data]# ls
qrcode test.log
安装部署sersync服务

1.下载

1
wget https://storage.googleapis.com/google-code-archive-downloads/v2/code.google.com/sersync/sersync2.5_64bit_binary_stable_final.tar.gz

2.配置sersync

1
2
3
4
5
6
7
8
9
10
11
[root@k8s-work3 data]# mkdir /usr/local/sersync/
[root@k8s-work3 data]# cd /usr/local/sersync/
[root@k8s-work3 sersync]# mv /root/sersync2.5_64bit_binary_stable_final.tar.gz .
[root@k8s-work3 sersync]# tar zxvf sersync2.5_64bit_binary_stable_final.tar.gz
GNU-Linux-x86/
GNU-Linux-x86/confxml.xml
GNU-Linux-x86/sersync2
[root@k8s-work3 sersync]# mkdir -p conf bin log
[root@k8s-work3 sersync]# mv GNU-Linux-x86/confxml.xml conf/
[root@k8s-work3 sersync]# cp conf/confxml.xml conf/confxml.xml.bak
[root@k8s-work3 sersync]# mv GNU-Linux-x86/sersync2 bin/

3.修改配置文件

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
vi  /usr/local/sersync/conf/confxml.xml
修改24--28行
24 <localpath watch="/opt/tongbu">
25 <remote ip="127.0.0.1" name="tongbu1"/>
26 <!--<remote ip="192.168.8.39" name="tongbu"/>-->
27 <!--<remote ip="192.168.8.40" name="tongbu"/>-->
28 </localpath>
修改后的内容为: watch:为模块对应的目录。会将服务器 ip:xxxx 模块www下的文件 同步到 watch=“/data/www”下
如果有多个同步模块,则按下面格式依次去写,仅更改
24 <localpath watch="/data">
25 <remote ip="192.168.50.54" name="www"/>
26 <remote ip="192.168.50.55" name="www"/>
27 </localpath>
修改29--35行,认证部分(rsync密码认证)
29 <rsync>
30 <commonParams params="-artuz"/>
31 <auth start="false" users="root" passwordfile="/etc/rsync.pas"/>
32 <userDefinedPort start="false" port="874"/><!-- port=874 -->
33 <timeout start="false" time="100"/><!-- timeout=100 -->
34 <ssh start="false"/>
35 </rsync>
修改后的内容如下:start改完true打开用户认证。 users为服务端配置文件中的users passwordfile密码文件 此处为:/etc/rsync.clientPwd 客户端密码文件
27 <rsync>
28 <commonParams params="-artuz"/>
29 <auth start="true" users="root" passwordfile="/etc/rsync.clientPwd"/>
30 <userDefinedPort start="false" port="874"/><!-- port=874 -->
31 <timeout start="true" time="100"/><!-- timeout=100 -->
32 <ssh start="false"/>
33 </rsync>
修改同步失败日志位置,并且每60分钟对失败的log进行重新同步

<failLog path="/usr/local/sersync/logs/rsync_fail_log.sh" timeToExecute="60"/><!--default ev ery 60mins execute once-->

4.创建相关文件:

1
2
[root@k8s-work3 sersync]# mkdir logs
[root@k8s-work3 sersync]# touch /usr/local/sersync/log/rsync_fail_log.sh

5.开启sersync守护进程同步数据

⚠执行命令前,请进行备份数据,该命令会删除远程的资源文件

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
[root@k8s-work3 sersync]# /usr/local/sersync/bin/sersync2 -d -r -o /usr/local/sersync/conf/confxml.xml
set the system param
execute:echo 50000000 > /proc/sys/fs/inotify/max_user_watches
execute:echo 327679 > /proc/sys/fs/inotify/max_queued_events
parse the command param
option: -d run as a daemon
option: -r rsync all the local files to the remote servers before the sersync work
option: -o config xml name: /usr/local/sersync/conf/confxml.xml
daemon thread num: 10
parse xml config file
host ip : localhost host port: 8008
daemon start,sersync run behind the console
use rsync password-file :
user is root
passwordfile is /etc/rsync.clientPwd
config xml parse success
please set /etc/rsyncd.conf max connections=0 Manually
sersync working thread 12 = 1(primary thread) + 1(fail retry thread) + 10(daemon sub threads)
Max threads numbers is: 32 = 12(Thread pool nums) + 20(Sub threads)
please according your cpu ,use -n param to adjust the cpu rate
------------------------------------------
rsync the directory recursivly to the remote servers once
working please wait...
execute command: cd /data && rsync -artuz -R --delete ./ root@192.168.50.54::www --password-file=/etc/rsync.clientPwd >/dev/null 2>&1
run the sersync:
watch path is: /data
[root@k8s-work3 sersync]# echo "PATH=$PATH:/usr/local/sersync" > /etc/profile.d/sersync.sh
[root@k8s-work3 sersync]# source /etc/profile.d/sersync.sh

参考文档:

1
https://blog.csdn.net/weixin_40134742/article/details/110950845