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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
07-19-2011, 03:42 PM
|
#1
|
|
Member
Registered: Dec 2003
Posts: 78
Rep:
|
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
|
|
|
|
07-19-2011, 05:05 PM
|
#2
|
|
Member
Registered: Jan 2009
Distribution: Debian
Posts: 59
Rep:
|
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.
|
07-20-2011, 03:09 AM
|
#4
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,321
|
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...
|
|
|
|
07-20-2011, 11:39 AM
|
#5
|
|
Member
Registered: Dec 2003
Posts: 78
Original Poster
Rep:
|
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.
|
07-20-2011, 12:05 PM
|
#6
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,321
|
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?
|
|
|
|
07-20-2011, 05:59 PM
|
#7
|
|
Member
Registered: Dec 2003
Posts: 78
Original Poster
Rep:
|
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
|
|
|
|
07-20-2011, 08:26 PM
|
#8
|
|
Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 6,321
|
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)
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 11:14 PM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|