LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Can't seem to figure out how to script a simple closed loop (http://www.linuxquestions.org/questions/linux-newbie-8/cant-seem-to-figure-out-how-to-script-a-simple-closed-loop-892578/)

blainemiller 07-19-2011 03:42 PM

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

Nermal 07-19-2011 05:05 PM

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...

chrism01 07-19-2011 05:51 PM

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.

grail 07-20-2011 03:09 AM

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...


blainemiller 07-20-2011 11:39 AM

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!

grail 07-20-2011 12:05 PM

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?

blainemiller 07-20-2011 05:59 PM

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

grail 07-20-2011 08:26 PM

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)


All times are GMT -5. The time now is 12:16 PM.