1 | #!/bin/bash |
---|
2 | #Rick Meertens |
---|
3 | |
---|
4 | #Usage: |
---|
5 | #By default no customers have a quota. |
---|
6 | #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. |
---|
7 | |
---|
8 | |
---|
9 | ##Example of config file |
---|
10 | ##This is to prevent that all voicemails will be deleted, when 1 file is exceeding the quota. |
---|
11 | ##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. |
---|
12 | #NEVER_DEL_YOUNGER_THAN=7 |
---|
13 | ##Customer gets 100 megabytes voicemail quota. |
---|
14 | #QUOTA=100000000 |
---|
15 | |
---|
16 | ASTERISK_QUOTA_CONFIG_DIR="/home/system/asterisk/voicemail_quotas"; |
---|
17 | |
---|
18 | #Main directory where all the customers voicemail directories are. |
---|
19 | SPOOL_DIR="/home/system/asterisk/spool/voicemail"; |
---|
20 | |
---|
21 | |
---|
22 | GetTotalBytesCust() |
---|
23 | { |
---|
24 | cust="$@"; |
---|
25 | du -b -s "$SPOOL_DIR/$cust/" | cut -f1 |
---|
26 | } |
---|
27 | |
---|
28 | for config_file in `ls -1 $ASTERISK_QUOTA_CONFIG_DIR/`; do |
---|
29 | |
---|
30 | #check if customer directory exist. |
---|
31 | if ! [ -r "$ASTERISK_QUOTA_CONFIG_DIR/$config_file" ]; then |
---|
32 | continue; |
---|
33 | fi |
---|
34 | if ! [ -r "$SPOOL_DIR/$config_file" ]; then |
---|
35 | continue; |
---|
36 | fi |
---|
37 | #load settings; |
---|
38 | NEVER_DEL_YOUNGER_THAN=0; |
---|
39 | source $ASTERISK_QUOTA_CONFIG_DIR/$config_file; |
---|
40 | |
---|
41 | if [ $NEVER_DEL_YOUNGER_THAN -ge 1 ]; then |
---|
42 | find_cmd="find $SPOOL_DIR/$config_file -iname *wav -mtime +$NEVER_DEL_YOUNGER_THAN -printf %T@\t%p\n"; |
---|
43 | else |
---|
44 | find_cmd="find $SPOOL_DIR/$config_file ( -iname *wav ) -printf %T@\t%p\n"; |
---|
45 | fi |
---|
46 | |
---|
47 | while true; do |
---|
48 | #check if voicemailbox is over her quota. |
---|
49 | bytes_used=`GetTotalBytesCust $config_file`; |
---|
50 | if [ $bytes_used -lt $QUOTA ]; then |
---|
51 | break |
---|
52 | fi |
---|
53 | |
---|
54 | #find the oldest file |
---|
55 | del_file=`$find_cmd | sort -n | head -1 | cut -f2 `; |
---|
56 | |
---|
57 | #nothing to delete? |
---|
58 | if ! [ "$del_file" ]; then |
---|
59 | break; |
---|
60 | fi |
---|
61 | |
---|
62 | #delete it |
---|
63 | del_path="$del_file" |
---|
64 | echo "deleting $del_path" |
---|
65 | rm "$del_path"; |
---|
66 | #delete meta file |
---|
67 | del_path="`echo $del_path | sed 's/.wav$/.txt/g'`"; |
---|
68 | echo "deleting $del_path" |
---|
69 | rm "$del_path"; |
---|
70 | done |
---|
71 | done |
---|