Oct
15
2008




Simple Bash script that copy’s a directory of files and renames them with a Time Stamp

NOTE: I myself am a novice with BASH. I hate scripting, but hey, it’s necessary part of most sysadmin jobs. I came across several hurdles (like changing a name and adding a time stamp in the same loop). I hope this document helps. Please click on my adds, they help pay for the hosting. Thanks and enjoy.

-Matt

—————————————————————–

In this scenario I have a customer that I did a custom Asterisk(Open Source PBX) installation for.  They want the capability to backup all voicemails for management to review.  So naturally I made a cron job that copied Asterisk voicemails to a web directory that was htaccess protected.   It seemed to work except I forgot that asterisk copies messages in the format ‘msg001.wav’.

The problem is that if a user erases the messages in his/her mailbox, then the next new incoming voicemail is named ‘msg001’ again. Next, the voicemail gets copied to the web directory and overwrites the original ‘msg001’.  So my challenge is to create a cron job, that copies the new messages and then renames them on the fly with a new name and an appended time stamp.

Every minute the script copies Asterisk voicemail accounts to another arbitrary directory.   Two loops are run on the contents of that directory matching the pattern ‘5???/mess*'(or in Asterisk syntax 5XXX/mess.).  In the case of my customer their VM boxes are all 5XXX(5001,5002,etc..).  The first loops adds a time stamp from when the voicemail was originally created.  The second loop strips ‘message??’ and replaces it with ‘voicemail_’. The result being something like: ‘voicemail_2008-09-15-03:14:09.wav’. I have to change the messages part because if I dont the first loop will add a second time stamp to previously stamped files.

At the end of the script we want to delete the message00?.txt file, because it is not relevent to simply downloading voicemail and may confuse some users. Also we have to change the owner/group to apache so the web server has permission to host the files.

The voicemail are protected with ‘.htaccess’ file, but with some additional work we could make user access based on PAM, LDAP, or MySQL.

Avaya sells voicemail auditing servers for $40,000 per server. But with the Open Source Asterisk PBX and a simple BASH script we can create a comparable system for under $1,000. However this script can be re-purposed for copying and renaming files for any task. Take care.

-Matt

script:

#!/bin/bash



#set -x



# Variable containing array
#==========================================================================



DIRECTORY1=”/var/spool/asterisk/voicemail/default/????”
DIRECTORY2=”/var/www/html/voicemail/”
AUDIT=”/var/www/html/voicemail/????/INBOX/msg*.wav”
MSGFILES=”/var/www/html/voicemail/????/INBOX/msg????.txt”



# copy files to web directory
cp -ruvp $DIRECTORY1 $DIRECTORY2



#rename those files with a time stamp
for i in $AUDIT ;



do mv $i $i$(date -r $i +%F-%T).wav ;



done



#——————————————————



#strip the ‘messages’ part of the name and replace w/ ‘voicemail’
for i in $AUDIT ;



do mv “$i” “${i/msg????.wav/voicemail_}” ;



done



# remove any msg????.txt files



rm -rf $MSGFILES



chown -R apache $DIRECTORY2
chgrp -R apache $DIRECTORY2

With Apache, Asterisk and this simple BASH script we have just created a web accessible way to check and back up voicemail!

If you like this article tell Google!!









Written by admin in: Asterisk,Asterisk Post,Linux,Scripts | Tags: , , , , , , , , , , ,

No Comments »

RSS feed for comments on this post. TrackBack URL

Leave a comment