自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linux文件分發(fā)腳本,只需一條命令將你的文件分發(fā)到各個(gè)服務(wù)器上

系統(tǒng) Linux
在運(yùn)維或在日常工作生活中,我們經(jīng)常會(huì)把一個(gè)文件拷貝到其它服務(wù)器上,或同時(shí)分發(fā)到多個(gè)服務(wù)器上,甚至要求目標(biāo)機(jī)將文件放在相同的路徑下,方便程序進(jìn)一步調(diào)用。

 背景

在運(yùn)維或在日常工作生活中,我們經(jīng)常會(huì)把一個(gè)文件拷貝到其它服務(wù)器上,或同時(shí)分發(fā)到多個(gè)服務(wù)器上,甚至要求目標(biāo)機(jī)將文件放在相同的路徑下,方便程序進(jìn)一步調(diào)用。

 

[[437674]]

 

遇到這種問題,我們通常的做法是使用scp或rsync命令把文件拷貝一個(gè)一個(gè)地拷貝到多臺(tái)服務(wù)器上,這樣做費(fèi)事費(fèi)力;大神的做法是使用ansible的playbook一下把事情干完,前提是你得會(huì)ansible;快捷的做法就是使用今天的腳本了。

效果演示

目前擁有4臺(tái)機(jī)器,分別為client、node1、node2和node3,client與其它3臺(tái)機(jī)器能夠建立ssh鏈接。在client的/root/test目錄下有a.txt和b.txt兩個(gè)文件。

 

Linux文件分發(fā)腳本,只需一條命令將你的文件分發(fā)到各個(gè)服務(wù)器上

 

 

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

 

我把文件分發(fā)到node1、node2和node3的/root/test下,執(zhí)行以下命令:

 

  1. # 在/root/test目錄下執(zhí)行, xrsync是我的腳本 
  2. [root@client test]# xrsync a.txt b.txt  

 

執(zhí)行分發(fā)過程:

 

  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 ~]#  

 

腳本奉上

整個(gè)腳本的代碼,只需要把其中的node1 node2 node3修改為自己環(huán)境下的主機(jī)名或ip地址即可。

 

  1. #!/bin/bash 
  2. # 判斷參數(shù)是否足夠 
  3. if [ $# -lt 1 ] 
  4. then 
  5.  echo Not Enounh Arguement! 
  6.  exit; 
  7. fi 
  8.  
  9. # 遍歷所有的機(jī)器 
  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.    # 獲取當(dāng)前目錄的名稱 
  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 

 

運(yùn)行條件

為了更方便腳本的運(yùn)行,建議使用如下優(yōu)化。

1.修改/etc/hosts文件,加入IP地址與主機(jī)名的對(duì)應(yīng)關(guān)系,這樣方便我們使用主機(jī)名直接操作。比如我演示的機(jī)器配置。

 

  1. vim  /etc/hosts 
  2. # 加入配置,自己的機(jī)器對(duì)應(yīng)修改 
  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.客戶機(jī)與目標(biāo)機(jī)之間使用ssh密碼驗(yàn)證登錄,這樣在傳輸文件時(shí)不需要二次驗(yàn)證。

 

  1. # 生成ssh私鑰 
  2. ssh-keygen -f /root/.ssh/id_rsa -N ''  
  3. # 循環(huán)把公鑰傳遞到服務(wù)器上,免密登錄 
  4. for i in node1 node2 node3  
  5. do  
  6.   ssh-copy-id $i 
  7. done 
  8.  
  9. # 根據(jù)提示輸入密碼 

 

3.給腳本加可執(zhí)行權(quán)限,并配置環(huán)境變量,使用全局可用。

 

  1. # 把文件存儲(chǔ)為xrsync,加上x權(quán)限 
  2. [root@client shell]# chmod +x xrsync  
  3. [root@client shell]#  
  4.  
  5. # 配置環(huán)境變量 
  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 

 

責(zé)任編輯:華軒 來源: 今日頭條
相關(guān)推薦

2010-10-20 13:58:22

sqlserver分發(fā)

2021-02-15 15:07:45

Windows 10Windows微軟

2012-11-23 17:20:43

Linux服務(wù)器

2011-12-27 09:37:40

服務(wù)器性能監(jiān)控

2017-03-31 13:24:09

2018-08-14 08:43:17

服務(wù)器命令CCS系統(tǒng)

2022-11-10 15:17:43

Windows文件Linux

2024-06-19 10:43:44

2021-06-03 06:52:11

Redis服務(wù)器命令

2010-10-28 16:12:01

Oracle數(shù)據(jù)庫配置

2016-11-18 15:08:54

linux服務(wù)器策略

2024-02-20 13:43:12

2018-08-15 08:45:38

2016-12-29 11:02:13

源目錄前綴算法

2024-02-01 18:07:37

2011-10-25 17:16:28

Linux服務(wù)器數(shù)據(jù)刪除

2022-01-24 17:10:55

Windows 10Windows 11微軟

2017-05-31 13:26:43

Linux服務(wù)器關(guān)機(jī)自定義消息

2023-03-12 09:22:58

2009-11-11 10:18:02

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)