Saturday, May 11, 2013

Archive script for FreePBX recordings (Asternic recordings)

Hi guys, just setup cron for this script and it will at the time you specify do a move, copy or delete files based on n number of days. If run manually, it will be like this; say I want to archive any files or directories older than 14 days, I can run it like this;

# archivefiles 14

This will set actions on folders older than 14 days. Remember, for folders, it will not traverse within each files inside folders to determine its age, it will only check the subfolders within the specified folder. However, you can specify the depth but that’s not recommended to change to other than 1. Modify as you wish, its yours to test. If you want to check the output, do a copy first, don’t do a move, thus MOVE=0 must be set. The newer Asternic and Asterisk recordings do store files by directories and not mixed up inside

Just copy paste into into an appropriate location and either setup a cronjob, e.g. below, or run manually from CLI

Crontab daily..

@daily /usr/local/sbin/archivefiles 14

Copy paste the script below, make it executable by #chmod +x archivefiles

 

#!/bin/bash
# sanjayws@gmail.com
# usage #archivefiles 14 (meaning archivefiles will find files older than 14 days to do a move or copy)
#
# DEFINE THESE
SOURCEPATH=/var/spool/asterisk/monitor-mp3/
# With trailing / (slash )
SOURCEFILENAME=*
# filetypes to archive when archiving by main folder files .e.g *.mp3 for mp3 only files
DEST=/usr/usbhdg1/asterisk-recordings/
# With trailing / (slash )
MOVE=1
# NOTE:0 - copy, 1 - move, 2 - delete
#
# START THE SCRIPT
# CHECK IF OLDER THAN VARIABLE DEFINED
#
OLDERTHAN=$1
if [[ $OLDERTHAN = "" ]]; then
        echo "Define a period. Like 14 for older than 14 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 | head -n 1 `
if [[ $grabfiles != "" ]]; then
        echo "OK - Source files exist"
else
        echo "Oops, no source files, check your settings...quitting"
        exit 1
fi
       
# CHECK IF DESTINATION EXISTS
#
if [[ $MOVE = "1"  ]];then
   
    #first way by files
    #find  $SOURCEPATH$SOURCEFILENAME -maxdepth 0 -mtime +$OLDERTHAN -exec mv -f {} $DEST \;
    #second way by directory
    for i in `find $SOURCEPATH -maxdepth 1 -type d -mtime +$OLDERTHAN -print`; do mv -f $i $DEST; done
   
elif [[ $MOVE = "0" ]]; then

    #first way by files
    #find  $SOURCEPATH$SOURCEFILENAME -maxdepth 0 -mtime +$OLDERTHAN -exec cp -f {} $DEST \;
    #second way by directory
    for i in `find $SOURCEPATH -maxdepth 1 -type d -mtime +$OLDERTHAN -print`; do cp -f $i $DEST; done
   
elif [[ $MOVE = "2" ]]; then

    #first way by files
    #find  $SOURCEPATH$SOURCEFILENAME -maxdepth 0 -mtime +$OLDERTHAN -exec rm {} $DEST \;
    #second way by directory
    for i in `find $SOURCEPATH -maxdepth 1 -type d -mtime +$OLDERTHAN -print`; do rm -r $i $DEST; done
fi   


exit 0

3 comments:

Unknown said...

Nice replies in return of this query with solid arguments and explaining everything on the topic of that.
quality control

gateway customer care number said...

Wonderful blog & good post.Its really helpful for me, awaiting for more new post. Keep
Blogging!

Jose Tapia said...

Great Info, thanks for share