The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Operating Systems
> UNIX Help
|
Need to delete files after x days via backup bash.
Discuss Need to delete files after x days via backup bash. in the UNIX Help forum on Dev Shed. Need to delete files after x days via backup bash. UNIX Help forum discussing the Unix Operating System and all variants including Irix, Solarix, and AIX. Unix was designed as a true multi-user operating system.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

January 17th, 2011, 10:23 AM
|
|
|
|
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
|

January 17th, 2011, 10:28 AM
|
|
|
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 {} \;
|

January 17th, 2011, 05:46 PM
|
|
|
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
|

January 18th, 2011, 11:06 AM
|
|
|
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. 
|

January 18th, 2011, 04:26 PM
|
|
|
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.
|

January 18th, 2011, 04:51 PM
|
|
|
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. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|