Shutdown with log shipping (bash)

Shuts down an instance and ships the Wowza logs.


export JAVA_HOME=/usr
export EC2_HOME=/opt/ec2/tools

export date=`date +%m-%d-%y`
export var_loc = $HOME/var
export count=`cat ${var_loc}/repcount.txt`
export start=0
export end=`expr $count - 1`
echo terminating ${count} repeaters from ${start} to ${end}
echo setting shutdown flag...
touch ~/var/shutdown.sem

# instantiate a loop to shut down each repeater

for (( i=0;i<=${end};i++))
export index=${i}
echo Terminating Repeater ${index}
if [ -f ~/var/repeater-${index}.iid ]

# Capture the instance ID
export iid=`cat ~/var/repeater-${index}.iid`

# Capture the instance IP address
export ip=`${EC2_HOME}/bin/ec2-describe-instances ${iid} | grep INSTANCE | cut -f17`

echo Terminating Repeater ${index} at AMI ${iid} on IP address ${ip}
echo Shutting down services...

# Shut down Wowza service, so that the load balancer is notified of the server being stopped
ssh -o StrictHostKeyChecking=no -i ~/.ssh/gsg-keypair.priv.txt root@${ip} service WowzaMediaServer stop

# zip up the logs
echo Archiving logs...
ssh -o StrictHostKeyChecking=no -i ~/.ssh/gsg-keypair.priv.txt root@${ip} gzip /usr/local/WowzaMediaServer/logs/*.log

# ship the logs to a local directory, organized by date and instance ID.
echo Shipping logs...
mkdir -p ${var_loc}/logs/repeaters/${date}/${iid}
scp -o StrictHostKeyChecking=no -i ~/.ssh/gsg-keypair.priv.txt root@${ip}:/usr/local/WowzaMediaServer/logs/*.gz ~/var/logs/repeaters/${date}/${index}

# Now we can actually shut it off.
echo Terminating instance...
${EC2_HOME}/bin/ec2-terminate-instances ${iid}
if [ $? != 0 ]; then
echo Error terminating instance for image ${iid}
exit 1

# Clean up temp files from startup script.

echo Removing temporary data files...
rm ~/var/repeater-${index}.*
rm ~/var/wowza_current_instance_${ip}

echo Instance ${iid} has been terminated.


## End of loop

# check to see if there's anything left over
if [ `ls -l ${var_loc}/repeater-*.iid | wc -l` -gt 0 ]
echo Some AMI ID files still exist. Please manually shut down those instances if necessary

# reset the repeater counter
echo 0 > ${var_loc}/repcount.txt

# remove the shutdown semaphore
rm ${var_loc}/shutdown.sem