Linux Daily Delete Job with Logwatch

I have PBX in a Flash set up to create a daily backup and FTP the file to a Windows server. But how do I get rid of the old backups on the Linux machine, which is running CentOS 5.2? A cron job can do the deletes, but I also want a list of the deleted files delivered with my daily Logwatch 7.3 report.

Here’s what I did:

1. Create the file /etc/cron.daily/dailydelete containing the command below to delete backup files over 30 days old (thanks Matir). It logs output to /var/log/dailydelete.log. Customize backupname to your FreePBX backup name. Change permissions on this file to 0755 to make it executable. In Webmin, check System > Scheduled Cron Jobs:  the file should automatically be included in the daily cron schedule. Update April 19, 2013: The job should be run automatically by anacron. Anacron jobs are not listed individually in the cron schedule.

Important: Omit the –delete action during testing! Add –delete once you are sure you are targeting the correct files.

find /var/lib/asterisk/backups/backupname –mtime +30 -print -delete > /var/log/dailydelete.log

2. Create the directory /etc/logwatch/scripts/services.

3. Create the file /etc/logwatch/scripts/services/dailydelete with this content:

###########################################################################
# $Id: dailydelete, 2010/06/07 Mark Berry MCB Systems Exp $
# Adapted from arpwatch, the simplest service script I could find.
###########################################################################

my $Detail = $ENV{‘LOGWATCH_DETAIL_LEVEL’} || 0;

while (defined($ThisLine = <STDIN>)) {
   chomp($ThisLine);
   next if ($ThisLine eq “”);
   $dailydelete{$ThisLine}++;
}

# Next line changed to work on _any_ Detail level (0 and higher)
if ( ($Detail >= 0) and (keys %dailydelete) ) {
   print “\n”;
   foreach $ThisOne (sort {$a cmp $b} keys %dailydelete) {
      print $ThisOne . “\n”;
   }
}

exit(0);

4. Create the file /etc/logwatch/conf/logfiles/dailydelete.conf with this content. Note that we are not filtering on date since there is no date stamp in the log file:

################################################################
# $Id: dailydelete.conf, 2010/06/07 Mark Berry MCB Systems Exp $
################################################################

# What actual file? Defaults to LogPath if not absolute path….
LogFile = dailydelete.log

# If the archives are searched, here is one or more line
# (optionally containing wildcards) that tell where they are…
#If you use a “-” in naming add that as well -mgt
Archive = dailydelete.log.*

# Expand the repeats (actually just removes them now)
#*ExpandRepeats

# Keep only the lines in the proper date range…
#*OnlyHost
#*ApplyStdDate

5. Create the file /etc/logwatch/conf/services/dailydelete.conf with this content:

#################################################################
# $id: dailydelete.conf, 2010/06/07 Mark Berry MCB Systems Exp $
#################################################################

Title = “dailydelete”

# Which logfile group…
LogFile = dailydelete

Now if all goes well, you should see a dailydelete section in your daily Logwatch email telling you which files have been deleted (should be one per day). Make sure the file date increments from one day to the next:

——————— dailydelete Begin ————————

/var/lib/asterisk/backups/FreePBXBackup/20100506.06.45.01.tar.gz

———————- dailydelete End ————————-

Notes:

  • The main Logwatch files are in /usr/share/logwatch. The /etc/logwatch files are for customizations/overrides.
  • To test Logwatch on today’s files rather than yesterday’s, add this line to /etc/logwatch/conf/logwatch.conf:

Range = today

  • To test (remember to remove “-delete”), run these two commands:

/etc/cron.daily/dailydelete
/etc/cron.daily/0logwatch

  • Remember to add “-delete” to the dailydelete job and comment out “Range = today” when you are done testing.

1 thought on “Linux Daily Delete Job with Logwatch

  1. Pingback: Monitor Asterisk with Logwatch | MCB Systems

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.