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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
1st:
I need a command to add to my script that will find the directory that has been backed up the day before by date and change it so it will read as below before running the next backup.
file.20080506.0
needs to be moved and read as:
file.20080506.1
That way a new backup will be called:
file.20080507.0
And an ls will show:
file.20080507.0
file.20080506.1
2nd:
I also need to add to my script something that will gzip all the backed up directories when the .6 is reached into one gzip file with the date of the .0 in the name, like below:
Much of this same functionality is present within the 'logrotate' script (auto-renumbering of old files, with the added potential of gzipping the output), if you would like to look at the man page for some suggestions.
Secondly, what language is your script written in? There are many ways to accomplish the above, be it in perl, bash, or some other language. Simple pattern matching (read up on 'regular expressions') can return a filename to the rest of your script (and testing for age is a trivial task).
1st:
I need a command to add to my script that will find the directory that has been backed up the day before by date and change it so it will read as below before running the next backup.
The find command can search for files by date.
Quote:
backup.20080506.0
needs to be moved and read as:
file.20080506.1
You first need to separate the suffix. Bash provides some mangling capabilities, for example, to separate the part of the string from the beginning to the last dot you can use this:
$suffix will hold the value '0', you then just add one to this number. $prefix will contain the rest of the name. You can integrate this into a for loop to rename all the files on a given directory in this fashion:
Code:
for file in *
do
#calculate prefix and sufix
#add 1 to suffix
#rename the current file
done
Quote:
2nd:
I also need to add to my script something that will gzip all the backed up directories when the .6 is reached into one gzip file with the date of the .0 in the name, like below:
This check could be as sophisticated as you wish, however a simple way is to "start" for *.6, if there's any file that complies that condition then do <something>.
Code:
stat *.7 >/dev/null 2>&1 && echo true
Of course here instead of "echo true" you would call a custom function or something else that will compress the backup for you.
I know its a challenge but there are some incredibly smart people on here that i think can help me. Thanks!
If the date is going to be part of the file name, then you can use the tricks I told you above to cut it into pieces and extract the date into a variable, then use it to compose the name of the final tarball.
I hope these hints are useful. Ask back if you need more info on any of these, I guess it's a bit daunting at first. I can help you but I don't want to give the script straight away because that won't help you with learning bash. I also recommend reading the bash man page (yeah, I know it's boring) and googling for the "advanced bash scripting guide". Regardless of the title it's suitable for beginners as well.
Actually, it helps me more to see how the script looks because i seem to learn best when i see the results of the question and reverse engineer it to see how it all works. Honestly, im kind of lost with your explanation but seeing how it would script out would help understand it more.
Just integrate all the pieces, for example, for #1:
Code:
for file in *
do
suffix="${file##*.}"
prefix="${file%.*}"
let suffix=suffix+1
mv "$file" "${preffix}.${suffix}"
done
That will read all the files on the current directory, separate the name files into two parts, increase by 1 the number and the rejoin them and rename the file.
It works good in a way, one problem though, it increments EVERY thing by one, scripts, txt files, etc.... Anyway to do it just for directories or by directories beginning with the name file?
Other than that, looks great!
Also, were you able to figure out part 2 to my original question, how to tar everything up into one big tar file with the date of the .6 file when the 7th backup is due to run? (please see my original post for the actual details
It works good in a way, one problem though, it increments EVERY thing by one, scripts, txt files, etc.... Anyway to do it just for directories or by directories beginning with the name file?
Just use anything else than '*'. If you can match by file name, then this will work:
Code:
for file in file.*
or even
Code:
for file in file.*.?
If you need some more fine-grained control then you are going to have to read the find man page. For example, to find all the directories beginning by "file." you'd do this:
Code:
find . -type d -name 'file.*' | while read file
do
suffix="${file##*.}"
prefix="${file%.*}"
let suffix=suffix+1
mv "$file" "${preffix}.${suffix}"
done
If you need anything else, again, check the find man page. it's quite complete.
About #2, you can capture the name of the .6 file, then if it's not an empty string (which means the file exist), tar all the relevant files. The first thing can be done with this:
Code:
foo=$(stat file.*.6 --print="%n")
I assume you want to check for the .6 file, if that's not the case change the number by whatever you want.
The second part is calling a function if that string is not empty.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.