Solved

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

Posted on 2013-05-24
3
431 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 6

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

896 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now