Delete faxes in /var/spool/asterisk/fax folder older than 30 days

Is it OK to remove *.pdf and *.tif files that are older than say 30 days from the /var/spool/asterisk/fax folder and subdirectories? We are using the local storage option and I need a way to clean it up.

I can just write a cron job that will remove files older than 30 days but am not sure if that would orphan database entries, etc.

Yes there is database information that would have to be cleaned. You could script off of that though…
mysql in your favorite language:

SELECT faxid,file FROM fax_store WHERE date < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY));

Then iterate:

delete file; then DELETE FROM fax_store WHERE faxid = ‘{FAXID}’;

Perfect. Thanks.

If you don’t mind, can you please share the full script? thanks

find /var/spool/asterisk/fax/* -mtime +30 -exec rm {} \;

Make it a cron job.

That doesn’t delete the database entries…

2 Likes

True, but the database entries are small compared to the actual TIFF images.

The question is, if you don’t delete the database entries, but you delete the files, if it will throw an error.

At best you will have inconsistencies and frustrated users. At worse you will break things and have frustrated users.

2 Likes

Here’s my beta version of the script. I noticed that there were quite a few files left after I ran it that were older than 30 days but didn’t have reference in the db so I’m thinking of adding a find with delete at the end to clean those up:

#!/bin/bash
#
# This script will remove all faxes from the Asterisk DB and the file system that are older than 30 days
# Written by Tim Gross
#
mysql -h localhost -D asterisk -NBe “SELECT faxid,file FROM fax_store WHERE date < UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY));” | while read -r faxid filename
do
tiffilename=“/var/spool/asterisk/fax/“basename $filename | sed 's/.pdf//'”.tif”
rm -f $filename
mysql -h localhost -D asterisk -NBe “DELETE FROM fax_store WHERE faxid = ‘$faxid’;”
# mysql -h localhost -D asterisk -NBe “SELECT * FROM fax_store WHERE faxid = ‘$faxid’;”;
if [ -f $tiffilename ]; then
echo “<$faxid> <$filename> <$tiffilename>”
rm -f $tiffilename
else
echo “<$faxid> <$filename>”
fi
done

I did the following to clean up the orphaned files and may add these to the end of the script:
find /var/spool/asterisk/fax/ -name “.pdf" -mtime +30 -delete
find /var/spool/asterisk/fax/ -name "
.tif” -mtime +30 -delete

Probably not a problem in this case, but be very careful with ever using “.” in a sed ‘substitute’ it will match ANY character , you should use "\." to match the literal “.”

Same with “find”

dicko,

Right you are. Thanks for pointing that out.

if you are using fax_pro, you have to remove them from the database as well

I am:
mysql -h localhost -D asterisk -NBe “DELETE FROM fax_store WHERE faxid = ‘$faxid’;”

1 Like

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.