-   Programming (
-   -   Need help with my backup script written in bash! (

guy12345 05-24-2007 09:52 AM

Need help with my backup script written in bash!
I would like some help with my bash script.

I have several one .gz file being backed up daily but I would like to first check that all the files are there from over the week which would be 7 if there are 7 there I would then like it to sort them by the time stamp on the file then remove the first one created at the start of the week. and tell my backup line of script


mysqldump --all-databases -P !!!! -u !!!! -h --password='!!!!!!!' | gzip >mysqlbackup${DATE}.gz
to begin the backup only if all the files are there and the first one has been deleted.

Can anyone help me with this I am very new to this so I will need a lot of explaining. :rolleyes:

Thanks in advance

nfisk 05-25-2007 03:12 AM

Would I be correct in understanding that you want to keep the 7 most recent daily backups? Then maybe you could use logrotate?

bigearsbilly 05-25-2007 03:22 AM

read the man page

blah | gzip -c > file

guy12345 05-25-2007 03:28 AM

I would like to keep 7 then just before the 8th backup which would be on a Monday I would like it to make sure there are 7 files backed up then delete the first backup created and then write the new backup.

I hope this makes sense.

Thanks for your replies!

billymayday 05-25-2007 03:30 AM

Why can't you just delete backup files > 7 days old using find?

find /destination/ -name '*' -type f -mtime +7 -exec rm -f {} \;

guy12345 05-25-2007 03:41 AM

I need to make sure all 7 files are there before I do anything though. I could not see in your line of text a command to delete the first file created from what I understand it deletes all 7. I do have time stamps in the file name of each file created. Thanks for your help.

billymayday 05-25-2007 03:43 AM

No, it deletes those over 7 days old.

guy12345 05-25-2007 03:51 AM

But how can I make sure the folder has all 7 files in there before i begin a new backup? Also would it be possible to translate that line in human readable. I apologise for being a pain.

guy12345 05-25-2007 03:52 AM

Ok thanks but how can I get it to check there are 7 files in there before I delete the first one? Also could you translate your line into human readable text? Sorry to be such a pain.

billymayday 05-25-2007 04:19 AM


find /destination/ -name '*' -type f -mtime +7 -exec rm -f {} \;
Basically, find is looking in /destination/ for files (f) called anything (-name '*') modified at least 7 days ago (-mtime +7) then executing the rm -f command on those file found. Does that make sense?

Maybe this would be better?


rm -f `ls -t | sed -e 1,7d`
basically, this lists the files in order of time, ignores entries 1-7 then passes them to rm -f (ie deletes them)

Edit - note that the quote mark I have used is a "`" not a "'" (look under the tilde "~" on US style keyboards)

guy12345 05-25-2007 04:36 AM

Ok that makes much more sense but how can I only delete the first file created and leave the other 6. Then the next day tuesday will be deleted as a new backup will need to be created. So it once Monday comes Mondays backup willbe deleted on Monday and a new backup for Monday will then be created.

billymayday 05-25-2007 04:40 AM

The first code deletes file over 7 days old, the second leaves the youngest 7 files and deletes the oldest (if you had 8 files, the first of those 8 created would be deleted). Is that what you want, or do you want to say today in Monday, delete last Monday's backup if it doesn't fit into either of the above?

Create a few files with "touch" and try it

chrism01 05-25-2007 04:41 AM

man logrotate
it does all that for you and more reliably than hand-hacking a script

billymayday 05-25-2007 04:46 AM

Does it? That's handy to know

Edit - ps - I still like my scripts

But can he run logrotate independently of his backup script (ie, they run separately don't they?)

chrism01 05-25-2007 05:09 AM

Try this , a bit old, but written to explain.
Also man logrotate.
As you can see, you can use cmds embedded, or set logrotate to a time that suits you and run your backup separately in cron.

All times are GMT -5. The time now is 03:32 AM.