LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-24-2007, 09:52 AM   #1
guy12345
LQ Newbie
 
Registered: May 2007
Posts: 6

Rep: Reputation: 0
Exclamation 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

Quote:
mysqldump --all-databases -P !!!! -u !!!! -h 127.0.0.1 --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.



Thanks in advance
 
Old 05-25-2007, 03:12 AM   #2
nfisk
Member
 
Registered: Jul 2002
Location: Stockholm, Sweden
Distribution: Kubuntu, Fedora
Posts: 45

Rep: Reputation: 15
Would I be correct in understanding that you want to keep the 7 most recent daily backups? Then maybe you could use logrotate?
 
Old 05-25-2007, 03:22 AM   #3
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Mint, Puppy
Posts: 3,282

Rep: Reputation: 172Reputation: 172
read the man page


blah | gzip -c > file
 
Old 05-25-2007, 03:28 AM   #4
guy12345
LQ Newbie
 
Registered: May 2007
Posts: 6

Original Poster
Rep: Reputation: 0
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!
 
Old 05-25-2007, 03:30 AM   #5
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

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

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

Last edited by billymayday; 05-25-2007 at 03:32 AM.
 
Old 05-25-2007, 03:41 AM   #6
guy12345
LQ Newbie
 
Registered: May 2007
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 05-25-2007, 03:43 AM   #7
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
No, it deletes those over 7 days old.
 
Old 05-25-2007, 03:51 AM   #8
guy12345
LQ Newbie
 
Registered: May 2007
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 05-25-2007, 03:52 AM   #9
guy12345
LQ Newbie
 
Registered: May 2007
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 05-25-2007, 04:19 AM   #10
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Code:
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?

Code:
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)

Last edited by billymayday; 05-25-2007 at 04:30 AM.
 
Old 05-25-2007, 04:36 AM   #11
guy12345
LQ Newbie
 
Registered: May 2007
Posts: 6

Original Poster
Rep: Reputation: 0
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.
 
Old 05-25-2007, 04:40 AM   #12
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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
 
Old 05-25-2007, 04:41 AM   #13
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

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

Last edited by chrism01; 05-25-2007 at 04:42 AM.
 
Old 05-25-2007, 04:46 AM   #14
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
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?)

Last edited by billymayday; 05-25-2007 at 04:48 AM.
 
Old 05-25-2007, 05:09 AM   #15
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022Reputation: 2022
Try this http://www.topology.org/linux/logrotate.html , 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.
 
  


Reply


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
Backup script in bash gauge73 Programming 13 10-17-2005 06:25 AM
Bash backup script failing using tar and --newer option saldkj Programming 3 03-12-2005 12:03 PM
Bash backup script - If multiple files starting with a exist problem demoncheese Programming 2 07-29-2004 10:47 PM
another dumb program written in bash. micxz Programming 0 10-13-2003 07:23 PM
bash shell backup script turnip Programming 0 04-03-2003 06:00 PM


All times are GMT -5. The time now is 08:37 AM.

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