Can't seem to figure out how to script a simple closed loop
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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.
#!/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...
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!
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:
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
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)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.