Linux之rsync同步分发脚本编写.

网友投稿 723 2022-11-03

Linux之rsync同步分发脚本编写.

Linux之rsync同步分发脚本编写.

说明

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

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

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

安装rsync增量拷贝工具

编写分发脚本

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

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

xsync文件的内容:

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

hosts文件配置

我的hosts配置,我把三台服务器分别配置了 zjj101 zjj102 zjj103 这三台, 如果你不会配置hosts的话,参考

[root@zjj101 ~]# cat /etc/hosts127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6172.16.10.101 zjj101172.16.10.102 zjj102172.16.10.103 zjj103

配置ssh免密码登录

生成公钥和私钥

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

命令: ssh-keygen -t rsa

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

[root@zjj101 ~]# ssh-keygen -t rsaGenerating 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:HLoOUb25W6avx5qhdKUmvR0Zh8lyKGbTir8x7z2lPTU root@zjj101.localdomainThe key's randomart image is:+---[RSA 2048]----+| || . || . o || . + * o || . * S B . || = * * +. E || o B B.=+ . . || = O @=.o || =oX=+. . |+----[SHA256]-----+[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

[root@zjj101 .ssh]# ssh-copy-id root@zjj102/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 i nstalled/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 keysroot@zjj102's password:Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@zjj102'"and check to make sure that only the key(s) you wanted were added.[root@zjj101 .ssh]# ssh-copy-id root@zjj103/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 i nstalled/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 keysroot@zjj103's password:Number of key(s) added: 1[root@zjj101 ~]# ssh-copy-id root@zjj101/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 i nstalled/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 keysroot@zjj101's password:Number of key(s) added: 1Now try logging into the machine, with: "ssh 'root@zjj101'"and check to make sure that only the key(s) you wanted were added.

更详细的介绍看这里

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

我的配置是这样,我把rsync脚本放到了/root/script/ , 具体怎么配置 ,直接参考这个去配置一下

export PATH="$PATH:/root/script/"export PATH

使用rsync分发脚本

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

[root@zjj101 ~]# sh xsync soft/要分发的文件的路径是:/root/soft---------------------zjj102---------------------sending incremental file listsoft/soft/rocketmq-all-4.7.1-bin-release.rarsent 13,618,966 bytes received 39 bytes 27,238,010.00 bytes/sectotal size is 13,615,496 speedup is 1.00---------------------zjj103---------------------sending incremental file listsoft/soft/rocketmq-all-4.7.1-bin-release.rarsent 13,618,966 bytes received 39 bytes 9,079,336.67 bytes/sectotal size is 13,615,496 speedup is 1.00[root@zjj101 ~]#

查看分发的效果

我这里用的是xcall脚本,

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

[root@zjj101 ~]# xcall ls soft/要执行的命令是ls soft/---------------------zjj101-----------------rocketmq-all-4.7.1-bin-release.rar---------------------zjj102-----------------rocketmq-all-4.7.1-bin-release.rar---------------------zjj103-----------------rocketmq-all-4.7.1-bin-release.rar

错误解决

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:html5sortable- jQuery 排序插件
下一篇:SpringBoot整合MQTT小结汇总
相关文章

 发表评论

暂时没有评论,来抢沙发吧~