Last active
August 29, 2015 14:06
-
-
Save jeremija/a6cb08fc7b7133f591e2 to your computer and use it in GitHub Desktop.
update applications on the server and restart the service without the prompts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Copies file to the server and restarts a service on the server. If you have uploaded | |
# your public ssh key to the server, you should be able to run this without any prompts. | |
# | |
# If your private ssh key is password protected, consider using ssh-add to avoid being | |
# prompted for password every time. | |
# | |
# author jsteiner | |
host=marsh | |
dest=/www/test | |
service=server-public | |
review=0 | |
SCRIPT=`basename $0` | |
src="$1" | |
YELLOW='\e[93m' | |
GREEN='\e[32m' | |
RED='\e[31m' | |
NC='\e[39m' # no-color | |
if [ $# -ne 1 ]; then | |
echo -e $RED"invalid number of arguments!"$NC | |
echo "usage: $SCRIPT <src>" | |
echo " <src> source directory" | |
exit 1 | |
fi | |
echo -e $YELLOW"the following variables are set:"$NC | |
echo " dest = $host:$dest" | |
echo " service = $service" | |
echo " " | |
if [ $review -gt 0 ]; then | |
echo -e $YELLOW"press <enter> to continue or ctrl+c to cancel:"$NC | |
read | |
fi | |
echo -e $YELLOW"checking if <src> is a directory..."$NC | |
if [ ! -d $src ]; then | |
echo -e $RED"error: <src> parameter should be a directory"$NC | |
exit 2; | |
fi | |
echo -e $YELLOW"done!"$NC | |
echo -e $YELLOW"copying files..."$NC | |
scp -p $PORT -r $src/* $host:$dest | |
if [ $? -gt 0 ]; then | |
echo -e $RED"error copying source to destination"$NC | |
exit 3; | |
fi | |
echo -e $YELLOW"done!"$NC | |
echo -e $YELLOW"restarting service $service..."$NC | |
ssh $host "sudo stop $service && sudo start $service" | |
if [ $? -gt 0 ]; then | |
echo -e $RED"error restarting service $service"$NC | |
exit 4; | |
fi | |
echo -e $YELLOW"all done!"$NC |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment