0

各サーバーで実行され、特定のファイルをコピーするスクリプトがあります。スクリプトは、実行中の場所とコピーする必要があるファイルを認識しています。

スクリプトはローカル データセンターからファイルをコピーしますlocal_dcが、それがダウンしているか応答していない場合は、リモート データセンターから同じファイルをremote_dc_1コピーし、それもダウンしている場合は、remote_dc_2以下に示すように別のリモート データセンターから同じファイルをコピーします。

ここで、local_dcマシンがダウンしている場合、マシンからファイルをコピーするとremote_dc_1します。そのため、このマシンがダウンしているため、他のリモート マシンからコピーするというメールを送信する必要があります。local_dc マシンがダウンしている場合に複数の電子メールを送信したくないので、最後に電子メールを送信して、ファイルごとに複数の電子メールではなく 1 つの電子メールのみを受信するようにします。

以下は私のスクリプトです -

do_Copy() {
  el=$1
  PRIMSEC=$2
  scp david@"$local_dc":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/. \
    || (a=1 && scp david@"$remote_dc_1":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/.) \
    || (b=2 && scp david@"$remote_dc_2":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/.) \
    || (c=3 && exit 1)
}

export -f do_Copy

parallel --retries 10 -j 10 do_Copy {} $PRIMARY ::: "${PRIMARY_PARTITION[@]}" &
parallel --retries 10 -j 10 do_Copy {} $SECONDARY ::: "${SECONDARY_PARTITION[@]}" &
wait

# problem is this doesn't work at all?
if [ $a -eq 1 ]
then
   echo "Local machine $local_dc was down, so copied from Primary Remote machine $remote_dc_1" | mailx -r "david@host.com" -s "$local_dc machine down" "david@host.com"
fi
if [ $b -eq 2 ]
then
   echo "Primary Remote machine $remote_dc_1 was down, so copied from Secondary Remote machine $remote_dc_2" | mailx -r "david@host.com" -s "$remote_dc_1 machine down" "david@host.com"
fi
if [ $c -eq 3 ]
then
   echo "All three machine's are down. Exiting out." | mailx -r "david@host.com" -s "All three machine's are down" "david@host.com"
fi

変数 a、b、および c を使用してサブシェルで行っていることは何か問題がありますか?

4

1 に答える 1

1

シェルや環境変数を使用してこのステータスを伝播することはできませんが、ファイルシステムのフラグは問題なく機能します。

#!/bin/bash
export status_dir=$(mktemp -t -d transfer.XXXXXX)
cleanup() { rm -rf "$status_dir"; }
trap cleanup 0 # automatically clean up on exit

do_Copy() {
  el=$1
  PRIMSEC=$2
  scp david@"$local_dc":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/. \
    || { touch "$status_dir/local_down" && scp david@"$remote_dc_1":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/.; } \
    || { touch "$status_dir/primary_down" && scp david@"$remote_dc_2":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/.; } \
    || { touch "$status_dir/secondary_down"; exit 1; }
}

...以降...

[[ -e "$status_dir/local_down" ]] && \
   mailx -r "david@host.com" -s "$local_dc machine down" "david@host.com" \
     <<<"Local machine $local_dc was down, so copied from Primary Remote machine $remote_dc_1"

[[ -e "$status_dir/primary_down" ]] && \
   mailx -r "david@host.com" -s "$remote_dc_1 machine down" "david@host.com" \
     <<<"Primary Remote machine $remote_dc_1 was down, so copied from Secondary Remote machine $remote_dc_2"

[[ -e "$status_dir/secondary_down" ]] && \
   mailx -r "david@host.com" -s "All three machine's are down" "david@host.com" \
     <<<"All three machines are down. Exiting out."
于 2015-04-03T01:20:27.547 に答える