LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
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-19-2011, 04:42 PM   #1
blainemiller
Member
 
Registered: Dec 2003
Posts: 78

Rep: Reputation: 1
Can't seem to figure out how to script a simple closed loop


Hello,

I'm stuck. I need to run a simple commandline command for subversion(svnadmin dump...) for 15 days. Then start again and overwrite each of the old .dmp files with new ones.

I think I can do it by brute force with a crontab entry for each of the 15 days at a time, but I'm not even sure I know how to do that.

I'm trying to automate a process I do manually every day. That is, I execute the same command every day for 15 days running, manually changing the filename of the file by 1 each time, i.e. file.1.dmp, file.2.dmp, and so on.

Then after I reach the end of 15 days, I start again changing the filename being written back to the name of the first file I created 15 days ago. This overwrites the 15 files one a day for 15 days. That is, after I create the file.15.dmp, I (manually) change the name of the file being created back to file.1.dmp again.

I realize that this may not be descriptive enough to help me with getting an answer. I don't know how else to explain it. I'd be more than glad to fill in any details I've missed.

I've thought about using a for loop, but it's been so long since I've had to do any programming I hesitate to go down that path. I've though about using the current date variable to change the filename, but how do I reset the script to start again after 15 days?

The point of my exercise is to have a rolling backup/dump file for 15 days that I can restore from if needed.

Your time and consideration is much appreciated.

Thanks!

Blaine Miller
 
Old 07-19-2011, 06:05 PM   #2
Nermal
Member
 
Registered: Jan 2009
Distribution: Debian
Posts: 59
Blog Entries: 2

Rep: Reputation: 6
OK, hows this idea:
Code:
#!/bin/bash
if [ -f file.15.dmp ]
then
  rm file.15.dmp
fi
for i in 14 13 12 11 10 9 8 7 6 5 4 3 2 1
do
  if [ -f file.$i.dmp ]
  then
    mv file.$i.dump file.`expr $i + 1`.dmp
  fi
done
dump command here....
This will move all the old ones up a number and delete the 15th first. This means the the newest is always 1 and the oldest is always 15...
 
1 members found this post helpful.
Old 07-19-2011, 06:51 PM   #3
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.6, Centos 5.10
Posts: 16,324

Rep: Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041Reputation: 2041
Nermal's soln looks about right to me.

Here's some good links on bash
http://rute.2038bug.com/index.html.gz
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/

A few notes

1. I'd always use [[ ]] rather than [ ] http://tldp.org/LDP/abs/html/testcon...ml#DBLBRACKETS
2. I'd also use bash arithmetic built-in $(( )) http://tldp.org/LDP/Bash-Beginners-G...ml#table_03_04 section 3.4.6 instead of calling expr (just an efficiency thing; no big deal)
3. don't forget to provide the entire path for any files, unless you first cd into the correct dir before you start.

If you're working on Linux (Unix generally) you're going to need a certain proficiency with the shell of your choice, hence the links above.
 
Old 07-20-2011, 04:09 AM   #4
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,693

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
I would add that you don't even really need the 15 test as if 14 exists it will overwrite 15 anyway.
So another revision could look like:
Code:
#!/bin/bash

for i in {14..1}
do
    [[ -f file.$i.dmp ]] && mv file.$i.dmp file.$((i+1)).dmp
done

dump command here...
 
Old 07-20-2011, 12:39 PM   #5
blainemiller
Member
 
Registered: Dec 2003
Posts: 78

Original Poster
Rep: Reputation: 1
OK...

First thanks very much for all your kind assistance. Not only am I a baby "script kitty", but so far removed from having to write scripts at this level as to be ridiculous. Apologies for my ignorance. I really am trying to remedy that as quickly as I can.

Next, I've modified the proposed script to mimic what my actual filenames are.

Code...:

#!/bin/bash
if [ -f main15.svn_dmp ]
then
rm main15.svn_dmp
fi
for i in 14 13 12 11 10 9 8 7 6 5 4 3 2 1
do
if [ -f main`$i`.svn_dmp ]
then
mv main`$i`.svn_dmp file.`expr $i + 1`.svn_dmp
fi
done
svnadmin dump /var/www/svn/main/ > /svndump/main'$i'.svn_dump


EndCode...:


This script will be called by another script that puts one in the appropriate directory for the created files to reside. A simple cd /svndump/...

This, what I call dump script, is in turn called by cron.

Third, I don't know if I made it too clear, but I'm executing the svnadmin dump command once a day every day for 15 days. I change the numeric characters by hand every day when I execute the script. Then after I have completed the 15th day of svnadmin dump, I simply start executing the command again starting with the /svndump/main1.svn_dump filename.

Will this proposed script step through those 15 days like I now do manually? I guess there should really be a day changing mechanism to wrap this proposed script? Or are we talking about a whole different script than what is proposed?

I know this sounds a little screwy. The best way for me to describe what the script should do is to describe what I'm already doing manually. If I'm not to clear on that, please tell me how I can respond to make it clearer.

Thanks again... I've looked through all my coding books and can't seem to find anything that approximates what I want to do. I'm hoping you will have the time to help me with my problem.

Thanks in advance for your consideration.

Blaine

PS.... Sorry to have to spell out the body of the script rather than embed it as code... I'm going to have to figure out how to do that in this forum too!
 
1 members found this post helpful.
Old 07-20-2011, 01:05 PM   #6
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,693

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
Well last issue first, you simply use [code][/code] tags around your code and it will appear like the other entries.

As for calling from another script in which you only do a cd, you can either add that to this script or just place the path within the script.

Yes all scripts demonstrated understand that you will be doing this everyday and on the 15th day the process should go back to day 1 file and overwrite.

Errors I can see is you have placed your variables in what look like back ticks. At best they could be double quotes but even they are not required.

The other main error is that your final line will always output as follows:
Code:
svnadmin dump /var/www/svn/main/ > /svndump/main1.svn_dump
ie it will always be number 1. This means the order is that the number 1 file will always be the most recent.

Let me know if I did not answer all your questions?
 
Old 07-20-2011, 06:59 PM   #7
blainemiller
Member
 
Registered: Dec 2003
Posts: 78

Original Poster
Rep: Reputation: 1
OK....

I need to think about this and try out the script as it's written to see if I can get it to work... I'll update this post tomorrow with tonight's results.

The script I'm going to try tonight reads as follows:

Code:
#!/bin/bash
if [ -f main15.svn_dmp ]
then
rm main15.svn_dmp
fi
for i in 14 13 12 11 10 9 8 7 6 5 4 3 2 1
do
if [ -f main$i.svn_dmp ]
then
mv main$i.svn_dmp file.`expr $i + 1`.svn_dmp
fi
done
svnadmin dump /var/www/svn/main/ > /svndump/main$i.svn_dump
Thanks again for all your help.

Blaine
 
Old 07-20-2011, 09:26 PM   #8
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,693

Rep: Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987Reputation: 1987
You did not read my last post correctly. Your last line is still incorrect. You do not want to use $i but
rather a '1' (See my code snippet from previous post)
 
  


Reply

Tags
scripting


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
how to loop over text file lines within bash script for loop? johnpaulodonnell Linux - Newbie 8 04-05-2011 10:18 AM
Trouble With FOR Loop In Simple Bash Script sudleyplace Linux - Newbie 7 03-12-2008 05:55 AM
closed loop security michaewlewis Linux - Networking 2 12-29-2006 02:14 PM
Simple script with here document. Can't figure out what's wrong. frankie_DJ Programming 3 10-03-2006 12:50 AM
Problem with simple shell script for loop abefroman Programming 2 10-25-2005 09:26 PM


All times are GMT -5. The time now is 12:11 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