TUTORIAL Don't fill up your drive - Purge your Voice Monitor files

krzykat

Telecom Strategist
Joined
Aug 2, 2008
Messages
3,145
Reaction score
1,235
Note: much of this was ripped from threads that @dicko contributed to.

It's nice to have recordings of files, but can kill a system if you forget to purge to them. (I literally found this out firsthand while in Cozumel on a cruise ship. Read panic mode).

Here's a nice little solution to the problem:

Create a file - we'll call ours purgeMON in the /etc directory and give it executable rights (755 will suffice)

Code:
#!/bin/bash

while (( `df /|/usr/bin/tail -1|/usr/bin/awk '{print $5'}|/bin/sed 's/\%//'` > 90 ));do  /bin/rm -f `/bin/find /var/spool/asterisk/monitor/ -name "*.wav" -ls | /usr/bin/awk '{print $11'} | sort -n | /usr/bin/head -n 1` ;done

find /var/spool/asterisk/monitor/ -empty -type d -delete

This will execute grabbing the files in /var/spool/asterisk/monitor directory and organize them oldest first and deleting these files until it is less than 90% of the drive used.

It will then remove any empty directories inside of the monitor folder.

Now to make it automated, edit /etc/crontab and add the lines:

Code:
# Purge Audio Recordings along with empty directories
5 5 * * * root /etc/purgeMON

This will run this file every morning at 5:05am - and insure that you never run out of disk space because of CDR recordings in the monitor directory.
 

kyle95wm

Phone Genius Owner
Joined
Apr 16, 2016
Messages
520
Reaction score
90
I actually wrote a shell script just recently that does this.

UPDATED AS OF APRIL 2ND

Code:
#!/usr/bin/env bash
# This script will delete call recordings every month, but will only delete the recordings for
# the current year, and 3 months prior to running the script.
# For example, if the script were run in April, all call recordings for January will be deleted.
# Additionally, if the script is run with an argument of "last", the script will
# check to see if last year's folder exists, and is empty. If both conditions are met, the script
# deletes the previous year's empty folder

#### EXAMPLE JOB TO PUT INTO /etc/crontab ####
# Run script with no arguments (normal mode) on the first day of every month at 9:30 AM local time
# 30 9 1 * * root /root/purge-recordings.sh > /dev/null 2>&1
# Run script with "last" argument (purge last year's folder if empty) at 10:30 AM local time
# on the first day of the next year
# 30 10 1 1 * root /root/purge-recordings.sh last > /dev/null 2>&1

#set -x # Un-comment to get a complete trace - used for debugging
dir="/var/spool/asterisk/monitor" # The call recordings directory
year="$(date -d '-3 months' +%Y)" # Get year from 3 months ago in XXXX form
month="$(date -d '-3 months' +%m)" # Get month number from 3 months ago in YY form
lastyear="$(date -d '-1 year' +%Y)" # Get last year's year number

# Check if "last" was specified as an argument.
if [ "$1" == "last" ] ; then
    if [ -d "$dir/$lastyear" ] ; then # Check for a directory from last year
        if [ -z "$(ls -A "$dir/$lastyear")" ] ; then # This checks if folder is completly empty
            if rm -rf "${dir:?}/${lastyear:?}" ; then # Test if deletion command exits with 0 status code
                # Send an email alert to notify the admin about successful deletion
                echo "Hi, just letting you know that the empty directory for $lastyear was deleted for server $(hostname -f) with IP $(curl -s icanhazip.com)." | mail -s lastyear [email protected]
            fi
        fi
    fi
    # We will exit to prevent the script from running any further from this point when "last"
    # is provided as an argument. We only want this portion of the script to run in this particular case.
    # If the script were to run normally, this entire section will not be executed.
    exit
fi

if [ -d "$dir/$year/$month" ] ; then # Check if month's directory does exist
    # If True, delete the directory
    if rm -rf "${dir:?}/${year:?}/${month:?}/" ; then # Did our deletion complete successfully?
        # If True, send an email.
        # We also grab the month name instead of number.
        # For example, a message might say "Hi, just letting you know that the
        # recordings for the month of January were purged.
        echo "Hi, just letting you know that the recordings for the month of $(date -d '-3 months' +%b) have been purged." | mail -s purged [email protected]
    else
        # If the job failed, we will send a different email using the same concept as before.
        # We will also tell the administrator which server failed via its IP address.
        echo "Hi, just letting you know that the recordings for the month of $(date -d '-3 months' +%b) were NOT purged. Please log into server $(curl -s icanhazip.com) to investigate." | mail -s FAILED [email protected]
    fi
else
    echo "Hi, just letting you know that there are no recordings for the month of $(date -d '-3 months' +%b) for server $(hostname -f) with IP $(curl -s icanhazip.com). Thank you!" | mail -s nothingtopurge [email protected]
fi
 
Last edited:

Jake

Active Member
Joined
Aug 27, 2010
Messages
419
Reaction score
81
Could this be modified to work with voicemail folders also?
 

dicko

Still learning but earning
Joined
Oct 30, 2015
Messages
1,633
Reaction score
842
Not easily, but there is an example script in the source directory


contrib/scripts/messages-expire.pl

that will do what you want. It will also explain why in fact it is "not easily" done.
 
Last edited:

kyle95wm

Phone Genius Owner
Joined
Apr 16, 2016
Messages
520
Reaction score
90
I updated my script above. Please have a look.
 

dicko

Still learning but earning
Joined
Oct 30, 2015
Messages
1,633
Reaction score
842
I would comment that your 'last' thingy could likely be rewritten as

#cleanup residual empty dirs
find /var/spool/asterisk/ -type d -empty -delete

You might always run that before exiting a script or just as a well timed cron job
 

kyle95wm

Phone Genius Owner
Joined
Apr 16, 2016
Messages
520
Reaction score
90
So I was going through my email today and looking through my spam folder. I'm not sure who is using pbx.integratedprivate.com.au, but I saw a purge email come from your server for call recordings. It seems that in my haste in posting my script above, whoever took it from here forgot to replace my email with theirs. If you're the one who is using my script on your PBX, please change the email. To make sure this doesn't happen again, I've put a placeholder in the script "[email protected]" to tell the user to put their email in those places.
 

hawk#1

Well-Known Member
Joined
Nov 3, 2015
Messages
716
Reaction score
309
Thanks for sharing the scripts. Hopefully whomever is using your email, will realize it and edit the script and remove your email address.
 

John C. Reid

Member
Joined
Sep 2, 2020
Messages
88
Reaction score
28
/etc is for configuration files. An executable script would be better placed in /usr/local/bin - it is a convention thing and not a technical limitation. Still, putting things in the correct, predictable place in the file system makes them easier to find should someone who is not you have to do something with it later.
 

Members online

Forum statistics

Threads
25,810
Messages
167,754
Members
19,240
Latest member
nikko
Get 3CX - Absolutely Free!

Link up your team and customers Phone System Live Chat Video Conferencing

Hosted or Self-managed. Up to 10 users free forever. No credit card. Try risk free.

3CX
A 3CX Account with that email already exists. You will be redirected to the Customer Portal to sign in or reset your password if you've forgotten it.
Top