logo
关于我们

技术分享

技术分享 rsync数据镜像备份工具

rsync数据镜像备份工具

2021-06-01

rsync简介:

rsync(remote synchronize)是Liunx/Unix下的一个远程数据同步工具。它可通过LAN/WAN快速同步多台主机间的文件和目录,并适当利用rsync算法(差分编码,也就是差异传输)以减少数据的传输。因此同步两端的数据很快,而且对同步的数据可以支持递归传输(可以使用tree查看发现两边目录tree一致),还可以保持数据原本的属性,也支持选择性压缩。(可本地也可异地传输)

rsync工作原理:

1.client构造filelist发送到server(filellist是包含了所有文件信息对:name->id,id用来代表唯一的文件,例MD5)

2.Server处理filelsit,将filelist中存在而本地不存在的对应数据构成newfilelist,将本地存在而filelist中不存在的数据删除。

3.Server发送构造好的newfilelist至Client

4.Client接受newfilelist,并发送newfilelist中对应的数据至Server.

rsync数据镜像备份工具

rsync同步数据模拟

京东云主机为Server IP :  AAA.AAA.AAA.AAA

阿里云主机为Client IP :  BBB.BBB.BBB.BBB

首先安装rsync,两端都要安装,笔者这都是已经默认安装好了的:

[root@JD ~]#  rpm -qa rsync
rsync-3.0.9-18.el7.x86_64

如果没有安装可以去官网下载安装

[root@JD ~]#  cd /usr/local/src
[root@JD ~]# 
tar
[root@JD src]#  tar -zvxf rsync-3.0.9.tar.gz 
[root@JD src]# cd rsync-3.0.9
[root@JD rsync-3.0.9]# ./configure
[root@JDrsync-3.0.9 ]#  make $$ make install


搭建远程服务之前先来小试一下rsync命令:

1 rsync [OPTION]... SRC [SRC]... DEST

2 rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST

3 rsync [OPTION]... SRC [SRC]... [USER@]HOST::DEST

4 rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST

5 rsync [OPTION]... [USER@]HOST:SRC [DEST]

6 rsync [OPTION]... [USER@]HOST::SRC [DEST]

7 rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

可以看到一共有6种用法使用较多的是2,6

第一种:rsync [OPTION]... SRC [SRC]... DEST
是将指定本地数据传输到指定本地路径,类似cp
[root@Aliyun rsynctest]# ll
total 4
drwxr-xr-x 2 root root 4096 Sep 24 15:34 test
-rw-r--r-- 1 root root    0 Sep 24 15:34 xad.test
[root@Aliyun rsynctest]# rsync -av xad.test test/
sending incremental file list
xad.test
sent 72 bytes  received 31 bytes  206.00 bytes/sec
total size is 0  speedup is 0.00
[root@Aliyun rsynctest]# ll test/xad.test 
-rw-r--r-- 1 root root 0 Sep 24 15:34 test/xad.test


第二种: rsync [OPTION]... SRC [SRC]... [USER@]HOST:DEST
将本地数据传输到远端路径,且是使用远端的用户传输,需要密码。
 [root@Aliyun ~]# rsync -av rsynctest/ root@ AAA.AAA.AAA.AAA:testrsync
The authenticity of host ' AAA.AAA.AAA.AAA ( AAA.AAA.AAA.AAA)' can't be established.
ECDSA key fingerprint is 6f:8c:32:66:d8:e4:91:78:e8:3a:4a:39:56:6f:1f:ec.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'AAA.AAA.AAA.AAA' (ECDSA) to the list of known hosts.
root@116.196.124.3's password: 
sending incremental file list
./
xad.test
test/
test/xad.test
sent 179 bytes  received 57 bytes  22.48 bytes/sec
total size is 0  speedup is 0.00
传输完成可以去远端检验
[root@JD testrsync]# ll
total 0
drwxr-xr-x 2 root root 22 Sep 24 15:35 test
-rw-r--r-- 1 root root  0 Sep 24 15:34 xad.test


第六种:rsync [OPTION]... [USER@]HOST::SRC [DEST]##注意是"::"
以远端的数据为基准同步到本地,属于服务器模式,使rsync在后台启用一个守护进程,使之永久运行,用于接受文件传输请求。
京东云主机为ServerIP :  AAA.AAA.AAA.AAA
阿里云主机为Client IP :  BBB.BBB.BBB.BBB
将server的数据同步到client

首先先找到配置文件/etc/rsync.conf
[root@JD ~]# cat /etc/rsync.conf
 uid = nobody##使用匿名用户执行守护进程
 gid = nobody
 use chroot = no
 max connections = 4##最大并发连接数
 strict modes = yes##检验口令文件的权限,若为yes,则密码文件需要root权限
 pid file = /var/run/rsyncd.pid
 [XAD]##定义一个模块名称
path = /root/testrsync  ##需要传输的数据的路径
comment = xadsrc file
read only = no
write only = no
hosts allow = *##允许连接的地址
#hosts deny = IP/NETMASK
uid = root
gid = root
auth users = backup
 ##使用此模块的用户,多个用户用空格隔开,此用户和linux用户没任何联系
secrets file = /etc/server.pass
 ##密码文件,格式是“user:pass”单独一行,需要自己手动创建,文件名是什么随意。

创建/etc/server.pass
[root@JD ~]#  cat /etc/server.pass
backup:xad123
[root@JD ~]# chmod 600 /etc/server.pass

创建守护进程
[root@JD ~]#  /usr/bin/rsync    --daemon
[root@JD ~]# ps -ef | grep rsync
root     15862     1  0 Sep23 ?        00:00:00 /usr/bin/rsync --daemon



现在去cilent端配置执行rsync,在这只需添加密码文件和执行传输操作就可以,什么都不用设置。
创建/etc/client.pass
[root@Aliyun ~]#  cat /etc/client.pass
xad123
[root@Aliyun ~]# chmod 600 /etc/client.pass
[root@Aliyun ~]#  /usr/bin/rsync  -vzrtopg --delete --progress --exclude "*access*" --exclude "debug*" backup@116.196.124.3::XAD  /root/rsynctest --password-file=/etc/client.pass
##命令中的参数
v是详细输出
z是压缩后传输
r是递归传输,可以是用tree查看对比
t是保数据的原始时间信息
o是保持数据的所属主
p是保持数据的文件权限
g是保持数据的属组
exclude排除不需要传输的文件类型
delete是以服务器的数据为基准去镜像数据的同步
progress显示数据镜像的过程
backup@116.196.124.3::XAD 红色标注的就是在服务器端设置的对应信息
/root/rsynctest 数据同步的保存路径
 --password-file=/etc/client.pass数据使用的密码文件!指的是backup用户!
##
测试
为了测试先删除/root/rsynctest中的文件后在执行
[root@Aliyun rsynctest]#  /usr/bin/rsync  -vzrtopg --delete --progress --exclude "*access*" --exclude "debug*" backup@ AAA.AAA.AAA.AAA::XAD /root/rsynctest --password-file=/etc/client.pass 
receiving incremental file list
./
xad.test
           0 100%    0.00kB/s    0:00:00 (xfer#1, to-check=2/4)
test/
test/xad.test
           0 100%    0.00kB/s    0:00:00 (xfer#2, to-check=0/4)
sent 126 bytes  received 232 bytes  716.00 bytes/sec
total size is 0  speedup is 0.00
[root@Aliyun rsynctest]# ll
total 4
drwxr-xr-x 2 root root 4096 Sep 24 15:35 test
-rw-r--r-- 1 root root    0 Sep 24 15:34 xad.test
设置定时备份
[root@Aliyun ~]# crontab -e 
301***/usr/bin/rsync  -vzrtopg --delete --progress --exclude "*access*" --exclude "debug*" backup@ AAA.AAA.AAA.AAA::XAD /root/rsynctest --password-file=/etc/client.pass
到此为止server端的数据就可以在凌晨1:30分开始同步到client了。



云祺备份软件,云祺容灾备份系统,虚拟机备份,数据库备份,文件备份,实时备份,勒索软件,美国,图书馆
  • 标签:
  • 其他

您可能感兴趣的新闻 换一批

现在下载,可享30天免费试用

立即下载