I wrote a simple script to retrieve the logs from my website, and place them in a subdirectory called logs
. It fetches the logs using sftp. I would like help with the error handling. I'm sure that there are errors I did not see, and I would like to make it so that it fails gracefully.
#!/bin/bash
# gets the logs
# Location to place downloaded logs
PlaceLogs=./logs
# ssh connection to sftp files from
SSHLocation=hjsblog
# Location and pattern of logs
GetLogs=../logs/access*
#######################################
#######################################
CWD=$(pwd)
# move to get location
if [ "$(ls -A $PlaceLogs)" ]
then
echo $PlaceLogs is not empty
return
fi
mkdir -p $PlaceLogs
cd $PlaceLogs
# perform the get operation
sftp $SSHLocation << EOF
cd $(dirname $GetLogs)
get $(basename $GetLogs)
EOF
# uncompress files
bunzip2 *.bz2
# return to script dir
cd $CWD