Reputation: 11709
I have a tar (abc.tar.gz)
file in my machineA
which I need to copy and untar it in my machineB
. In my machineB
, I have an application running which is part of that tar file so I need to write shell script which will do these below things on machineB
by running the shell script on machineA
-
sudo stop test_server
on machineB
/opt/data/
folder leaving that tar file.machineB
inside this location /opt/data/
/opt/data/
folder.sudo chown -R posture /opt/data
sudo start test_server
In machineA
, I will have my shell script and tar file in the same location. As of now I have got below shell script by reading the tutorials as I recently started working with shell script.
#!/bin/bash
set -e
readonly SOFTWARE_INSTALL_LOCATION=/opt/data
readonly MACHINE_LOCATION=(machineB) # I will have more machines here in future.
readonly TAR_FILE_NAME=abc.tar.gz
ssh david@${MACHINE_LOCATION[0]} 'sudo stop test_server'
ssh david@${MACHINE_LOCATION[0]} 'sudo rm -rf /opt/data/*'
sudo scp TAR_FILE_NAME david@${MACHINE_LOCATION[0]}:$SOFTWARE_INSTALL_LOCATION/
ssh david@${MACHINE_LOCATION[0]} 'tar -xvzf /opt/data/abc.tar.gz'
ssh david@${MACHINE_LOCATION[0]} 'sudo chown -R posture /opt/data'
ssh david@${MACHINE_LOCATION[0]} 'sudo start test_server'
Did I got everything right or we can improve anything in the above shell script? I need to run the above shell script in our production machine.
Is it possible to show the status message for each step, saying this step got executed successfully so moving to next step otherwise terminate out of the shell script with the proper error message?
Upvotes: 0
Views: 73
Reputation: 189739
You can do it all in a single ssh
round-trip, and the tarball doesn't need to be stored at the remote location at all.
#!/bin/bash
set -e
readonly SOFTWARE_INSTALL_LOCATION=/opt/data
readonly MACHINE_LOCATION=(machineB)
readonly TAR_FILE_NAME=abc.tar.gz
ssh david@${MACHINE_LOCATION[0]} "
set -e
sudo stop test_server
sudo rm -rf '$SOFTWARE_INSTALL_LOCATION'/*
tar -C '$SOFTWARE_INSTALL_LOCATION' -x -v -z -f -
sudo chown -R posture '$SOFTWARE_INSTALL_LOCATION'
sudo start test_server" <"$TAR_FILE_NAME"
This also fixes the bugs that the tarball would be extracted into your home directory (not /opt/data
) and you lacked a dollar sign on the interpolation of the variable TAR_FILE_NAME
.
I put the remote script in double quotes so that you can use $SOFTWARE_INSTALL_LOCATION
inside the script; notice how the interpolated value is interpolated in single quotes for the remote shell (this won't work if the value contains single quotes, of course).
You could perhaps avoid the chown
if you could run the tar
command as user posture
directly.
Adding echo
prompts to show progress is a trivial exercise.
Things will be a lot smoother if you have everything -- both ssh
and sudo
-- set up for passwordless access, but I believe this should work even if you do get a password prompt or two.
(If you do want to store the tarball remotely, just add a tee
before the tar
.)
Upvotes: 1
Reputation: 1748
You can generate status message for each step using exit status of ssh command like I tried with first two ssh command.
#!/bin/bash
set -e
readonly SOFTWARE_INSTALL_LOCATION=/opt/data
readonly MACHINE_LOCATION=(machineB) # I will have more machines here in future.
readonly TAR_FILE_NAME=abc.tar.gz
# An error exit function
function error_exit
{
echo "$1" 1>&2
exit 1
}
if ssh david@${MACHINE_LOCATION[0]} 'sudo stop test_server'; then
echo "`date -u` INFO : Stopping test_Server" 1>&2
else
error_exit "`date -u` ERROR : Unable Stop test_Server! Aborting."
fi
if ssh david@${MACHINE_LOCATION[0]} 'sudo rm -rf /opt/data/*'; then
echo "`date -u` INFO : Removing /opt/data" 1>&2
else
error_exit "`date -u` ERROR : Unable to remove! Aborting."
fi
sudo scp TAR_FILE_NAME david@${MACHINE_LOCATION[0]}:$SOFTWARE_INSTALL_LOCATION/
ssh david@${MACHINE_LOCATION[0]} 'tar -xvzf /opt/data/abc.tar.gz'
ssh david@${MACHINE_LOCATION[0]} 'sudo chown -R posture /opt/data'
ssh david@${MACHINE_LOCATION[0]} 'sudo start test_server'
Password management you can create ssh pass-wordless authentication. http://www.linuxproblem.org/art_9.html
Upvotes: 1