SRCDS Steam group


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Shel script to zip & move .dem files to web directory
#1
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 Smile

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=# 1 = change owner, 0 = don't change owner

# If CHO=1, specify owner username
USER='owner'

# Change moved files ovner group
CHG=# 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 -"$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 --"$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 --"$zipped" "$1"
            
echo "File moved"
        
fi
        X
=$(( $X ))
        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 [ ! -
"$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 [ ! -"$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 -"$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>&>> /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]
Reply
#2
Looks nice Smile
Reply
#3
I get an error when i tries this bash script



Code:
./dem.sh: 29: Syntax error: "(" unexpected

fixed

changed start of bash script from #!/bin/sh to #!/bin/bash
Reply
#4
Change the Shebang to #!/bin/bash and it should work.
The Shell does not Support $(command) only `command`.
My guess at the writers system /bin/sh is linked to /bin/bash so he did not notice the difference between thise two shells.
Interactive web based config creator for CS, CSS, TF2 and DODS
Creates server and client configs in an explained dialog.

You`ll also find precompiled debian gameserver kernels for download
Reply
#5
(02-21-2011, 01:11 AM)Terrorkarotte Wrote:  Change the Shebang to #!/bin/bash and it should work.
The Shell does not Support $(command) only `command`.
My guess at the writers system /bin/sh is linked to /bin/bash so he did not notice the difference between thise two shells.

Yes, in my system is not difference "/bin/sh" or "/bin/bash" + as i say - a'im noob Sad
[Image: 76561198032233876.png]
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)