LinuxQuestions.org
Review your favorite Linux distribution.
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
 
Search this Thread
Old 07-15-2011, 02: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.
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 07-15-2011, 02:22 AM   #2
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291Reputation: 1291
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, 10:06 AM   #3
lithos
Senior Member
 
Registered: Jan 2010
Location: SI : 45.9531, 15.4894
Distribution: CentOS, OpenNA/Trustix, testing desktop openSuse 12.1 /Cinnamon/KDE4.8
Posts: 1,144

Rep: Reputation: 217Reputation: 217Reputation: 217
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.
 
2 members found this post helpful.
Old 07-15-2011, 11:51 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,396
Blog Entries: 2

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
Sounds like a job for logrotate. Technically, it isn't a log file, but the type of data doesn't really matter.
--- rod.
 
Old 04-09-2013, 03:47 AM   #5
eccenux
LQ Newbie
 
Registered: Apr 2013
Posts: 1

Rep: Reputation: Disabled
Sorry for digging out old thread, but it came up in Google...

For non-recursive search you need `-maxdepth 1` parameter, not `-xdev` (very different thing). You should also make sure you are deleting files and so you should add `-type f`.

So the command might look like this:
Code:
find /home/backup/mysql_*.gz -maxdepth 1 -type f -mtime +7 -delete
You could also use `-iregex` to control matches a bit more. I personally like POSIX RegExp (like in e.g. JavaScript). With regexp the command might look like this:
Code:
find /home/backup/mysql_*.gz -maxdepth 1 -regextype posix-extended -type f -mtime +7 -iregex '.+-[0-9]{4}0[^16][0-9]{2}-[0-9]{4}\.tar\.gz' -delete
Above will not match e.g. ...-20120131-... and so preserve January and June backups. Correct RegExp depends on your date/time format.
 
  


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


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 01:29 AM
script to compress a directory and then delete older than 7 days replica88 Linux - General 3 02-03-2010 08:21 PM
Automated script to DELETE files older than 2 days in a Particular folder siddhartha_ece2004 Linux - Newbie 14 07-11-2008 06:46 AM
shell script - auto ftp files that older than 60 days bakeng Linux - Newbie 2 12-25-2007 11:35 PM
script to auto delete files older than X days nocnoc Programming 17 12-06-2006 09:30 AM


All times are GMT -5. The time now is 02:25 PM.

Main Menu
Advertisement
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 Google+: linuxquestions
Open Source Consulting | Domain Registration