How to copy only specific files uploaded through FTP

Today I’m going to show you a nice way to sync only specific files from user directories to a “fast speed download” server.

Basically, all my users upload resources (maps, sounds, models, etc) for a Counter Strike 1.6 Server through FTP. The issue was the fact that all these files had to be copied to a webserver, in order to be served using “sv_downloadurl “http://fast.howtodoityourself.org/cs” option from Counter Strike server.

And, of course, most of the users upload also crap on FTP in their directories and most of the times in different locations. So I used the option from PURE FTP server to use an upload script that is ran each time a user uploads a file/directory.

So, I created a script that checks for .res, .bsp, .wav, .mdl, .wad files and copies them on the high speed upload server at the correct location, meaning that any .bsp file will go to /var/www/cs/maps/ directory, any .wav file will end up in /var/www/cs/sound directory and so on.

Below, I provide you the full script, you may use it any way you want, of course I do not take any kind of responsibility for any loss you or your “victim” may suffer ๐Ÿ™‚

By the way, for this you will need to have installed lftp client!

#! /bin/bash
INPUT=$1
LOG="/var/log/upload-script.log"
echo "Script BEGIN" >> $LOG
echo $INPUT >> $LOG
FULLPATH=${INPUT%/*}
echo "The path to the filename is $FULLPATH" >> $LOG
FILENAME=${INPUT##*/}
echo "The complete filename is $FILENAME" >> $LOG
BASENAME=${FILENAME%%.*}
echo "The name of the file (without the extension) is $BASENAME" >> $LOG
EXT=${FILENAME#*.}
echo "The actual extension is $EXT" >> $LOG

#We now truncate the extension to lowercase
EXT=`echo $EXT | tr '[A-Z]' '[a-z]'`
echo "The extension in lowercase is $EXT" >> $LOG

SWITCH="yes"
	if [[ $EXT = "bsp" ]]; then
		WAY=cs/maps
		echo "This is a map file" >> $LOG
		echo "The path to map files is $WAY" >> $LOG
	elif [[ $EXT = "wav" ]];then
		WAY=cs/sound
		echo "This is a sound file" >> $LOG
		echo "The path to sound files is $WAY" >> $LOG
	elif [[ $EXT = "spr" ]];then
		WAY=cs/sprites
		echo "This is a sprite file" >> $LOG
		echo "The path to sprite files is $WAY" >> $LOG
	elif [[ $EXT = "res" ]];then
		WAY=cs/resource
		echo "This is a resource file" >> $LOG
		echo "The path to resource files is $WAY" >> $LOG
        elif [[ $EXT = "mdl" ]];then
                WAY=cs/models
                echo "This is a model file" >> $LOG
                echo "The path to model files is $WAY" >> $LOG
	elif [[ $EXT = "wad" ]];then
		WAY=cs
		echo "This is a wad file" >> $LOG
		echo "The path to wad files is $WAY" >> $LOG
	else
	        echo "FUCK the dumb user" >> $LOG
		echo "The uploaded file has no reason to be on fast download server" >> $LOG
		SWITCH="no"
	        exit 1
	fi
	
HOST='howtodoityourself.org'
USER='myuser'
PASS='mypassword'
TDIR='/var/www'

if [ "$SWITCH" = "yes" ];then
TDIR="${TDIR}/${WAY}"
SDIR="/tmp/for_sync"

if [ -d $SDIR ];then
	echo "Sourcedirectory already exists" >> $LOG
else
	echo "Creating source directory $SDIR" >> $LOG
	mkdir $SDIR
fi
cp $INPUT $SDIR;

lftp -f "
open $HOST
user $USER $PASS
mirror --reverse --only-missing --Remove-source-files $SDIR $TDIR
bye
"
echo "UPLOAD SUCCESSFUL!" >> $LOG
fi

echo "Script END" >> $LOG
echo >> $LOG
echo >> $LOG

Of course, if there is anything that you do not understand, please post your question using the comments form.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.