Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2013-05-24
3
Medium Priority
?
474 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 1800 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 200 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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

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 …
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

885 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