Linux文件分发脚本,只需一条命令将你的文件分发到各个服务器上

系统 Linux
在运维或在日常工作生活中,我们经常会把一个文件拷贝到其它服务器上,或同时分发到多个服务器上,甚至要求目标机将文件放在相同的路径下,方便程序进一步调用。

 背景

在运维或在日常工作生活中,我们经常会把一个文件拷贝到其它服务器上,或同时分发到多个服务器上,甚至要求目标机将文件放在相同的路径下,方便程序进一步调用。

 

[[437674]]

 

遇到这种问题,我们通常的做法是使用scp或rsync命令把文件拷贝一个一个地拷贝到多台服务器上,这样做费事费力;大神的做法是使用ansible的playbook一下把事情干完,前提是你得会ansible;快捷的做法就是使用今天的脚本了。

效果演示

目前拥有4台机器,分别为client、node1、node2和node3,client与其它3台机器能够建立ssh链接。在client的/root/test目录下有a.txt和b.txt两个文件。

 

Linux文件分发脚本,只需一条命令将你的文件分发到各个服务器上

 

 

  1. [root@client test]# ls /root/test/ 
  2. a.txt  b.txt 
  3. [root@client test]#  

 

我把文件分发到node1、node2和node3的/root/test下,执行以下命令:

 

  1. # 在/root/test目录下执行, xrsync是我的脚本 
  2. [root@client test]# xrsync a.txt b.txt  

 

执行分发过程:

 

  1. [root@client test]# xrsync a.txt b.txt  
  2. ============ node1 ============ 
  3. sending incremental file list 
  4. a.txt 
  5.  
  6. sent 93 bytes  received 35 bytes  256.00 bytes/sec 
  7. total size is 2  speedup is 0.02 
  8. sending incremental file list 
  9. b.txt 
  10.  
  11. sent 93 bytes  received 35 bytes  85.33 bytes/sec 
  12. total size is 2  speedup is 0.02 
  13. ============ node2 ============ 
  14. sending incremental file list 
  15. a.txt 
  16.  
  17. sent 93 bytes  received 35 bytes  256.00 bytes/sec 
  18. total size is 2  speedup is 0.02 
  19. sending incremental file list 
  20. b.txt 
  21.  
  22. sent 93 bytes  received 35 bytes  256.00 bytes/sec 
  23. total size is 2  speedup is 0.02 
  24. ============ node3 ============ 
  25. sending incremental file list 
  26. a.txt 
  27.  
  28. sent 93 bytes  received 35 bytes  85.33 bytes/sec 
  29. total size is 2  speedup is 0.02 
  30. sending incremental file list 
  31. b.txt 
  32.  
  33. sent 93 bytes  received 35 bytes  256.00 bytes/sec 
  34. total size is 2  spee 

 

到node2上看一下,文件果然存在。同样地,node3和node4也同步过去了。

 

  1. # node2上查看 
  2. [root@node2 ~]# ls /root/test/ 
  3. a.txt  b.txt 
  4. [root@node2 ~]#  
  5.  
  6. # node3上查看 
  7. [root@node3 ~]# ls /root/test/ 
  8. a.txt  b.txt 
  9. [root@node3 ~]#  
  10.  
  11. # node4上查看 
  12. [root@node4 ~]# ls /root/test/ 
  13. a.txt  b.txt 
  14. [root@node4 ~]#  

 

脚本奉上

整个脚本的代码,只需要把其中的node1 node2 node3修改为自己环境下的主机名或ip地址即可。

 

  1. #!/bin/bash 
  2. # 判断参数是否足够 
  3. if [ $# -lt 1 ] 
  4. then 
  5.  echo Not Enounh Arguement! 
  6.  exit; 
  7. fi 
  8.  
  9. # 遍历所有的机器 
  10. for host in node1 node2 node3 
  11. do 
  12.  echo ============  $host ============ 
  13.  for file in $@ 
  14.  do 
  15.   # 判断文件是否存在 
  16.   if [ -e $file ] 
  17.   then 
  18.    # 获取父目录 
  19.    pdir=$(cd -P $(dirname $file); pwd) 
  20.  
  21.    # 获取当前目录的名称 
  22.    fname=$(basename $file) 
  23.    ssh $host "mkdir -p $pdir" 
  24.    rsync -av $pdir/$fname $host:$pdir 
  25.   else 
  26.    echo $file does not exists! 
  27.   fi 
  28.  done 
  29. done 

 

运行条件

为了更方便脚本的运行,建议使用如下优化。

1.修改/etc/hosts文件,加入IP地址与主机名的对应关系,这样方便我们使用主机名直接操作。比如我演示的机器配置。

 

  1. vim  /etc/hosts 
  2. # 加入配置,自己的机器对应修改 
  3. …… 
  4. 192.168.31.47 client 
  5. 192.168.31.48 node1 
  6. 192.168.31.50 node2 
  7. 192.168.31.51 node3 

 

2.客户机与目标机之间使用ssh密码验证登录,这样在传输文件时不需要二次验证。

 

  1. # 生成ssh私钥 
  2. ssh-keygen -f /root/.ssh/id_rsa -N ''  
  3. # 循环把公钥传递到服务器上,免密登录 
  4. for i in node1 node2 node3  
  5. do  
  6.   ssh-copy-id $i 
  7. done 
  8.  
  9. # 根据提示输入密码 

 

3.给脚本加可执行权限,并配置环境变量,使用全局可用。

 

  1. # 把文件存储为xrsync,加上x权限 
  2. [root@client shell]# chmod +x xrsync  
  3. [root@client shell]#  
  4.  
  5. # 配置环境变量 
  6. # 我把脚本放在/opt/shell下的,自己情况类比修改 
  7. [root@client shell]# vim /etc/profile.d/my_env.sh  
  8. export PATH=$PATH:/opt/shell 
  9.  
  10. # 配置生效,就可以在全局生效了 
  11. [root@client opt]# source /etc/profile 

 

责任编辑:华轩 来源: 今日头条
相关推荐

2010-10-20 13:58:22

sqlserver分发

2021-02-15 15:07:45

Windows 10Windows微软

2012-11-23 17:20:43

Linux服务器

2011-12-27 09:37:40

服务器性能监控

2017-03-31 13:24:09

2018-08-14 08:43:17

服务器命令CCS系统

2022-11-10 15:17:43

Windows文件Linux

2021-06-03 06:52:11

Redis服务器命令

2010-10-28 16:12:01

Oracle数据库配置

2016-11-18 15:08:54

linux服务器策略

2024-02-20 13:43:12

2016-12-29 11:02:13

源目录前缀算法

2018-08-15 08:45:38

2024-02-01 18:07:37

2017-05-31 13:26:43

Linux服务器关机自定义消息

2011-10-25 17:16:28

Linux服务器数据删除

2022-01-24 17:10:55

Windows 10Windows 11微软

2012-11-19 11:20:04

IT管理服务器调研报告

2009-11-11 10:18:02

2023-03-12 09:22:58

点赞
收藏

51CTO技术栈公众号