集群分发脚本xsync

scp(secure copy)安全拷贝

(1)定义:scp可以实现服务器与服务器之间的数据拷贝
(2)基本语法:

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

(3)案例:
在hadoop102上,把数据拷贝到hadoop103
scp -r jdk1.8.0_371/ root@hadoop103:/opt/module

在hadoop103上,拉取hadoop102的数据
scp -r root@hadoop102:/opt/module/hadoop-3.2.4 ./

rsync 远程同步工具

  1. 基本语法
rsync -av $pdir/$fname $user@host:$pdir/$fname
# rsync 命令
# -av   -a 归档拷贝   -v 显示复制过程
# $pdir/$fname 要拷贝的文件路径/名称
# $user@host:$pdir/$fname 目的地用户名@主机目的地路径/名称
  1. 案例:
    在hadoop102上,同步hadoop102上的数据到hadoop103
    rsync -av hadoop-3.2.4/ root@hadoop103:/opt/module/hadoop-3.2.4/

集群分发脚本

rsync命令原始拷贝rsync -av /opt/module root@hadoop103:/opt
期望脚本使用方式:xsync 要同步的文件名称
期望脚本在任何路径都能使用(脚本放在声明了全局环境变量的路径)

[amo@hadoop102 ~]$ echo $PATH
/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/opt/module/jdk1.8.0_371/bin:/opt/module/hadoop-3.2.4/bin:/opt/module/hadoop-3.2.4/sbin:/home/amo/.local/bin:/home/amo/bin

home目录下创建bin文件夹,并在该文件夹下创建xsync文件vim xsync (名字随便起)

#!/bin/bash

#1.判断参数个数
if [ $# -lt 1 ]
then
    echo Not Enough Arguement!
    exit;
fi

#2.遍历集群所有机器
for host in hadoop102 hadoop103 hadoop104
do
    echo ========= = $host =============
    #3.遍历所有目录,一个个发送

    for file in $@
    do
        #4.判断文件是否存在
        if [ -e $file ]
            then
                #5.获取父目录
                pdir=$(cd -P $(dirname $file); pwd)

                #6.获取当前文件的名称
                filename=$(basename $file)
                ssh $host "mkdir -p $pdir"
                rsync -av $pdir/$filename $host:$pdir
            else
                echo $file does not exists!
        fi
    done
done

修改脚本 xsync 具有执行权限
chmod 777 xsync

测试脚本
xsync /bin
将脚本复制到/bin中,以便全局调用
sudo cp xsync /bin/
同步环境变量配置(root所有者)
sudo ./bin/xsync /etc/profile.d/my_env.sh

环境变量生效source /etc/profile

SSH免密登录

免密登录原理

  1. A服务器通过ssh-keygen -t rsa命令生成密钥对(公钥和私钥)
  2. A服务器通过ssh-copy-id 服务器B命令将公钥拷贝到B服务器
  3. A服务器ssh访问B服务器(数据用私钥加密)
  4. B服务器接收到数据后,去授权的key中查找A服务器的公钥,并解密数据
  5. 将采用A公钥加密后的数据返回给A服务器

SSH免密登录配置

#切换到home目录下
cd ~ 
# 查看home目录下的所有文件(包括隐藏文件)
ll -al 
# 切换到.ssh文件夹下
cd .ssh
# 生成公钥和私钥
ssh-keygen -t rsa
# 授权给另一个服务器
ssh-copy-id hadoop103

生成公钥和私钥

# 生成公钥和私钥
[root@hadoop102 .ssh]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
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:
SHA256:pCATVCsPvIqYqZMz0KYnHxIE5V7gsqz+MU41co6SkNY root@hadoop102
The key's randomart image is:
+---[RSA 2048]----+
|.o=..            |
|.+ o .           |
|..O +   .        |
|o=.O . o         |
|=+oE.+. S        |
|B=+ * .          |
|X*.= .           |
|X.=.o            |
|.Ooo             |
+----[SHA256]-----+

授权

# 授权
[root@hadoop102 .ssh]# ssh-copy-id hadoop103
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@hadoop103's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'hadoop103'"
and check to make sure that only the key(s) you wanted were added.

[root@hadoop102 .ssh]# ssh-copy-id hadoop104
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@hadoop104's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'hadoop104'"
and check to make sure that only the key(s) you wanted were added.

测试

[amo@hadoop102 ~]$ ssh hadoop103
Last login: Fri Mar  1 19:40:22 2024 from 192.168.1.1
[amo@hadoop103 ~]$ 
03-05 13:44