1
\$\begingroup\$

I have a Bash script that resides on Remote Server A and transfers files from Remote Server 1 to Remote Server 2. Remote Server 2 is a Dropbox folder so in this script, I call another script (that's not mine) to interface with the Dropbox API.

This is one of my 1st forays into the Bash-scripting world (I'm mostly a designer and front-end guy), so I'd love to hear some input on how close to "best practices" I've come. Also, I'm not 100% positive I've handled errors correctly (using a combination of trap and checking various commands' return signals). And of course, if there are any blaring performance or security issues, let me know. Thanks in advance.

#!/usr/bin/env bash


clean_up () {
    ARG=$?

    if mount | grep $localPath > /dev/null; then
        fusermount -u $localPath
    fi

    if [ -d $localPath ]; then
        rm -rf $timestamp
    fi

    if [ ${#errors[@]}  -gt 0 ] ; then
        figlet -f small Error
        (   IFS=$'\n'; echo "${errors[*]}" )
        echo "***"
        echo "This is not typical - please copy the error message above and  notify your developer immmediately."
        echo "***"
    else
        figlet -f small Success
        echo "Upload Complete - Files now accessible via Dropbox"
    fi

    echo
    echo

    exit $ARG
}
trap clean_up EXIT


timestamp=$(date +%Y.%m.%d-%H.%M.%S)
localPath=$PWD/$timestamp

token="MY_TOKEN"
sourcePath=chef/revel
destPath=/chef_uploads
pattern=*.txt
user=me
server=example.com
DIR=/chef_uploads

errors=()

mkdir $timestamp

sshfs $user@$server:$sourcePath ${localPath}
if [ $? -eq 0 ]; then
    echo
    echo
    echo "Transferring $timestamp to $destPath"
    ./dropbox_uploader.sh -p -h upload "$timestamp/" "$destPath/"
    if [ $? -gt 0 ]; then
        errors+=(" !! ERROR: cannot copy files to $timestamp.")
    fi
else
    errors+=("     ERROR: cannot mount remote filesystem - $timestamp")
fi
\$\endgroup\$

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.