Tuesday, December 7, 2010

Asterisk Script for Recordings to Move or Copy -n- number of days old (an archiving tool if you may)


Here’s' a script that moves or copies certain files (in my case Asterisk recordings) of anything above a certain date (as defined in the CLI) to a destination you pick.
You must first define the sourcepath, sourcefilename, destination and an option to flag move or just copy.
Place it anywhere and execute ./archivefiles 10
-> which means move/copy files that are over 10 days old from date to a desired location.
You could of course run it in cron if you like on a regular basis. Say you run ./
Here’s the script. Copy from the start till the end.
1) nano /usr/bin/archivefiles # create it
2) paste start –> end of script below # copy the script
3) chmod +x /usr/bin/archivefiles # make it executable
NOTE: Make sure your date/time in server is consistent/correct.
###start###
#!/bin/bash
# sanjay@astiostech.com
# usage #archivefiles 10 (meaning archivefiles will find files older than 10 days to do a move or copy)
#
# DEFINE THESE
SOURCEPATH=/var/spool/asterisk/monitor/                                 # With trailing / (slash )
SOURCEFILENAME=*.wav                                                 # filetype
DEST=/usr/src/mytestdst/                                                 # With trailing / (slash )
MOVE=0                                                                # NOTE:0 - copy, 1 - move
#
#
#
# START THE SCRIPT
# CHECK IF OLDER THAN VARIABLE DEFINED
#
OLDERTHAN=$1
if [[ $OLDERTHAN = "" ]]; then
        echo "Define a period. Like 10 for 10 days"
    exit 1
else
        echo ""
fi
# CHECK IF VALUE ENTERED IS AN INTEGER
#
if [ $OLDERTHAN ]; then
    if [ ! $(echo "$OLDERTHAN" | grep -E "^[0-9]+$") ]; then
        echo $OLDERTHAN is not a valid integer.
        exit 1
    else
    if ! [ $OLDERTHAN -ge 0 ] || ! [ $OLDERTHAN -le 10000 ]; then
        echo $OLDERTHAN is an invalid value. Range is [1-10000]
        exit 1
    fi
    fi
fi
# CHECK IF SOURCE DIRECTORY EXIST
#
if [[ -d $SOURCEPATH ]]; then
        echo "OK - Source directory exits"
        cd $SOURCEPATH
else
        echo "Source Directory not found, check your settings... quitting"
        exit 1
fi
grabfiles=`ls -l  $SOURCEPATH$SOURCEFILENAME | head -n 1 | grep -c "\-r"`
if [[ $grabfiles != "0" ]]; then
        echo "OK - Source files exist"
else
        echo "Oops, no source files, check your settings...quitting"
        exit 1
fi
       
# CHECK IF DESTINATION EXISTS
#
if [ -d $DEST ]; then
    echo "OK - Destination directory exits"
else
    echo "Destination directory not found, check your settings... quitting"
    exit 1
fi
# START OUR JOB, DEFINE ACTIONS
#
if [[ $MOVE = "1"  ]];then
   
    find  $SOURCEPATH$SOURCEFILENAME -mtime +$OLDERTHAN -exec mv -f {} $DEST \;
else
    find  $SOURCEPATH$SOURCEFILENAME -mtime +$OLDERTHAN -exec cp -fpr {} $DEST \;
fi   
###end###

4 comments:

asterisk21st said...

Tried to get this working but repeated failed attempts with error below....

OK - Source directory exits
OK - Source files exist
OK - Destination directory exits
find: invalid argument `+' to `-mtime'

JayWS said...

Give more details like your OS platform, etc....

asterisk21st said...

centos 5.5
freepbx 2.9
asterisk 1.8.1.3

asterisk21st said...

Would you like any more information to troubleshoot this?