了解最新公司動(dòng)態(tài)及行業(yè)資訊
背景
在運(yùn)維或者日常工作生活中,我們經(jīng)常將一個(gè)文件拷貝到其他服務(wù)器,或者同時(shí)分發(fā)到多臺(tái)服務(wù)器,甚至要求目標(biāo)機(jī)器把文件放在同一個(gè)路徑下服務(wù)器運(yùn)維,方便的程序可以進(jìn)一步調(diào)用。
遇到這些問題,我們一般的做法是使用scp或者rsync命令將文件一個(gè)一個(gè)拷貝到多臺(tái)服務(wù)器上,費(fèi)力又費(fèi)力;大師的方法是用它來完成工作服務(wù)器運(yùn)維,前提是你有技能;快速的方法是使用明天的腳本。
功效展示
目前有4臺(tái)機(jī)器,分別是node1、node2和node3,可以完成與其他3臺(tái)機(jī)器的ssh鏈接。/root/test 目錄下有兩個(gè)文件 a.txt 和 b.txt。
[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看看,文件確實(shí)存在。同樣,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è)腳本的代碼,只需要將改成自己環(huán)境的或者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ī)名的對(duì)應(yīng)關(guān)系,這樣我們就可以直接使用主機(jī)名進(jìn)行操作了。例如我演示的機(jī)器配置。
vim /etc/hosts
# 加入配置,自己的機(jī)器對(duì)應(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)境變量使用全局可用。
# 把文件存儲(chǔ)為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
至此,早點(diǎn)完成工作,開始愉快的玩耍吧~
24小時(shí)免費(fèi)咨詢
請(qǐng)輸入您的聯(lián)系電話,座機(jī)請(qǐng)加區(qū)號(hào)