Linuxクラスタ環境におけるファイル同期スクリプトの作成と活用

クラスタ内の複数ノード間でファイルを同期させるための同期スクリプトを作成し、各ターゲットノードの同一ディレクトリにファイルを循環的に同期します(root権限を持つユーザー、またはrootユーザーでxsyncを作成する必要があります)
1.sudoでxsyncファイルを作成(rootユーザーは直接作成可能)

xsyncスクリプトのソースコード:

#!/bin/bash
#1. パラメータ数の確認
if [ $# -lt 1 ]
then
echo 引数が不足しています!
exit;
fi

#2. クラスタ内のすべてのマシンを反復処理
for server in primary worker1 worker2
do
echo ==================== $server ====================
#3. すべてのディレクトリを反復処理し、それぞれを送信
for item in $@
do
#4. ファイルの存在確認
if [ -e $item ]
then
#5. 親ディレクトリの取得
parent_dir=$(cd -P $(dirname $item); pwd)
#6. 現在のファイル名の取得
file_name=$(basename $item)
ssh $server "mkdir -p $parent_dir"
rsync -av $parent_dir/$file_name $server:$parent_dir
else
echo $item は存在しません!
fi
done
done

yum install -y rsync

xsyncファイルに実行権限を付与:

chmod 777 xsync

  • 注意点:
  • 1.各ノードにrsyncツールがインストールされ、起動されていることを確認

例:primaryマシンの/optディレクトリにあるscpをworker1とworker2に複製

[root@primary bin]#  xsync /opt/scp
-bash: /usr/local/bin/xsync: /bin/bash^M: 不良なインタプリタ: そのようなファイルやディレクトリはありません
[root@primary bin]# sudo  xsync /opt/scp
==================== primary ====================
sending incremental file list
 
sent 134 bytes  received 18 bytes  304.00 bytes/sec
total size is 49  speedup is 0.32
==================== worker1 ====================
sending incremental file list
 
sent 134 bytes  received 18 bytes  304.00 bytes/sec
total size is 49  speedup is 0.32
==================== worker2 ====================
sending incremental file list
 
sent 130 bytes  received 18 bytes  296.00 bytes/sec
total size is 49  speedup is 0.33
[root@primary bin]# cd 
[root@primary ~]# xsync /opt/scp
-bash: /usr/local/bin/xsync: /bin/bash^M: 不良なインタプリタ: そのようなファイルやディレクトリはありません
[root@primary ~]# sudo xsync /opt/scp
==================== primary ====================
sending incremental file list
 
sent 130 bytes  received 18 bytes  296.00 bytes/sec
total size is 49  speedup is 0.33
==================== worker1 ====================
sending incremental file list
 
sent 134 bytes  received 18 bytes  304.00 bytes/sec
total size is 49  speedup is 0.32
==================== worker2 ====================
sending incremental file list
 
sent 134 bytes  received 18 bytes  304.00 bytes/sec
total size is 49  speedup is 0.32
[root@primary ~]# cd /opt/
[root@primary opt]# cd scp/
[root@primary scp]# ll
合計 4
-rw-r--r-- 1 root root 45 12月 11 21:06 1.html
drwxr-xr-x 2 root root 22 12月 11 21:38 abc
[root@primary scp]# mkdir testdir
[root@primary scp]# cd testdir/
[root@primary testdir]# vi sample.txt
[root@primary testdir]# cd
[root@primary ~]# sudo xsync /opt/scp
==================== primary ====================
sending incremental file list
 
sent 183 bytes  received 19 bytes  404.00 bytes/sec
total size is 62  speedup is 0.31
==================== worker1 ====================
sending incremental file list
scp/
scp/testdir/
scp/testdir/sample.txt
 
sent 249 bytes  received 48 bytes  594.00 bytes/sec
total size is 62  speedup is 0.21
==================== worker2 ====================
sending incremental file list
scp/
scp/testdir/
scp/testdir/sample.txt
 
sent 249 bytes  received 48 bytes  594.00 bytes/sec
total size is 62  speedup is 0.21
[root@primary ~]# cd
[root@primary ~]# cd /bin/
[root@primary bin]# vi xsync
[root@primary bin]# 

参考:

zk.sh スクリプト

#!/bin/bash
case $1 in
"start"){
for i in primary worker1 worker2
do
 echo ---------- zookeeper $i 起動 ------------
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh  start"
done
};;
"stop"){
for i in primary worker1 worker2
do
 echo ---------- zookeeper $i 停止 ------------ 
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh stop"
done
};;
"status"){
for i in primary worker1 worker2
do
 echo ---------- zookeeper $i 状態 ------------ 
ssh $i "/opt/module/zookeeper-3.5.7/bin/zkServer.sh status"
done
};;
esac

jpsall.sh スクリプト:

#!/bin/bash
 
# jpsコマンドを実行して各サーバーのノード状態を照会
echo ======================クラスタノード状態====================
 
for i in primary worker1 worker2
do
        echo ====================== $i ====================
        ssh $i "/opt/jdk/jdk1.8.0_202/bin/jps"
done
echo ======================完了====================

タグ: linux クラスタ ファイル同期 Bashスクリプト rsync

7月29日 08:14 投稿