scp、rsync与集群分发

1、scp(secure copy)安全拷贝
定义

scp可以实现服务器与服务器之间的数据拷贝。(from server1 to server2)

基本语法
scp   -r      $pdir/$fname          $user@$host:$pdir/$fname
命令   递归     要拷贝的文件路径/名称     目的地用户@主机:目的地路径/名称
实操

在 bigdata801,将文件从 bigdata801 堆到 bigdata802

[root@bigdata801 module]# scp -r hadoop-3.3.1/ bigdata802:/opt/module/

在 bigdata803,将文件从 bigdata801 拉到 bigdata803

[root@bigdata803 module]# scp -r bigdata801:/opt/module/hadoop-3.3.1 bigdata803:/opt/module/hadoop-3.3.1

在 bigdata802,将文件从 bigdata801 拷到 bigdata804

[root@bigdata802 module]# scp -r bigdata801:/opt/module/hadoop-3.3.1 bigdata804:/opt/module/hadoop-3.3.1
2、rsync远程同步工具
定义

rsync主要用于备份和镜像。具有速度快、避免复制相同内容和支持符号链接的优点。

rsync和scp区别

用rsync做文件的复制要比scp的速度快,rsync只对差异文件做更新。scp是把所有文件都复制过去。

基本语法
rsync    -av       $pdir/$fname            $user@$host:$pdir/$fname
命令   选项参数      要拷贝的文件路径/名称       目的地用户@主机:目的地路径/名称
选项功能
-a归档拷贝
-v显示复制过程
实操

bigdata801 中的 hadoop 安装目录下,存在 wcinput wcoutput 两个文件夹,而 bigdata802 中没有

[root@bigdata801 hadoop-3.3.1]# pwd
/opt/module/hadoop-3.3.1
[root@bigdata801 hadoop-3.3.1]# ll
总用量 88
drwxr-xr-x 2 1000 1000   203 615 13:52 bin
drwxr-xr-x 3 1000 1000    20 615 13:15 etc
drwxr-xr-x 2 1000 1000   106 615 13:52 include
drwxr-xr-x 3 1000 1000    20 615 13:52 lib
drwxr-xr-x 4 1000 1000   288 615 13:52 libexec
-rw-rw-r-- 1 1000 1000 23450 615 13:02 LICENSE-binary
drwxr-xr-x 2 1000 1000  4096 615 13:52 licenses-binary
-rw-rw-r-- 1 1000 1000 15217 615 13:02 LICENSE.txt
-rw-rw-r-- 1 1000 1000 29473 615 13:02 NOTICE-binary
-rw-rw-r-- 1 1000 1000  1541 522 00:11 NOTICE.txt
-rw-rw-r-- 1 1000 1000   175 522 00:11 README.txt
drwxr-xr-x 3 1000 1000  4096 615 13:15 sbin
drwxr-xr-x 4 1000 1000    31 615 14:18 share
drwxr-xr-x 2 root root    22 822 18:00 wcinput
drwxr-xr-x 2 root root    88 822 18:43 wcoutput
[root@bigdata801 hadoop-3.3.1]# 
[root@bigdata802 hadoop-3.3.1]# pwd
/opt/module/hadoop-3.3.1
[root@bigdata802 hadoop-3.3.1]# ll
总用量 88
drwxr-xr-x. 2 root root   203 822 19:23 bin
drwxr-xr-x. 3 root root    20 822 19:23 etc
drwxr-xr-x. 2 root root   106 822 19:23 include
drwxr-xr-x. 3 root root    20 822 19:23 lib
drwxr-xr-x. 4 root root   288 822 19:23 libexec
-rw-r--r--. 1 root root 23450 822 19:23 LICENSE-binary
drwxr-xr-x. 2 root root  4096 822 19:21 licenses-binary
-rw-r--r--. 1 root root 15217 822 19:23 LICENSE.txt
-rw-r--r--. 1 root root 29473 822 19:23 NOTICE-binary
-rw-r--r--. 1 root root  1541 822 19:23 NOTICE.txt
-rw-r--r--. 1 root root   175 822 19:23 README.txt
drwxr-xr-x. 3 root root  4096 822 19:23 sbin
drwxr-xr-x. 4 root root    31 822 19:23 share
[root@bigdata802 hadoop-3.3.1]# 

在 bigdata801 上执行如下命令

[root@bigdata801 hadoop-3.3.1]# yum install -y rsync
[root@bigdata801 hadoop-3.3.1]# rsync -av /opt/module/hadoop-3.3.1/ bigdata802:/opt/module/hadoop-3.3.1/
3、xsync集群分发脚本
需求

循环复制文件到所有节点的相同目录下

需求分析

(a)rsync命令原始拷贝:

[root@bigdata801 hadoop-3.3.1]# rsync -av /opt/module/hadoop-3.3.1/ bigdata802:/opt/module/hadoop-3.3.1/

(b)期望脚本:

xsync 要同步的文件名称

(c)期望脚本在任何路径都能使用(脚本放在声明了全局环境变量的路径)

[root@bigdata801 ~]# echo $PATH/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/opt/module/jdk1.8.0_181/bin:/opt/module/hadoop-3.3.1/bin:/opt/module/hadoop-3.3.1/sbin:/root/bin
脚本实现

(a)在/root/bin目录下创建xsync文件

[root@bigdata801 ~]# cd /root/[root@bigdata801 ~]# mkdir bin[root@bigdata801 ~]# cd bin/[root@bigdata801 bin]# vim xsync

在该文件中编写如下代码

#!/bin/bash#1. 判断参数个数if [ $# -lt 1 ]then    echo Not Enough Arguement!    exit;fi#2. 遍历集群所有机器for host in bigdata801 bigdata802 bigdata803 bigdata804do    echo ====================  $host  ====================    #3. 遍历所有目录,挨个发送    for file in $@    do        #4. 判断文件是否存在        if [ -e $file ]            then                #5. 获取父目录                pdir=$(cd -P $(dirname $file); pwd)                #6. 获取当前文件的名称                fname=$(basename $file)                ssh $host "mkdir -p $pdir"                rsync -av $pdir/$fname $host:$pdir            else                echo $file does not exists!        fi    donedone
修改脚本 xsync 具有执行权限
[root@bigdata801 bin]# chmod 777 xsync [root@bigdata801 bin]# ll总用量 4-rwxrwxrwx 1 root root 753 8月  22 20:45 xsync
测试脚本

将刚创建的 bin 分发到其他机器

[root@bigdata801 ~]# xsync bin/

执行过程如下

[root@bigdata801 ~]# xsync bin/==================== bigdata801 ====================sending incremental file listsent 75 bytes  received 17 bytes  184.00 bytes/sectotal size is 753  speedup is 8.18==================== bigdata802 ====================sending incremental file listbin/bin/xsyncsent 878 bytes  received 39 bytes  611.33 bytes/sectotal size is 753  speedup is 0.82==================== bigdata803 ====================sending incremental file listbin/bin/xsyncsent 878 bytes  received 39 bytes  1,834.00 bytes/sectotal size is 753  speedup is 0.82==================== bigdata804 ====================sending incremental file listbin/bin/xsyncsent 878 bytes  received 39 bytes  611.33 bytes/sectotal size is 753  speedup is 0.82[root@bigdata801 ~]# 

在 bigdata802上查看文件是否分发成功

[root@bigdata802 hadoop-3.3.1]# cd /root/[root@bigdata802 ~]# ll总用量 4-rw-------. 1 root root 1460 2月  28 19:08 anaconda-ks.cfgdrwxr-xr-x. 2 root root   19 8月  22 20:47 bin[root@bigdata802 ~]# 
分发环境变量
[root@bigdata801 ~]# xsync /etc/proprofile    profile.d/ protocols  [root@bigdata801 ~]# xsync /etc/profile==================== bigdata801 ====================sending incremental file listsent 46 bytes  received 12 bytes  116.00 bytes/sectotal size is 2,318  speedup is 39.97==================== bigdata802 ====================sending incremental file listprofilesent 323 bytes  received 59 bytes  764.00 bytes/sectotal size is 2,318  speedup is 6.07==================== bigdata803 ====================sending incremental file listprofilesent 323 bytes  received 59 bytes  764.00 bytes/sectotal size is 2,318  speedup is 6.07==================== bigdata804 ====================sending incremental file listprofilesent 323 bytes  received 59 bytes  764.00 bytes/sectotal size is 2,318  speedup is 6.07[root@bigdata801 ~]# 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值