Solved

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

Posted on 2013-05-24
3
439 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
It’s 2016. Password authentication should be dead — or at least close to dying. But, unfortunately, it has not traversed Quagga stage yet. Using password authentication is like laundering hotel guest linens with a washboard — it’s Passé.
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…

751 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