< Building kismet from source
archive
A better mousetrap>

(blog entry)

Tuesday 30 January 2007

Auto scp upload directory script

Put this script on your crontab to automatically check a given directory for files, if it finds any they will be uploaded with scp to a given host (e.g. your webhost).
# crontab line

* * * * */usr/bin/webup -l 200 /local/dir user@host:/remote/dir
End result drop a file into a local folder, and it will be uploaded to your website, ideal for uploading loads of media, files are loaded up sequentially with optional upload rate limiting.
#!/bin/bash

# Description 
#  Upload files from a directory to a host using scp
#  (hopefully using public/private key pair auth)
#  Files will be moved to ./complete subdirectory. So
#  folder can be used across samba etc for easy upload
#  to your webhost as drag and drop folder
# 
# Usage
#   webup [-l limitkbps] localfolder user@host:remotefolder 
#
# Warning
#  Be aware this script could upload a php pl asp or other
#  server side script which could then be run via apache
#  - Consider restricting access to localfolder 
#   or remote folder (i.e. chmod, .htaccess respectively)
#
# Requires
#  - Some host to upload with scp
#  - scp available
#  - key pair setup for SSH so there is no password prompt
#
# Uses
#   PID file - /var/run/webup 
#
# Suggested usage
#  Trigger this from a crontab 
#  Example crontab line, check every minute
#
#  * * * * */usr/bin/webup -l 200 /local/dir user@example.com:/remote/dir
#

pidf="/tmp/webup.pid"
default_uplimit_kbps="200"

sub=""
uplimit=""

while getopts l:s: name
    do
        case $name in
        l)
            uplimit="$OPTARG";;
        s)
            sub="$OPTARG";;
        ?)
            echo "Usage `basename $0` host localdir remotedir [limit_kbps]"
            exit 2;;
        esac
    done

shift $(($OPTIND - 1 ))

# Save parameters
localdir=$1
localdircomplete=$localdir/complete
localdirlog=$localdir/log.txt
uhd=$2
localfile=$3

if [ "$sub" = "upload" ]; then

    echo "scp -r -l $uplimit "$localfile" $uhd" >> $localdirlog
    scp -r -l $uplimit "$localfile" $uhd >> $localdirlog

    return=$?

    # Only copy files which copied without fault
    # to complete

    if [ "$return" -eq 0 ]; then
        mv "$localfile" "$localdircomplete" 
    fi;

else

    if [ ! -d $localdir ]; then
        echo "Local directory $localdir does not exist. Exiting";
        exit 1;
    fi;

    # Make complete directory if does not already exist
    mkdir $localdircomplete 2> /dev/null;

    # Save optional limit
    if [ "$uplimit" == "" ]; then
        uplimit=$default_uplimit_kbps
    fi

    if [ -f $pidf ]; then
        echo "Instance `cat $pidf` already running. Exiting"
        exit 1
    fi;

    # create run lock
    echo $$ > $pidf

    # Find all files in localdir and pass to self for
    # upload

    find $localdir -mindepth 1 -maxdepth 1 -name "*" | grep -v "$localdircomplete" | grep -v "$localdirlog" | xargs -I{} $0 -l $uplimit -s upload $localdir $uhd "{}" \;

    # release run lock
    rm -f $pidf

fi
Powered by
Movable Type 4.12