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.
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 “.”