LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
LinkBack Search this Thread
Old 07-15-2011, 01:04 AM   #1
Rk_Raj
LQ Newbie
 
Registered: Nov 2010
Posts: 3

Rep: Reputation: 0
Delete backups older than 7 days through shell script


Hi friends,

I am having my own testing server in which mySQL database will be backed up daily. in the format mysql_backup_dd/mm/yy.tar.gz in my home folder

I need to setup a cron job to delete the backups older than 7 days. please tell me how to do this.
 
Old 07-15-2011, 01:22 AM   #2
EricTRA
Guru
 
Registered: May 2009
Location: Barcelona, Spain
Distribution: LMDE Gnome with Awesome WM + Kernel 3.3.0-1 amd64
Posts: 6,529
Blog Entries: 1

Rep: Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219Reputation: 1219
Hello,

The easiest way is to write a Bash script that goes through the files in the backup directory, compares the date with the date saved in the file name and deletes if older then 7 days. Or just delete all backup files older then 7 days with a find command in combination with -exec. Have a look at the man page for find, more in particular the mtime option. Then you can run that script through a cron job.

Kind regards,

Eric
 
Old 07-15-2011, 09:06 AM   #3
lithos
Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA, trying desktop Fedora & openSuse
Posts: 850

Rep: Reputation: 133Reputation: 133
Using find command it would be like:
Code:
find . -name 'mysql_backup_*' -mtime +7 -delete
which would search for All mysql_backup_..... files within the current dir and subdirs and if found then delete it.

Works for me.
 
Old 07-15-2011, 10:51 AM   #4
theNbomr
Senior Member
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 4,504

Rep: Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602Reputation: 602
Sounds like a job for logrotate. Technically, it isn't a log file, but the type of data doesn't really matter.
--- rod.
 
  


Reply

Tags
delete, older, shell script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell script to copy all folders older than 7 days from Linux to Windows by samba blackthu80 Linux - General 4 03-17-2011 12:29 AM
script to compress a directory and then delete older than 7 days replica88 Linux - General 3 02-03-2010 07:21 PM
Automated script to DELETE files older than 2 days in a Particular folder siddhartha_ece2004 Linux - Newbie 14 07-11-2008 05:46 AM
shell script - auto ftp files that older than 60 days bakeng Linux - Newbie 2 12-25-2007 10:35 PM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 08:30 AM


All times are GMT -5. The time now is 03:24 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration