rsync服务
  kOLpNtlbOkJN 2023年11月13日 41 0

rsync服务的模式

本地模式
类似使用cp命令 在本地进行增量或者全量的拷贝
远程模式
将数据从一台Linux服务器 拷贝到另外一台服务器
将数据从另外一台服务器拉取到本地服务器
进程模式
C/S架构 服务端/客户端
将数据从客户端上传到服务端
将数据从服务端下载到客户端

本地模式语法:

Local:  rsync [OPTION...] SRC...          [DEST]
本地: rsync 参数选项 源文件/源目录 目标位置
[root@backup ~]# mkdir test
[root@backup ~]# ll
total 0
drwxr-xr-x 2 root root 6 Jun 7 09:39 test
[root@backup ~]# touch test/1.txt
[root@backup ~]# ll test/
total 0
-rw-r--r-- 1 root root 0 Jun 7 09:39 1.txt
通过cp方式 每次拷贝都是用的全量复制
[root@backup ~]# cp -r test/ /opt/
[root@backup ~]# ll /opt/
total 0
drwxr-xr-x 2 root root 19 Jun 7 09:48 test
[root@backup ~]# ll /opt/test/
total 0
-rw-r--r-- 1 root root 0 Jun 7 09:48 1.txt
[root@backup ~]# cp -r oldboy/ /opt/
cp: overwrite ‘/opt/oldboy/1.txt’? y
使用rsync进行复制文件
注意事项: rsync在操作目录的过程中 如果目录后面加/ 则操作的是目录下面的文件
如果目录后面不加/ 则是操作的目录本身 其他命令目录后面加/和不加/意义相同

[root@backup ~]# rm -rf /opt/*
[root@backup ~]# rsync -avz test /opt/
sending incremental file list
test/
test/1.txt

sent 118 bytes received 39 bytes 314.00 bytes/sec
total size is 0 speedup is 0.00
[root@backup ~]# ll /opt/
total 0
drwxr-xr-x 2 root root 19 Jun 7 09:39 test

默认rsync使用增量的拷贝方式
[root@backup ~]# touch test/2.txt
[root@backup ~]# rsync -avz test /opt/
sending incremental file list
test/
test/2.txt

sent 143 bytes received 39 bytes 364.00 bytes/sec
total size is 0 speedup is 0.00

远程访问模式:

语法结构:    
Pull: rsync [OPTION...] [USER@]HOST:SRC... [DEST]
下载: rsync -avz 远程用户@IP:源文件/源目录 目标位置
将远程服务器上的文件 下载到本地的某个目录下
Pull: 下载 更新
rsync:命令
USER: 真实的系统用户 管理员 普通用户
表示远程拷贝数据时需要验证的用户身份
如果为root 那拥有拷贝所有文件的权限
@: 分隔符
HOST:IP地址 主机名称 域名(前提是可以通信)
主机名表示自己的主机 是可以ping通的
ping 测试主机或者域名是否可达
hosts文件作用: 本地DNS域名解析服务
SRC:远程主机的文件位置
DEST:下载到本地的哪个目录

在41服务器下载31下的/etc/hosts文件到当前目录
[root@backup ~]# rsync -avz 172.16.1.31:/etc/hosts .
The authenticity of host '172.16.1.31 (172.16.1.31)' can't be established.
ECDSA key fingerprint is SHA256:aue4n851RYpyNcK5oViChFpxT/NJSzzMxlINWQt+HVs.
ECDSA key fingerprint is MD5:73:86:d2:db:73:80:93:1f:62:b9:53:98:85:63:d0:04.
Are you sure you want to continue connecting (yes/no)? yes #输入yes
Warning: Permanently added '172.16.1.31' (ECDSA) to the list of known hosts.
root@172.16.1.31's password: # 需要验证31root的密码 而非41的root密码
receiving incremental file list
hosts

sent 43 bytes received 136 bytes 12.34 bytes/sec
total size is 158 speedup is 0.88
[root@backup ~]# ll
total 4
-rw-r--r-- 1 root root 158 Jun 7 2013 hosts
-----------------------------
在31创建odboy用户
[root@nfs ~]# useradd test
[root@nfs ~]#
[root@nfs ~]# echo 1|passwd --stdin test
Changing password for user test.
passwd: all authentication tokens updated successfully.

在41通过oldboy用户下载/root/test.txt
[root@backup ~]# rsync -avz oldboy@172.16.1.31:/root/test.txt .
test@172.16.1.31's password:
receiving incremental file list
rsync: change_dir "/root" failed: Permission denied (13) #权限拒绝

普通用户下载31的/etc/hosts文件
[root@backup ~]# rsync -avz oldboy@172.16.1.31:/etc/hosts .
oldboy@172.16.1.31's password:
receiving incremental file list
hosts

使用域名方式下载: 先配置hosts解析 然后使用nfs为主机进行远程下载
[root@backup ~]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
10.0.0.31 nfs
[root@backup ~]# rsync -avz root@nfs:/etc/passwd .

注意目录:
31上下载41/root下的oldboy目录到当前目录
[root@nfs ~]# rsync -avz 172.16.1.41:/root/oldboy .
The authenticity of host '172.16.1.41 (172.16.1.41)' can't be established.
ECDSA key fingerprint is SHA256:aue4n851RYpyNcK5oViChFpxT/NJSzzMxlINWQt+HVs.
ECDSA key fingerprint is MD5:73:86:d2:db:73:80:93:1f:62:b9:53:98:85:63:d0:04.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '172.16.1.41' (ECDSA) to the list of known hosts.
root@172.16.1.41's password:
receiving incremental file list
test/
test/1.txt
test/2.txt

sent 66 bytes received 179 bytes 98.00 bytes/sec
total size is 0 speedup is 0.00
[root@nfs ~]# ll
total 0
drwxr-xr-x 2 root root 32 Jun 7 09:53 test

守护进程模式:

语法结构:     
Pull: rsync [OPTION...] [USER@]HOST::SRC... [DEST]
下载: rsync -avz 用户@主机::模块名称 下载本地的目录
Push: rsync [OPTION...] SRC... [USER@]HOST::DEST
推送: rsync -avz 本地的文件 用户@主机::模块名称


部署安装:

第一个流程: 下载安装服务 默认已经安装
[root@backup ~]# yum -y install rsync

第二个流程: 配置服务
[root@backup ~]# cat /etc/rsyncd.conf # rsync服务默认的配置文件
[root@backup ~]# cat /etc/rsyncd.conf
uid = rsync 用户需要创建
gid = rsync
port = 873
fake super = yes
use chroot = no
max connections = 200
timeout = 600
ignore errors
read only = false
list = false
auth users = rsync_backup # 登陆验证用户 在这个位置配置
secrets file = /etc/rsync.passwd # 需要创建密码文件
[backup] # 模块名称 执行命令需要用模块名称 可以自定义修改任意名称
path = /backup # 存储文件的目录
第三个流程: 根据配置文件创建必要的信息
1)确认rsync虚拟用户是否存在 如果没有则创建
[root@backup ~]# id rsync
id: rsync: no such user 找不到rsync用户
创建虚拟用户 不允许登陆 不创建家目录
[root@backup ~]# useradd -s /sbin/nologin -M rsync
[root@backup ~]# id rsync
uid=1000(rsync) gid=1000(rsync) groups=1000(rsync)

2) 认证用户不需要创建
3) 密码文件 手动可以修改密码的名称和位置
创建密码文件: 密码文件名称和配置文件中的密码文件名称必须相同
[root@backup ~]# vim /etc/rsync.passwd
[root@backup ~]# cat /etc/rsync.passwd
rsync_backup:123456

4) 给密码文件授权为600权限
[root@backup ~]# chmod 600 /etc/rsync.passwd
[root@backup ~]# ll /etc/rsync.passwd
-rw------- 1 root root 20 Jun 7 11:13 /etc/rsync.passwd

5) 创建backup目录
[root@backup ~]# mkdir /backup
[root@backup ~]# ll -d /backup
drwxr-xr-x 2 root root 6 Jun 7 11:18 /backup

[root@backup ~]# chown rsync.rsync /backup/
[root@backup ~]# ll -d /backup/
drwxr-xr-x 2 rsync rsync 6 Jun 7 11:18 /backup/

第三个流程: 启动服务
​ [root@backup ~]# systemctl start rsyncd
​ [root@backup ~]# systemctl enable rsyncd
​ 查看服务是否启动成功:
​ [root@backup ~]# netstat -tnulp
​ Active Internet connections (only servers)
​ Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
​ tcp 0 0 0.0.0.0:873 0.0.0.0:* LISTEN 17365/rsync

第四个流程: 测试服务
客户端: 10.0.0.200
上传/etc/hosts 到服务器的backup模块----> /backup目录

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月13日 0

暂无评论

推荐阅读
kOLpNtlbOkJN
作者其他文章 更多

2023-11-13