rsync服务的解释和实现

4 篇文章 0 订阅
3 篇文章 0 订阅

rsync

rsync是Linux系统下的数据镜像备份工具。使用快速增量备份工具Remote Sync可以远程同步,支持本地复制,与其他SSH、rsync主机同步数据。
注意:本文档是基于红帽系列的操作系统来做配置,适用于RHEL和CentOS等主流Linux系统。

一、rsync命令的用法:

基本格式:rsync [选项] 原始位置 目标位置

常用选项:
-a 归档模式,递归并保留对象属性
-v 显示同步过程的详细(verbose)信息
-z 在传输文件时进行压缩(compress)
-H 保留硬链接文件
-A 保留ACL属性

  • -delete 删除目标位置有而原始位置没有的文件
    -r 递归模式,包含目录及子目录中所有文件
    -l 对于软链接文件仍然复制为软链接文件
    -p 保留文件的权限标记
    -t 保留文件的时间标记
    -g 保留文件的属组标记(仅超级用户使用)
    -o 保留文件的属主标记(仅超级用户使用)
    -D 保留设备文件及其他特殊文件

这里我们介绍的的是两种,一种是基于ssh的同步,一种是基于rsync的同步
两者区别基本在于,ssh需要使用的是一个本地的用户,而rsync可以创建虚拟用户,这样会显得更加安全,因为一台机子上有过多的用户并不是一件好事,但是ssh因为可以创建用户,因此密码可以设置的非常复杂,但是rsync创建的虚拟用户不太能够创建更加复杂的密码

我们这里选择两台Centos7的机子,并都要装上rsync,server上创建一个文件目录供给client同步,同样的,client也创建一个存放处
192.168.1.2(server)
192.168.1.10(client)

在server上创建一个授权用户这里我创建的是mkml

[root@client client]# rsync -avz mkml@192.168.1.2:/server/ /client 
The authenticity of host '192.168.1.2 (192.168.1.2)' can't be established.
ECDSA key fingerprint is SHA256:Ibw5WhTH15faTzq4ruPkNrC7cycW9jSMd9kDfSvakk0.
ECDSA key fingerprint is MD5:b3:79:65:52:99:55:43:31:64:a9:41:ab:48:e9:40:43.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.2' (ECDSA) to the list of known hosts.
mkml@192.168.1.2's password: 
receiving incremental file list
./
file1/
file10/
file11/
file12/
file13/
file14/
file15/
file16/
file17/
file18/
file19/
file2/
file20/
file3/
file4/
file5/
file6/
file7/
file8/
file9/

sent 107 bytes  received 435 bytes  98.55 bytes/sec
total size is 0  speedup is 0.00

开始提取,提取成功

一般来说,很多是不需要上传这个说法的,因为这个样子如果大家都随意上传的话,下载也会下载到别人上传的东西,一般不全部开放,一般是可以使用一个比较特殊的acl来限制

我们来演示一下
现在是没有任何acl配置或者开放

[root@client client]# touch mkml-fufu.txt

报错

[root@client client]# rsync -avz /client/ mkml@192.168.1.2:/server
mkml@192.168.1.2's password:
sending incremental file list
rsync: failed to set times on "/server/.": Operation not permitted (1)
./
mkml-fufu.txt
rsync: mkstemp "/server/.mkml-fufu.txt.N7vc5L" failed: Permission denied (13)
sent 111 bytes  received 195 bytes  87.43 bytes/sec
total size is 0  speedup is 0.00
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1179) [sender=3.1.2]
Permission denied一看到这个就知道是权限问题了
/server并没有mkml的写入权限,我们分配一个权限给它,但是我们不能给太大的权限,我们给一个特定用户的权限
[root@server server]# setfacl -m user:mkml:rwx /server
[root@server server]# getfacl /server/
getfacl: Removing leading '/' from absolute path names
file: server/
owner: root
group: root
user::rwx
user:mkml:rwx
group::r-x
mask::rwx
other::r-x

再次执行命令

可以感觉的到上面的操作和scp基本上是很像的,没有体现到rsync的好处

现在,我们展示基于rsync的同步
rsync的管理是靠xinetd的,因此我们也需要在服务端安装xinetd
我们先在/etc/rsync.conf 下添加

address = 192.168.1.2
port 873
pid file = /var/run/rsyncd.pid
log file = /var/log/rsyncd.log
[share]
comment = soft
path = /server
read only = yes
dont compress = *.gz *.bz2 *.zip
auth users = mkml-1
secrets file = /etc/rsyncd_users.db
  1. 第一行是你的主机
  2. 端口,端口记住,最好就是873,默认,如果乱改的话,会导致同步时,server监控端口和client发送端口不一致,导致同步出错
  3. pid存放
  4. 日志存放
  5. 这个就是定义一个参数,到时候客户端就不是根据绝对路径去寻找地址了,只需要知道服务器定义的是什么名字,就可以自动获取路径
  6. 一个解释
  7. 环境,也就是你的存放路径
  8. 仅读
  9. 压缩格式
  10. 授权的虚拟用户
  11. 虚拟用户的密码

现在我们去创建一下虚拟用户的密码存放处
记住,一般的明文存放一些密码什么的,基本上都要给文件600的权限

echo 'mkml-1:123456' >> /etc/rsyncd_users.db
chmod 600 /etc/rsyncd_users.db 

好了,现在来到,/etc/xinetd.d下面定义rsync服务了

vim /etc/xinetd.d/rsync
service rsync
{
        disable = yes
        flags = IPv6
        socket_type = stream
        wait = no
        user = root
        server = /usr/bin/rsync
        server_args = --daemon
        log_on_failure += USERID
}

有些时候可能在xinetd下面并没有这个文件,我们直接创建就可以了
定义我们的rsync服务

{

  1. 是否开启,disable是
  2. flag,你定义成IPv6就是可以支持IPv6,也支持IPv4
  3. 套接字类型,数据流,基本上都是比较固定的
  4. 是否等待
  5. 操作用户
  6. 服务在哪里
  7. 服务参数

}

[root@server xinetd.d]# rsync --daemon
[root@server xinetd.d]# netstat -pantu | grep "rsync"
tcp        0      0 192.168.1.2:873         0.0.0.0:*               LISTEN      5360/rsync

启动并查看监控端口

客户端上可以开始传输文件了

[root@client client]# rsync -avz mkml-1@192.168.1.2::share /client
Password:
receiving incremental file list
./
file1
file10
file11
file12
file13
file14
file15
file16
file17
file18
file19
file2
file20
file3
file4
file5
file6
file7
file8
file9
mkml-fufu.txt
sent 426 bytes  received 1,107 bytes  613.20 bytes/sec
total size is 0  speedup is 0.00

现在我们可以看到,比较麻烦的就是每次都需要输入密码,也比较麻烦

将rsync密码的参数参数加入profie或者是export

export RSYNC_PASSWORD=123456

RSYNC_PASSWORD这玩意是个系统变量可以不用纠结

[root@client client]# rsync -avz mkml@192.168.1.2::share /client
@ERROR: auth failed on module share
rsync error: error starting client-server protocol (code 5) at main.c(1649) [Receiver=3.1.2]
[root@client client]# rsync -avz mkml-1@192.168.1.2::share /client
receiving incremental file list
./
file1
file10
file11
file12
file13
file14
file15
file16
file17
file18
file19
file2
file20
file3
file4
file5
file6
file7
file8
file9
mkml-fufu.txt
sent 426 bytes  received 1,107 bytes  1,022.00 bytes/sec
total size is 0  speedup is 0.00

这个就是免密了,如果不想直接这个直接写出来被别人history看到,也可以写到txt里面,然后提取出来,也是可以的
这里已经提供了rsync的同步源无需密码的同步,ssh的rsync同步也是,ssh免密登录也就是将公钥上传至用户处,也就是可以免密登录,rsync也是这样的,这里就不作操作了

接下来,我们演示的就是rsync里面的排除以及,同步删除非服务端拥有的数据

exclude

很多情况下,我们下载的时候,并不想下载这么多的东西,现在的比较小,但是如果放几个G的玩意进去,下载就是一个比较麻烦的事情
我们可以使用rsync自带的一个排除参数exclude

[root@client client]# rsync -avz --exclude=mkml-fufu.txt mkml-1@192.168.1.2::share /client
receiving incremental file list
./
file1
file10
file11
file12
file13
file14
file15
file16
file17
file18
file19
file2
file20
file3
file4
file5
file6
file7
file8
file9
sent 426 bytes  received 1,039 bytes  2,930.00 bytes/sec
total size is 0  speedup is 0.00

可以看到,的确把mkml-fufu.txt排除了

如果我想排除多个呢,使用{}

[root@client client]# rsync -avz --exclude={mkml-fufu.txt,file1,file2,file3} mkml-1@192.168.1.2::share /client
receiving incremental file list
./
file10
file11
file12
file13
file14
file15
file16
file17
file18
file19
file20
file4
file5
file6
file7
file8
file9
sent 402 bytes  received 892 bytes  2,588.00 bytes/sec
total size is 0  speedup is 0.00

如果我们每次都写那些排除的话,不免有些麻烦,我们可以写在一个txt里面,每次需要只需要调用或加入就可以了

exclude-from,排除你自己定义的一些文件名字,如果有多个,一行一个
echo "mkml-fufu.txt" >> exclude.txt
[root@client client]# rsync -avz --exclude-from=/root/exclude.txt mkml-1@192.168.1.2::share /client
receiving incremental file list
./
file1
file10
file11
file12
file13
file14
file15
file16
file17
file18
file19
file2
file20
file3
file4
file5
file6
file7
file8
file9
sent 426 bytes  received 1,039 bytes  2,930.00 bytes/sec
total size is 0  speedup is 0.00

的确是没了

delete

好了,排除已经弄完了,我们现在学习删除,这个删除不是删除服务端的,而是删除非服务端的共有的数据
我们先在client上创建一些文件,并在一个服务端共有的文件上加入一些字符

echo "123456" >> /client/file1
[root@client client]# rsync -avz --delete mkml-1@192.168.1.2::share /client
receiving incremental file list
deleting c.txt
deleting b.txt
deleting a.txt
./
file1
file10
file11
file12
file13
file14
file15
file16
file17
file18
file19
file2
file20
file3
file4
file5
file6
file7
file8
file9
mkml-fufu.txt
sent 432 bytes  received 1,107 bytes  3,078.00 bytes/sec
total size is 0  speedup is 0.00
[root@client client]# ll
total 0
-rw-r--r--. 1 root root 0 Apr 17 20:56 file1
-rw-r--r--. 1 root root 0 Apr 17 20:56 file10
-rw-r--r--. 1 root root 0 Apr 17 20:56 file11
-rw-r--r--. 1 root root 0 Apr 17 20:56 file12
-rw-r--r--. 1 root root 0 Apr 17 20:56 file13
-rw-r--r--. 1 root root 0 Apr 17 20:56 file14
-rw-r--r--. 1 root root 0 Apr 17 20:56 file15
-rw-r--r--. 1 root root 0 Apr 17 20:56 file16
-rw-r--r--. 1 root root 0 Apr 17 20:56 file17
-rw-r--r--. 1 root root 0 Apr 17 20:56 file18
-rw-r--r--. 1 root root 0 Apr 17 20:56 file19
-rw-r--r--. 1 root root 0 Apr 17 20:56 file2
-rw-r--r--. 1 root root 0 Apr 17 20:56 file20
-rw-r--r--. 1 root root 0 Apr 17 20:56 file3
-rw-r--r--. 1 root root 0 Apr 17 20:56 file4
-rw-r--r--. 1 root root 0 Apr 17 20:56 file5
-rw-r--r--. 1 root root 0 Apr 17 20:56 file6
-rw-r--r--. 1 root root 0 Apr 17 20:56 file7
-rw-r--r--. 1 root root 0 Apr 17 20:56 file8
-rw-r--r--. 1 root root 0 Apr 17 20:56 file9
-rw-r--r--. 1 1003 1003 0 Apr 17 20:59 mkml-fufu.txt
[root@client client]# cat file1
[root@client client]# 

可以看到,里面的我们原本创建的文件没了,加入file1里面的字符也没有了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值