<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=269747997384049&amp;ev=PageView&amp;noscript=1">

AVIO Insights

Automatically cleanup those archive files

Spring cleanup of archive files!

 

During a project, we casually gathered waaaay too many archive files.  These were intentionally stored, however, in development and testing environments, they became a nuisance, and took up too much space.  Occasionally the Ops team would send out an email saying that disk space was full, and it was time to do some cleanup.

I created a fairly simple script (see below) that searches some directories for specific files/file types, and lists them out, or optionally deletes them.  After creating the script for my specific requirement, I went back and added options for more re-usability.  In our scenario, we were storing some temp files under a '/mnt/edi/tmp' directory, and archives under a 'staging' directory.

Using the 'find' command under Linux, we were able to locate files that exceeded a certain age, and then delete them.

 

**You will need to modify the script to point to your specific directories!

 

Here are some common usages:

Help

Take a look at the command options.

:~/scripts$ ./cleanup.sh -h
************************************
Cleanup command line options:
-d : actually delete the files.
-l : list the files for deletion.
-p : does not prompt for deletion.
-f : forces the deletion.
-t n : where n is number of days old. Defaults to 7
-h : displays these instructions.
************************************

List

List out the files that would be deleted.

 :~/scripts$ ./cleanup.sh -l
Thu Aug 3 09:27:17 MDT 2017 Looking for files older than 7 days.
checking /mnt/edi/tmp directory. 4 files found for destruction
checking /mnt/edi/Eligibility/*/staging directories. 0 files found for destruction

Files to delete...
/mnt/edi/tmp/FS0A9B8D1115D7F4987590000038F07287.payload
/mnt/edi/tmp/FS0A9B8D1115D7B03AB080000038F06582.payload
/mnt/edi/tmp/FS0A9B8D1115D7F4988260000038F07296.payload
/mnt/edi/tmp/FS0A9B8D1115D7F4988150000038F07293.payload

Delete

Actually, delete the files (add the -p command to not prompt).

:~/scripts$ ./cleanup.sh -dp
Thu Aug 3 09:29:00 MDT 2017 Looking for files older than 7 days.
checking /mnt/edi/tmp directory. 10 files found for destruction
checking /mnt/edi/Eligibility/*/staging directories. 0 files found for destruction

*******************************
Deleting files.
*******************************

 

Cleanup.sh bash script

Please, please test this thoroughly before applying it to your environment!

#!/bin/bash

prompt="-i"
mtime="7"
while getopts "dlhpft:" opt;
do
case $opt in
d) doDelete="true"
;;
l) listfiles="true"
;;
h) listhelp="true"
;;
p) prompt=""
;;
f) force="-f"
;;
t) mtime="$OPTARG"
;;
esac
done

if [ "$listhelp" == "true" ];
then
echo "************************************"
echo "Cleanup command line options:"
echo " -d : actually delete the files."
echo " -l : list the files for deletion."
echo " -p : does not prompt for deletion."
echo " -f : forces the deletion."
echo " -t n : where n is number of days old. Defaults to 7"
echo " -h : displays these instructions."
echo "************************************"

exit
fi

echo "$(date) Looking for files older than" $mtime "days."
echo "checking /mnt/edi/tmp directory. " `find /mnt/edi/tmp -name *.payload -mtime +$mtime | wc -l` " files found for destruction"
echo "checking /mnt/edi/Eligibility/*/staging directories. " `find /mnt/edi/Eligibility/*/staging -name "*.*" -mtime +$mtime | wc -l` " files found for destruction"

if [ "$listfiles" == "true" ];
then
echo ""
echo "Files to delete..."
find /mnt/edi/tmp -name *.payload -mtime +$mtime
find /mnt/edi/Eligibility/*/staging -name "*.*" -mtime +$mtime
fi

if [ "$doDelete" == "true" ];
then
echo ""
echo "*******************************"
echo " Deleting files."
echo "*******************************"
find /mnt/edi/tmp -name *.payload -mtime +$mtime -exec rm $prompt $force {} \;
find /mnt/edi/Eligibility/*/staging -name "*.*" -mtime +$mtime -exec rm $prompt $force {} \;
fi

 

Bonus Option: Automatically Schedule this using Cron!

Execute 'crontab -e' and add an entry to run this automatically.  This sample entry runs the script every day at noon, deleting, non-prompting, and listing out the files while logging all actions to a cleanup.log file!

0 12 * * * /home/oracle/scripts/cleanup.sh -dpl >> /home/oracle/scripts/cleanup.log 2>&1

 

Also, check out how to Automatically Rotate Managed Server Log Files!

2 minute read