0

I have a script which will be running in each server and copies certain files into it. Script knows where I am running and what files I need to copy.

Script will copy files from local datacenter local_dc but if it is down or not responding, then it will copy same files from remote datacenter remote_dc_1 and if that is also down, then it will copy same files from another remote datacenter remote_dc_2 as shown below -

do_Copy() {
  el=$1
  PRIMSEC=$2
  scp david@"$local_dc":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/. || scp david@"$remote_dc_1":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/. || scp david@"$remote_dc_2":"$dir3"/new_weekly_2014_"$el"_200003_5.data "$PRIMSEC"/.
}
export -f do_Copy

# copying 5 files in parallel simultaneously in primary and secondary folder
parallel --retries 10 -j 5 do_Copy {} $PRIMARY ::: $primary_partition &
parallel --retries 10 -j 5 do_Copy {} $SECONDARY ::: $secondary_partition &
wait

echo "All files copied successfully."

Now my question is :

  • Let's say if local_dc machine is down, then will it definitely copy from remote_dc_1 machine for sure? And if remote_dc_1 machine is also down, then will it also copy from remote_dc_2?
  • Now if all three machines are down then what will happen? In this case, I want to print the message saying, all three machines are down so exiting out of the shell script.

I cannot test this properly without making the machine down.

  • 1
    A way of testing it without taking the machine down is to add a null route: sudo route add -host remote-server gw non-existing.ip.on.local.net e.g. sudo route add -host 130.228.1.10 gw 192.168.1.248 – Ole Tange Apr 2 '15 at 21:07
  • @OleTange I see but as per your understanding what do you think? Will it work? It will start copying from remote datacenter if local is down? – arsenal Apr 2 '15 at 21:09
1

Yes. It will attempt to copy from the first, if that fails it will try the second and if that fails too, it will try the third. To have it email you at the end, use this:

scp ... || scp ... || scp || echo "All attempts failed" | mailx -r "foo@bar.com"

To have each scp command tell you if it failed, you can use subshells:

scp ... || (echo "machine1 down" | mailx -r "foo@bar.com"&& scp ... ) ||
( echo "machine2 down" | mailx -r "foo@bar.com" && scp ... ) || 
     echo "All attempts failed" | mailx -r "foo@bar.com"
  • understood and If I need to exit out if all attempts failed, then I need to use scp ... || scp ... || scp || (echo "All attempts failed" | mailx -r "foo@bar.com" && exit 1) Right? – arsenal Apr 2 '15 at 23:56
  • @lining yes, that should do it. – terdon Apr 2 '15 at 23:57
1

What will happen will be based on whatever commands you tell your script to run. I am not a master coder, but I recently had a similar situation.

What I did was just to create a few variables for the possible servers to, then use a loop to test connection to those IP, when it finds on that works, set that IP as the one to use (FTPHOST) :

USER=username

PASS=password

attempt1=your.first.server.ip

attempt2=your.second.server.ip

attempt3=your.third.server.ip

curl --connect-timeout 5 ftp://$USER:$PASS@$attempt1 >> /dev/null 2>&1;

if [ $? == '0' ];

then 

    FTPHOST=$attempt1; 

    echo "FTPHOST set to $FTPHOST"

else 

    echo "Connection to $attempt1 failed. Attempting next IP"; 

    curl --connect-timeout 5 ftp://$USER:$PASS@$attempt2 >> /dev/null 2>&1;

    if [ $? == '0' ];

        then

etc. etc., repeat the loop for however many possible IPs you have until it finds on that works. If none work, then of course you won't be downloading anything from there so make your script behave accordingly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.