#!/bin/bash #Rick Meertens #Usage: #By default no customers have a quota. #Only when you put a file (named as the subdirectory of the customer which is containing the voicemails)in the $ASTERISK_QUOTA_CONFIG_DIR. This file must contain 2 values: QUOTA in bytes and $NEVER_DEL_YOUNGER_THAN in days. ##Example of config file ##This is to prevent that all voicemails will be deleted, when 1 file is exceeding the quota. ##Only set this to 0, when the 'General voicemail setting': maxmessage in voicemail.conf is set to an #value that wouldn't exceed the quota or else this script works to perfect. #NEVER_DEL_YOUNGER_THAN=7 ##Customer gets 100 megabytes voicemail quota. #QUOTA=100000000 ASTERISK_QUOTA_CONFIG_DIR="/home/system/asterisk/voicemail_quotas"; #Main directory where all the customers voicemail directories are. SPOOL_DIR="/home/system/asterisk/spool/voicemail"; GetTotalBytesCust() { cust="$@"; du -b -s "$SPOOL_DIR/$cust/" | cut -f1 } for config_file in `ls -1 $ASTERISK_QUOTA_CONFIG_DIR/`; do #check if customer directory exist. if ! [ -r "$ASTERISK_QUOTA_CONFIG_DIR/$config_file" ]; then continue; fi if ! [ -r "$SPOOL_DIR/$config_file" ]; then continue; fi #load settings; NEVER_DEL_YOUNGER_THAN=0; source $ASTERISK_QUOTA_CONFIG_DIR/$config_file; if [ $NEVER_DEL_YOUNGER_THAN -ge 1 ]; then find_cmd="find $SPOOL_DIR/$config_file -iname *wav -mtime +$NEVER_DEL_YOUNGER_THAN -printf %T@\t%p\n"; else find_cmd="find $SPOOL_DIR/$config_file ( -iname *wav ) -printf %T@\t%p\n"; fi while true; do #check if voicemailbox is over her quota. bytes_used=`GetTotalBytesCust $config_file`; if [ $bytes_used -lt $QUOTA ]; then break fi #find the oldest file del_file=`$find_cmd | sort -n | head -1 | cut -f2 `; #nothing to delete? if ! [ "$del_file" ]; then break; fi #delete it del_path="$del_file" echo "deleting $del_path" rm "$del_path"; #delete meta file del_path="`echo $del_path | sed 's/.wav$/.txt/g'`"; echo "deleting $del_path" rm "$del_path"; done done