说明

这个脚本可以把这台机器的文件批量复制到别的集群上相同目录里面.

比如说你给A机器的 /usr/local 路径下的 Java安装包,复制到 B机器和C机器相同路径下 , 你不可能每台机器都去登录一下,然后每台机器都复制一下, 这样 如果集群中机器很多的话,比如说10台 20台,你这么搞会累死你.

如果你写了个脚本,把A机器直接同步到 其它的 10台 20 台机器上,这样岂不美滋滋.

安装rsync增量拷贝工具

https://blog.csdn.net/qq_41489540/article/details/109081656

编写分发脚本

如果多个机器同步文件的话你不可能去手动挨个执行命令去同步文件,这样会累死, 直接写个同步脚本去批量同步配置,相当于把命令封装到shell脚本里面,这样不用每次都写一遍命令,使用起来也很方便.

下面脚本就是我可以通过 zjj101 机器向 zjj102和zjj103机器上同步文件夹里面的文件,如果文件夹里面有软连接的话,也会同步软连接对应的目录的文件.

xsync文件的内容:

  1. #!/bin/bash
  2. #校验参数是否合法
  3. if(($#==0))
  4. then
  5. echo 请输入要分发的文件!
  6. exit;
  7. fi
  8. # -P 如果是软连接也能获取分发文件的绝对路径
  9. dirpath=$(cd `dirname $1`; pwd -P)
  10. filename=`basename $1`
  11. echo 要分发的文件的路径是:$dirpath/$filename
  12. #循环执行rsync分发文件到集群的每条机器
  13. # for循环适当修改一下, 也可以在这里写死你自己的服务名字.
  14. # 我这里的服务器名字是 zjj102和 zjj103
  15. for((i=102;i<=103;i++))
  16. do
  17. echo ---------------------zjj$i---------------------
  18. rsync -rvlt $dirpath/$filename root@zjj$i:$dirpath
  19. done

hosts文件配置

我的hosts配置,我把三台服务器分别配置了 zjj101 zjj102 zjj103 这三台,
如果你不会配置hosts的话,参考: https://blog.csdn.net/qq_41489540/article/details/116400827

  1. [root@zjj101 ~]# cat /etc/hosts
  2. 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
  3. ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
  4. 172.16.10.101 zjj101
  5. 172.16.10.102 zjj102
  6. 172.16.10.103 zjj103

配置ssh免密码登录

生成公钥和私钥

生成公钥和私钥 , 默认保存括号里面的内容,

命令: ssh-keygen -t rsa

直接回车三下就可以了,啥也不用管

  1. [root@zjj101 ~]# ssh-keygen -t rsa
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:HLoOUb25W6avx5qhdKUmvR0Zh8lyKGbTir8x7z2lPTU root@zjj101.localdomain
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. | |
  13. | . |
  14. | . o |
  15. | . + * o |
  16. | . * S B . |
  17. | = * * +. E |
  18. | o B B.=+ . . |
  19. | = O @=.o |
  20. | =oX=+. . |
  21. +----[SHA256]-----+
  22. [root@zjj101 ~]#

将公钥复制到远程机器中

命令: ssh-copy-id root@zjj101

敲完了之后需要舒若zjj101的密码,

解释: root@zjj101是要复制的目标机器

下面是操作步骤:

执行下面的命令,没有先后顺序执行完了之后需要输入目标机器的登录密码:

ssh-copy-id root@zjj102

ssh-copy-id root@zjj103

ssh-copy-id root@zjj101

  1. [root@zjj101 .ssh]# ssh-copy-id root@zjj102
  2. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  3. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already i nstalled
  4. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install th e new keys
  5. root@zjj102's password:
  6. Number of key(s) added: 1
  7. Now try logging into the machine, with: "ssh 'root@zjj102'"
  8. and check to make sure that only the key(s) you wanted were added.
  9. [root@zjj101 .ssh]# ssh-copy-id root@zjj103
  10. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  11. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already i nstalled
  12. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install th e new keys
  13. root@zjj103's password:
  14. Number of key(s) added: 1
  15. [root@zjj101 ~]# ssh-copy-id root@zjj101
  16. /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
  17. /usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already i nstalled
  18. /usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install th e new keys
  19. root@zjj101's password:
  20. Number of key(s) added: 1
  21. Now try logging into the machine, with: "ssh 'root@zjj101'"
  22. and check to make sure that only the key(s) you wanted were added.

更详细的介绍看这里: https://blog.csdn.net/qq_41489540/article/details/109091590

配置环境变量实现任何地方都能执行这个脚本

我的配置是这样,我把rsync脚本放到了/root/script/ , 具体怎么配置 ,直接参考这个去配置一下 : https://blog.csdn.net/qq_41489540/article/details/109099887

  1. export PATH="$PATH:/root/script/"
  2. export PATH

使用rsync分发脚本

将 soft文件夹里面所有文件都同步分发一下 , soft文件夹里面只有 rocketmq-all-4.7.1-bin-release.rar 文件

  1. [root@zjj101 ~]# sh xsync soft/
  2. 要分发的文件的路径是:/root/soft
  3. ---------------------zjj102---------------------
  4. sending incremental file list
  5. soft/
  6. soft/rocketmq-all-4.7.1-bin-release.rar
  7. sent 13,618,966 bytes received 39 bytes 27,238,010.00 bytes/sec
  8. total size is 13,615,496 speedup is 1.00
  9. ---------------------zjj103---------------------
  10. sending incremental file list
  11. soft/
  12. soft/rocketmq-all-4.7.1-bin-release.rar
  13. sent 13,618,966 bytes received 39 bytes 9,079,336.67 bytes/sec
  14. total size is 13,615,496 speedup is 1.00
  15. [root@zjj101 ~]#

查看分发的效果

我这里用的是xcall脚本, 博客: https://blog.csdn.net/qq_41489540/article/details/109094840

发现集群多台机器已经自动生成了 soft 目录, 并且soft目录有了 rocketmq-all-4.7.1-bin-release.rar 这个文件,

  1. [root@zjj101 ~]# xcall ls soft/
  2. 要执行的命令是ls soft/
  3. ---------------------zjj101-----------------
  4. rocketmq-all-4.7.1-bin-release.rar
  5. ---------------------zjj102-----------------
  6. rocketmq-all-4.7.1-bin-release.rar
  7. ---------------------zjj103-----------------
  8. rocketmq-all-4.7.1-bin-release.rar

错误解决

行3: 未预期的符号 `$’\r’’ 附近有语法错误

解决博客: https://blog.csdn.net/qq_41489540/article/details/109094955