Tuesday, September 14, 2010

Purge old voicemails and renumber/rename the voicemail files

1. Write the script to renumber the voicemail files. Why to renumber? (Test yourself: go to /var/spool/asterisk/voicemail/default/{VMbox}/INBOX ; say you have msg0000.xxx and msg0001.xxx, msg0003.xxx in your INBOX. Say you deleted msg00000.xx because it's older than 10 days. Now leave the voicemail message for that VMbox, the new message will appear as msg0000.xxx while it should be appeared as msg0003.xxx (in our case) so that while retrieving the message, older message would be played before the new message. Thus we have a problem. So, what is the solution?

When you delete the old files (say older then 10 days) you need to make sure that you rename the other files accordingly. In this case, after you delete msg0000.xxx (older than 10 days) , you need to rename msg0001.xxx to msg0000.xxx so that other new messages will appear in the appropriate order.

/root# vi renumber.sh

#!/bin/bash

function padder() {

case $1 in
"new")
case ${#new} in
1) newpad=000$new;;
2) newpad=00$new;;
esac;;

"old")
case ${#old} in
1)oldpad=000$old;;
2)oldpad=00$old;;
esac;;
esac

}

new=0
padder new
totalfiles=`find . -type f -name "msg.*txt"|wc -l`

for (( old=0; old<100; old+=1 )); do
padder old
if ( test -e msg$oldpad.txt); then
if ( test -e msg$newpad.txt); then
echo "file already exists";
else
for ext in txt WAV; do mv msg$oldpad.$ext msg$newpad.$ext; done
fi

let new+=1
padder new
if (( $new == $totalfiles )); then break; fi
fi

done




2. Now you have to write the script that can crawl through all of the VMbox and rename the voicemails appropriately.

/root #vi collect-and-rename.sh
#!/bin/bash

basedir=/var/spool/asterisk/voicemail/default

cd $basedir

echo $basedir

for str in `find . -type f -name "msg*.txt"`; do
maindir=`echo "$str"|awk -F/ '{print $2}'`

cd ./$maindir/INBOX
# echo `pwd`
bash /root/renumber.sh

cd $basedir

cd ./$maindir/Old
# echo `pwd`
bash /root/renumber.sh

cd $basedir
done

3. Script to delete files older than 10 days

/root # vi deleteOldVm.sh

#!/bin/bash

$dir=/var/spool/asterisk/voicemail/default
cd $dir
find . -type f -name msg????.??? -mtime +10 -exec -rm {} \;



http://www.oneharding.com/voip/asterisk_voicemail_scripts.html
http://djlab.com/2010/05/automatically-purge-old-voicemail-on-asteriskfreepbxtrixbox/
http://www.vectorsite.net/tsawk_2.html



No comments:

Post a Comment