|NO.Z.00076|——————————|^^ 部署 ^^|——|Linux&RSYNC服务.V02|——|SSH协议|Rsync同步|

一、ssh协议数据同步:将NFS服务器数据同步备份到rsync服务器
### --- rsync部署

~~~     我们一般使用rsync来进行单向数据同步,因此我们需要确定一个基准,
~~~     比如:两台服务器,一台NFS作为网站数据服务器(基准服务器),
~~~     另一台专门做rsync数据备份服务器,我们以此为基础开始我们的实验
### --- 实验环境:一台NFS服务器,一台rsync服务器

~~~     centos6.x:20.20.20.21   SRC——数据服务器,推送:服务器端
~~~     centos6.x:20.20.20.22   DST——备份服务器,下载:本地客户端
### --- 在两台服务器上分别创建目录(/filesrc、/filedst)
### --- 下行同步(下载)
~~~     格式:rsync -avz  服务器地址:/服务器目录/* /本地目录
~~~     示例:rsync -avz root@20.20.20.21:/filesrc/* /filedst
~~~     -a: 归档模式,递归保留对象属性
~~~     -v:显示同步过程
~~~     -z:在传输文件时进行压缩

[root@server21 ~]#  yum install-y rsync
[root@server22 ~]# yum install -y rsync
[root@server21 ~]# mkdir /filesrc
[root@server21 ~]# touch /filesrc/{1..5}.txt
[root@server22 ~]# mkdir /filedst
~~~		#报错信息:
[root@server22 ~]# rsync -avz root@10.10.10.12:/filesrc/* /filedst/
rsync: Failed to exec ssh: No such file or directory (2)
rsync error: error in IPC code (code 14) at pipe.c(84) [receiver=3.0.6]
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: error in IPC code (code 14) at io.c(600) [receiver=3.0.6]

~~~		#解决方案:检查原因是ssh客户端没有安装
[root@server22 ~]# yum install openssh-clients -y
~~~		#报错信息
[root@server22 ~]# rsync -avz root@20.20.20.21:/filesrc/* /filedst/
root@20.20.20.21's password: 
bash: rsync: command not found
rsync: connection unexpectedly closed (0 bytes received so far) [receiver]
rsync error: remote command not found (code 127) at io.c(600) [receiver=3.0.6]

~~~		#解决方案:目标服务器没有安装rsync服务
[root@server21 ~]# yum install-y  rsync
[root@server22 ~]# rsync -avz root@20.20.20.21:/filesrc/* /filedst/
root@20.20.20.21's password: 空格
^[[Areceiving incremental file list
1.txt
2.txt
3.txt
4.txt
5.txt
[root@server22 ~]# ls /filedst/
1.txt  2.txt  3.txt  4.txt  5.txt 
### --- 上行同步(上传)
~~~     格式:rsync -avz /本地目录/*~# 服务器地址:/服务器目录
~~~     示例:rsync -ava /filedst/* root@192.168.88.10:/filesrc

[root@server21 ~]# rm -rf /filesrc/*
[root@server22 ~]# rsync -avz  /filedst/* root@20.20.20.21:/filesrc/
root@20.20.20.21's password: 
sending incremental file list
1.txt
2.txt
3.txt
4.txt
5.txt

sent 250 bytes  received 107 bytes  238.00 bytes/sec
total size is 0  speedup is 0.00 
[root@server21 ~]# ls /filesrc/
1.txt  2.txt  3.txt  4.txt  5.txt  
### --- 注意:使用root用户进行实验可以,但生产环境中尽量使用创建的普通用户,减少权限溢出 
### --- 创建用来做数据同步的用户,并给予用户对目录的相应权限,一般使用ACL设置权限

[root@server21 ~]# ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
f4:71:ca:0d:cb:b8:21:c3:6d:9e:a6:97:56:eb:0e:b1 root@server21
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|                 |
|        . o .    |
|     . o = B     |
|      + S * .    |
|       = *.      |
|        Eo .     |
|       o+..      |
|      .o oo      |
+-----------------+
~~~		#报错信息
[root@server21 ~]# ssh-copy-id root@20.20.20.22
-bash: ssh-copy-id: command not found
 
~~~		#报错分析
[root@server21 ~]# rpm -qa|grep openssh
openssh-5.3p1-117.el6.x86_64
openssh-server-5.3p1-117.el6.x86_64

~~~		#解决方案:发现没有安装客户端
[root@server21 ~]# yum install -y openssh-clients
[root@server21 ~]# ssh -copy-id root@20.20.20.22
Unknown cipher type 'opy-id'
[root@server21 ~]# ssh-copy-id root@20.20.20.22
to make sure we haven't added extra keys that you weren't expecting.

[root@server22 ~]# ssh-keygen -t rsa -b 2048 
[root@server22 ~]# ssh-copy-id root@20.20.20.21
root@20.20.20.21's password: 
to make sure we haven't added extra keys that you weren't expecting.
### --- 验证不需要密码直接传输
### --- 拓展:若要实现免密码数据同步,只需要做好ssh秘钥对登录即可

[root@server22 ~]# rm -rf /filedst/* 
[root@server21 ~]# rsync -avz /filesrc/* root@20.20.20.22:/filedst
1.txt
2.txt
3.txt
4.txt
5.txt 
二、rsync协议数据同步,将NFS服务器数据同步备份到rsync服务器
### --- 实验环境:一台服务器,一台客户端
### --- 在两台服务器上分别创建目录(/filesrc、/filedst)
### --- 搭建rsync服务(仅需要在NFS服务器上搭建即可)
### --- 创建主配置文件
### --- 创建认证所需账户密码文件

[root@server21 ~]# vim /etc/rsyncd.conf                     // 这个文件默认没有,自己创建即可
address = 20.20.20.21                                       // rsync服务绑定IP
port 873                                                    // 默认服务端口873 
log file = /var/log/rsyncd.log                              // 日志文件位置
pid file = /var/run/rsyncd.pid                              // 进程号文件位置
[web]                                                       // 共享名:用来连接是写在url上的,切记
        comment = web directory backup                      // 共享描述话语
        path = /filesrc                                     // 实际共享目录
        read only = no                                      // 是否仅允许读取
        dont compress = *.gz *.bz2                          // 那些文件类型不进行压缩
        auth users = user1                                  // 登录用户名(非系统用户,需要自行创建)
        secrets file = /etc/rsyncd_users.db                 // 认证所需账号密码文件(需自行创建同上)
### --- 创建认证所需账户密码文件

[root@server21 ~]# vim /etc/rsyncd_users.db
user1:123456
[root@server21 ~]# chmod 600 /etc/rsyncd_users.db           // 必须修改权限,否则登录报错
### --- 启动服务

[root@server21 ~]# rsync --daemon
[root@server21 ~]# netstat -antp |grep :873
tcp        0      0 20.20.20.21:873             0.0.0.0:*                   LISTEN      1679/rsync          
### --- 设置映射用户对共享目录有权限(r)

[root@server21 ~]# setfacl -m u:nobody:rwx /filesrc
~~~		#注意:
~~~		关闭服务可使用kill命令,但偶尔会造成服务被结束,
~~~		但进程号配置we年不被删除的问题,若遇到此类问题可自己手动删除,
~~~		再启动则正常(建议自己写一个rsync的服务管理脚本)
### --- 下行同步(下载)
~~~     格式:rsync -avz  rsync://用户名@服务器地址/共享模块名 /本地目录
~~~     示例:rsync -avz rsync://user1@20.20.20.21/web /filedst
~~~     拓展:--delete:删除本地比服务器多出来的文件(源地址没有,目标地址有的删掉)

[root@server22 ~]# rm -rf /filedst/*
[root@server21 ~]# touch /filesrc/6.txt
[root@server21 ~]# ls /filesrc/
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt 
~~~		#报错信息:出现以上情况说明,服务器已经连接上了,密码验证失败
[root@server22 ~]# rsync -avz  rsync://user1@20.20.20.21/web /filedst/
Password: 
@ERROR: auth failed on module web
rsync error: error starting client-server protocol (code 5) at main.c(1503) [receiver=3.0.6]

~~~		#解决方案:
[root@server22 ~]# rsync -avz --delete rsync://user1@20.20.20.21/web /filedst/      #客户端下载 
Password: 123456                
./
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt
### --- 本地创建7.txt文件

[root@server22 ~]# touch /filedst/7.txt                     
[root@server22 ~]# rsync -avz rsync://user1@20.20.20.21/web /filedst/
Password: 
receiving incremental file list                             // 同步7.txt并没有同步过去
./
[root@server22 ~]# rsync -avz --delete rsync://user1@20.20.20.21/web /filedst/
Password: 
receiving incremental file list
deleting 7.txt                                              // 同步删除差异文件:会拿着本地文件和本地对比,服务器端有,下载下来,服务器端没有,本地有,删除本地即可;保持本地文件和服务器端文件一致
[root@server22 ~]# ls /filedst/
1.txt  2.txt  3.txt  4.txt  5.txt  6.txt
### --- 上行同步(上传)
~~~     格式:rsync -avz /本地目录/* rsync://用户名@服务器地址/共享模块名
~~~     示例:rsync -ava /filedst/* rsync://user1@20.20.20.21/web

[root@server21 ~]# rm -rf /filesrc/*
[root@server22 ~]# rsync -avz /filedst/  rsync://user1@20.20.20.21/web 
Password: 
sending incremental file list
./
rsync: failed to set times on "/." (in web): Operation not permitted (1)
1.txt
2.txt
3.txt
4.txt
5.txt
6.txt 
### --- 拓展:rsync协议的免密码可以借助一个环境变量实现
~~~     export RSYNC_PASSWORD=虚拟用户密码(客户端生成)

[root@server22 ~]# export RSYNC_PASSWORD=123456             // rsync协议的免密码可以借助一个环境变量实现
[root@server22 ~]# rm -rf /filedst/*
[root@server22 ~]# touch /filedst/{a..f}.txt
[root@server22 ~]# ls /filedst/
a.txt  b.txt  c.txt  d.txt  e.txt  f.txt
[root@server22 ~]# rsync -avz --delete rsync://user1@20.20.20.21/web /filedst/
receiving incremental file list                             // 没有用户名及密码
deleting f.txt                                              // 删除本地差异文件
deleting e.txt
deleting d.txt
deleting c.txt
deleting b.txt
deleting a.txt
./
1.txt                                                       // 没有的文件同步过来
2.txt
3.txt
4.txt
5.txt
6.txt

 
 
 
 
 
 
 
 
 

Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
                                                                                                                                                   ——W.S.Landor

 

 

posted on   yanqi_vip  阅读(53)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示