Solved

Need a PERL script to delete backup files dated exactly  7 days ago.

Posted on 2013-05-24
3
435 Views
Last Modified: 2013-05-24
Hi Team,

   Just need help on a cronjob task i need to do for housekeeping my Centos file system.   I have an application that backups an app database twice a day and these backup files have started accummulating in our /root/backup directory like so.....

MYAPP.db.backup.14-May-2013-12-15AM.sql
MYAPP.db.backup.14-May-2013-12-35AM.sql
MYAPP.db.backup.15-May-2013-12-15AM.sql
MYAPP.db.backup.15-May-2013-12-35AM.sql
MYAPP.db.backup.16-May-2013-12-15AM.sql
MYAPP.db.backup.16-May-2013-12-53AM.sql
MYAPP.db.backup.17-May-2013-12-15AM.sql
MYAPP.db.backup.17-May-2013-12-35AM.sql
MYAPP.db.backup.18-May-2013-12-35AM.sq
MYAPP.db.backup.18-May-2013-12-35AM.sq

Open in new window


I just need help with a PERL script that I can include in a cron job so that it will automatically delete files with file names that have the date of exactly 7 days ago.   In the case of the example above, if today were 24 May 2013, I want the cron job to delete only files MYAPP.db.backup.17-May-2013*   but leave the other dates untouched.

Any help would be appreciated.
0
Comment
Question by:rleyba828
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 450 total points
ID: 39193974
Why perl?

rm /root/backup/MYAPP.db.backup.$(date -d "7 days ago" +"%d-%b-%Y")*.sql

When adding this line to crontab you must escape the % signs (cron restriction)

0 7 * * * /bin/rm /root/backup/MYAPP.db.backup.$(/bin/date -d "7 days ago" +"\%d-\%b-\%Y")*.sql
0
 
LVL 7

Assisted Solution

by:Robert Saylor
Robert Saylor earned 50 total points
ID: 39194075
Here is a shell script I use for this purpose:

#!/bin/bash

FIND=/usr/bin/find
RM=/bin/rm

echo Cleaning backup files older then 4 days

# change the path below to where you want to auto delete
$FIND /backup/extended/* -mtime +4 -exec $RM {} \;

Open in new window

0
 

Author Comment

by:rleyba828
ID: 39195905
Hi woolmilkporc,

   Thanks for the solution.  It worked excellently and is the exact thing I was looking for.  My sysadmin just preferred to standardize on perl but bash will do in this case.

Hi rsaylor, your solution was helpful, but in our case, the date was part of the filename, so we had to work within those constraints, but thanks as well.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I. Introduction There's an interesting discussion going on now in an Experts Exchange Group — Attachments with no extension (http://www.experts-exchange.com/discussions/210281/Attachments-with-no-extension.html). This reminded me of questions tha…
Utilizing an array to gracefully append to a list of EmailAddresses
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question