Linux文件分發(fā)腳本,只需一條命令將你的文件分發(fā)到各個(gè)服務(wù)器上
背景
在運(yùn)維或在日常工作生活中,我們經(jīng)常會把一個(gè)文件拷貝到其它服務(wù)器上,或同時(shí)分發(fā)到多個(gè)服務(wù)器上,甚至要求目標(biāo)機(jī)將文件放在相同的路徑下,方便程序進(jìn)一步調(diào)用。
遇到這種問題,我們通常的做法是使用scp或rsync命令把文件拷貝一個(gè)一個(gè)地拷貝到多臺服務(wù)器上,這樣做費(fèi)事費(fèi)力;大神的做法是使用ansible的playbook一下把事情干完,前提是你得會ansible;快捷的做法就是使用今天的腳本了。
效果演示
目前擁有4臺機(jī)器,分別為client、node1、node2和node3,client與其它3臺機(jī)器能夠建立ssh鏈接。在client的/root/test目錄下有a.txt和b.txt兩個(gè)文件。

- [root@client test]# ls /root/test/
- a.txt b.txt
- [root@client test]#
我把文件分發(fā)到node1、node2和node3的/root/test下,執(zhí)行以下命令:
- # 在/root/test目錄下執(zhí)行, xrsync是我的腳本
- [root@client test]# xrsync a.txt b.txt
執(zhí)行分發(fā)過程:
- [root@client test]# xrsync a.txt b.txt
- ============ node1 ============
- sending incremental file list
- a.txt
- sent 93 bytes received 35 bytes 256.00 bytes/sec
- total size is 2 speedup is 0.02
- sending incremental file list
- b.txt
- sent 93 bytes received 35 bytes 85.33 bytes/sec
- total size is 2 speedup is 0.02
- ============ node2 ============
- sending incremental file list
- a.txt
- sent 93 bytes received 35 bytes 256.00 bytes/sec
- total size is 2 speedup is 0.02
- sending incremental file list
- b.txt
- sent 93 bytes received 35 bytes 256.00 bytes/sec
- total size is 2 speedup is 0.02
- ============ node3 ============
- sending incremental file list
- a.txt
- sent 93 bytes received 35 bytes 85.33 bytes/sec
- total size is 2 speedup is 0.02
- sending incremental file list
- b.txt
- sent 93 bytes received 35 bytes 256.00 bytes/sec
- total size is 2 spee
到node2上看一下,文件果然存在。同樣地,node3和node4也同步過去了。
- # node2上查看
- [root@node2 ~]# ls /root/test/
- a.txt b.txt
- [root@node2 ~]#
- # node3上查看
- [root@node3 ~]# ls /root/test/
- a.txt b.txt
- [root@node3 ~]#
- # node4上查看
- [root@node4 ~]# ls /root/test/
- a.txt b.txt
- [root@node4 ~]#
腳本奉上
整個(gè)腳本的代碼,只需要把其中的node1 node2 node3修改為自己環(huán)境下的主機(jī)名或ip地址即可。
- #!/bin/bash
- # 判斷參數(shù)是否足夠
- if [ $# -lt 1 ]
- then
- echo Not Enounh Arguement!
- exit;
- fi
- # 遍歷所有的機(jī)器
- for host in node1 node2 node3
- do
- echo ============ $host ============
- for file in $@
- do
- # 判斷文件是否存在
- if [ -e $file ]
- then
- # 獲取父目錄
- pdir=$(cd -P $(dirname $file); pwd)
- # 獲取當(dāng)前目錄的名稱
- fname=$(basename $file)
- ssh $host "mkdir -p $pdir"
- rsync -av $pdir/$fname $host:$pdir
- else
- echo $file does not exists!
- fi
- done
- done
運(yùn)行條件
為了更方便腳本的運(yùn)行,建議使用如下優(yōu)化。
1.修改/etc/hosts文件,加入IP地址與主機(jī)名的對應(yīng)關(guān)系,這樣方便我們使用主機(jī)名直接操作。比如我演示的機(jī)器配置。
- vim /etc/hosts
- # 加入配置,自己的機(jī)器對應(yīng)修改
- ……
- 192.168.31.47 client
- 192.168.31.48 node1
- 192.168.31.50 node2
- 192.168.31.51 node3
2.客戶機(jī)與目標(biāo)機(jī)之間使用ssh密碼驗(yàn)證登錄,這樣在傳輸文件時(shí)不需要二次驗(yàn)證。
- # 生成ssh私鑰
- ssh-keygen -f /root/.ssh/id_rsa -N ''
- # 循環(huán)把公鑰傳遞到服務(wù)器上,免密登錄
- for i in node1 node2 node3
- do
- ssh-copy-id $i
- done
- # 根據(jù)提示輸入密碼
3.給腳本加可執(zhí)行權(quán)限,并配置環(huán)境變量,使用全局可用。
- # 把文件存儲為xrsync,加上x權(quán)限
- [root@client shell]# chmod +x xrsync
- [root@client shell]#
- # 配置環(huán)境變量
- # 我把腳本放在/opt/shell下的,自己情況類比修改
- [root@client shell]# vim /etc/profile.d/my_env.sh
- export PATH=$PATH:/opt/shell
- # 配置生效,就可以在全局生效了
- [root@client opt]# source /etc/profile