Oct
15
2008
0

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!

Oct
15
2008
0

Fedora Core 7 Xen Paravirtualized Guest Install

NOTE: I myself am a novice with Xen. This setup worked well for me, but I cannot guarantee results. I came across several hurdles and have worked through most issues. I hope this document helps. Please click on my adds, they help pay for the hosting. Thanks and enjoy.

-Matt

This is a standard fedora 7 installation with current yum updates; in this setup we have two interfaces. One interface(eth0) provides ethernet connectivity to Dom0 (the master domain from which guest are created). The second interface, is for connecting to the paravirtualized guest. Both network cards are plugged into the same switch.

We will use the standard fedora linux networking mechanism rather than letting xen setup up our interfaces. The advantage to this is that we can easily set up multiple NIC’s, connected to Different LANs with separate firewall rules configured by the individual paravirtualized guest.

Hardware:

Intel Super Micro mainboard

Intel Corporation 82566DM-2 Gigabit Network (does not work with xen kernel 2.6.21-7.fc7xen)

– added 3com 3c905 100BaseTX ethernet card

– added Realtek RTL-8139/8139C/8139C+ ethernet card

Intel(R) Core(TM)2 Duo CPU E6550@2.33GHz

OS:

kernel 2.6.21-7.fc7xen

Step 1: Install libvirt and virt-manager

[root@mattcom1 ~]$ yum install libvirt

and

[root@mattcom1 ~]$ yum install virt-manager

———————————————————————

Step 2: prepare installation media

Now we have to create a fedora 7 source tree for installation. One caveat, that I didn’t get at first was how to create the Fedora 7 source tree. The virt-manager tool will expect a standard source tree setup, but doesn’t tell you what that is…

I chose to install from the http, but failed when I copied the files from the Fedora DVD. After some googling I found that the directory for your web server needs to be exactly like this, ‘127.0.0.1/fedora/linux/core/7/i386/os/<files from the fedora dvd>’. Believe it or not, it wouldn’t take the install files in any other directory structure.

In Fedora 7 the default apache web root is ‘/var/www/html/’. So your full path will look like this ‘/var/www/html/fedora/linux/core/7/i386/os/<files from fedora DVD>’. It took me a while to find this information online…

step 3

I like to use the virt-manager to create guest machines. virt-manager is a graphical interface for libvirt. Libvirt is an API for interacting with Xen and other hypervisors. It is defiantly buggy though. Libvirt and Many times I’ll shut down a guest and the virt-manager still shows it running. If this happens do:

[root@mattcom1 ~]# service xend restart
[ OK ]

Then virt-manger will be fine. One other thing, virt-manger has a built in VNC client, but in my testing it failed. More on that later.

Written by mattb in: Uncategorized | Tags: , ,