02-16-2011, 04:20 PM 
	
	
	
		Hi.
First of all sorry for my bad English.
A few weeks ago I transfer my srcds's from win2k3 and web from vps to may new i7 box with Debian Lenny 64bit.
Now I host 3 srcds servers and web on one server.
Finding the best solution how to move and compress .dem files from srcds to subdomain dir + demo files are placed in the dir's according to map start date for auto web page generation with php script, I understood that need a simple shel script which runs from cron job.
I am a noob in shel scripting, but based on my knowledge of php + googlepower I did it 
 
As a result, there is my demo web: http://demo.spelupasaule.lv/
I decided to share this script, and listen your suggestions if it can be optimized. Now 2 weeks working with no errors.
And here's the script that move and compress files:
To set cron job, I use something like this:
In future, I plan to add auto directory erasing from web which are older than X days.
	
	
	
First of all sorry for my bad English.
A few weeks ago I transfer my srcds's from win2k3 and web from vps to may new i7 box with Debian Lenny 64bit.
Now I host 3 srcds servers and web on one server.
Finding the best solution how to move and compress .dem files from srcds to subdomain dir + demo files are placed in the dir's according to map start date for auto web page generation with php script, I understood that need a simple shel script which runs from cron job.
I am a noob in shel scripting, but based on my knowledge of php + googlepower I did it
 
 As a result, there is my demo web: http://demo.spelupasaule.lv/
I decided to share this script, and listen your suggestions if it can be optimized. Now 2 weeks working with no errors.
And here's the script that move and compress files:
PHP Code:
#!/bin/sh
# Define directories
DEMODIR='/home/user/srcds/css1/orangebox/cstrike'
WEBDIR='/var/www/user/web/demo' 
# To change files and folders ownership (if you need it), you must run this script as root
# Chang moved files owner
CHO=0 # 1 = change owner, 0 = don't change owner
# If CHO=1, specify owner username
USER='owner'
# Change moved files ovner group
CHG=0 # 1 = change group, 0 = don't change
# If CHG=1, specify group name
GROUP='group'
####################################################
DIRDATE=$(date +"%Y.%m.%d")
DFN=$(date +"%Y%m%d")
X=0
####################################################
# Main compress and move not in use files function #
####################################################
function move_ready_file()
{
    local fn=${1##*/}
    if ! fuser -s "$1"; then
        echo "$fn ready to move."
        
        local fndate=`echo | awk '{ print substr("'"$fn"'",6,8) }'`
        if [ "$fndate" == "$DFN" ]; then
            echo "Demo file is today. Compress file"
            local zipped="$WEBDIR/$DIRDATE/$fn.zip" 
            zip -m -T "$zipped" "$1"
            echo "File moved" 
        else
            local fny=`echo | awk '{ print substr("'"$fn"'",6,4) }'`
            local fnm=`echo | awk '{ print substr("'"$fn"'",10,2) }'`
            local fnd=`echo | awk '{ print substr("'"$fn"'",12,2) }'`
            local fnfull="$fny.$fnm.$fnd"
            echo "Demo file is not today ($fnfull). Let's check web dir"
            create_web_dir "$WEBDIR/$fnfull" && echo "Dir $fnfull created" || echo "Dir $fnfull aldeady exist"
            echo "Compress file"
            local zipped="$WEBDIR/$fnfull/$fn.zip"
            zip -m -T "$zipped" "$1"
            echo "File moved"
        fi
        X=$(( $X + 1 ))
        if [ "$CHO" != "0" ]; then
            chown "$USER" "$zipped"
            echo "USER changed"
        fi
        if [ "$CHG" != "0" ]; then
            chgrp "$GROUP" "$zipped"
            echo "GROUP changed"
        fi
    else
        echo "$fn is in use! Leave him."
    fi
}
# Create date directory in web folder if not exist
function create_web_dir()
{
    if [ ! -d "$1" ]; then
        mkdir "$1"
        if [ "$CHO" != "0" ]; then
            chown "$USER" "$1"
        fi
        if [ "$CHG" != "0" ]; then
            chgrp "$GROUP" "$1"
        fi
        return 0
    else
        return 1
    fi
}
##############
# Lets start #
##############
echo ""
echo "-------------------"
echo "   SCRIPT START" 
date +"%Y.%m.%d %H:%M:%S"
echo "-------------------"
echo ""
# Check if script can write to the WEBDIR
if [ ! -w "$WEBDIR" ]; then
    echo "ERROR: Cold not write to web folder: $WEBDIR"
    echo "Verify that you have write access to WEBDIR folder."
    exit 1
fi
# Check if DEMODIR contains at least one .dem file 
COUNT=$(ls -1 "$DEMODIR"/*.dem 2>/dev/null | wc -l)
if [ "$COUNT" != "0" ]; then
    # Create today web folder if needed
    create_web_dir "$WEBDIR/$DIRDATE" && echo "Today dir $DIRDATE created" || echo "Today dir $DIRDATE already exist"
    cd $DEMODIR
    for demofile in *.dem; do
        echo "- - - - -"
        move_ready_file "$demofile"
    done
    echo "-------------------"
    echo "       DONE"
    date +"%Y.%m.%d %H:%M:%S"
    echo "  Moved $X files."
    echo "-------------------"
    echo ""
else
    echo "No demo files in dir."
    echo ""
fi 
To set cron job, I use something like this:
PHP Code:
*/15 * * * * root /patch/to/script/processdemo.sh 2>&1 >> /patch/to/script/processdemo.log 
In future, I plan to add auto directory erasing from web which are older than X days.

 
 
 

 
![[Image: 76561198032233876.png]](http://badges.steamprofile.com/profile/simple/steam/76561198032233876.png)



