UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old January 17th, 2011, 10:23 AM
joepwnage joepwnage is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 209 joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 19 h 19 m 45 sec
Reputation Power: 82
Need to delete files after x days via backup bash.

I'm very new to bash, and i have a basic script that is running on my server via cron job. It runs a backup every 15min of mysql db's.

However, as you can tell - this gets rather heavy. So i just need to add some code for it to DELETE anything in that folder that exceeds CURRENT DATE -60 days. So anything that is older than 60 days will be deleted.

Is that too heavy to be in the same script as a backup?
I've tried googling for some code snippets, but no avail. is this something simple?

here is what i currently have, i just need to add that "delete older than 60 days snippet"

Code:
#!/bin/bash

MYSQL_USERNAME='1337hov'
MYSQL_PASSWORD='password'
MYSQL_DATABASE='databse'
BACKUP_DIR='/home/trsmedia/mysql-backup'

if [ -z $MYSQL_USERNAME ]
then
	echo "[-] MySQL username and password required for automatic backups."
else
	/usr/bin/mysqldump -u$MYSQL_USERNAME -p$MYSQL_PASSWORD $MYSQL_DATABASE | gzip > $BACKUP_DIR/`date +%d%m%Y_%H%M`.sql.gz
fi

Reply With Quote
  #2  
Old January 17th, 2011, 10:28 AM
joepwnage joepwnage is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 209 joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 19 h 19 m 45 sec
Reputation Power: 82
The closest thing that i've found it this, but i don't know how this will find the proper files? based on created date? or filename containing the date?

Code:
find /path/to/files* -mtime +30 -exec rm {} \;

Reply With Quote
  #3  
Old January 17th, 2011, 05:46 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
That's the sort of thing you'll need - but I do suggest changing the rm {} \; to ls -ld {} \; until you are happy that it will only pick on the files you want.

Your backups follow a naming standard (albeit with the 'wildcard' of the date) in a fixed directory, so:

Code:
find /home/trsmedia/mysql-backup -type f -name \*.sql.gz -mtime +59 -exec ls -ld {} \;

should list all backups that are over 59 days old.
__________________
The moon on the one hand, the dawn on the other:
The moon is my sister, the dawn is my brother.
The moon on my left and the dawn on my right.
My brother, good morning: my sister, good night.
-- Hilaire Belloc

Reply With Quote
  #4  
Old January 18th, 2011, 11:06 AM
joepwnage joepwnage is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 209 joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 19 h 19 m 45 sec
Reputation Power: 82
Gotcha, that looks great. How is it going to list if i am just running it as a cron job? in some sort of command/terminal?

Sorry again, very new to this.

Reply With Quote
  #5  
Old January 18th, 2011, 04:26 PM
SimonJM SimonJM is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Mar 2006
Posts: 2,108 SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level)SimonJM User rank is General 8th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 1 Week 1 Day 4 h 48 m 25 sec
Reputation Power: 1485
Run it by hand first: ensure that there are no bugs, and that what you get is what you want. Once you have done that swap the ls -ld for your rm and, once again, run it by hand. Then make sure you have a valid backup in case it all drops in the pot ...
Once you are happy slap it in cron.

If you wanted the list via cron then either redirect the output to a file or just allow cron to use it's default of mailing the running user with any output from the command run.

Reply With Quote
  #6  
Old January 18th, 2011, 04:51 PM
joepwnage joepwnage is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 209 joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level)joepwnage User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 19 h 19 m 45 sec
Reputation Power: 82
Ah ok understood, Simon you've been a fantastic help. i'll do this the second i get home. I appreciate your time my man.

gotta DL more ebooks.

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Need to delete files after x days via backup bash.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap