LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 05-08-2009, 08:25 AM   #1
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Rep: Reputation: 15
Script help


I need help with two things:

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:

file.20080512.0
file.20080511.1
file.20080510.2
file.20080509.3
file.20080508.4
file.20080507.5
file.20080506.6

Becomes:

file.20080512.tar.gz

I know its a challenge but there are some incredibly smart people on here that i think can help me. Thanks!

Last edited by investmentbnker75; 05-08-2009 at 10:54 AM.
 
Old 05-08-2009, 08:41 AM   #2
Poetics
Senior Member
 
Registered: Jun 2003
Location: California
Distribution: Slackware
Posts: 1,181

Rep: Reputation: 49
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).
 
Old 05-08-2009, 09:49 AM   #3
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
bash. I thought it was possible, anyone have any examples?
 
Old 05-08-2009, 10:31 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by investmentbnker75 View Post
I need help with two things:

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:

Code:
var=backup.20080506.0
suffix=${var##*.}
prefix="${var%.*}"
$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.


Quote:
file.20080512.0
file.20080511.1
file.20080510.2
file.20080509.3
file.20080508.4
file.20080507.5
file.20080506.6

Becomes:

file.20080512.tar.gz

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.

Last edited by i92guboj; 05-08-2009 at 10:33 AM.
 
Old 05-08-2009, 11:01 AM   #5
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
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.
 
Old 05-08-2009, 11:20 AM   #6
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
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.

Untested.
 
Old 05-08-2009, 01:15 PM   #7
investmentbnker75
Member
 
Registered: Oct 2007
Location: Eastern Seaboard
Distribution: CentOS
Posts: 162

Original Poster
Rep: Reputation: 15
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

Thanks again!
 
Old 05-08-2009, 01:41 PM   #8
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,083

Rep: Reputation: 405Reputation: 405Reputation: 405Reputation: 405Reputation: 405
Quote:
Originally Posted by investmentbnker75 View Post
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.

Code:
if [ -n "$foo" ]; then
  do_backup
fi
Then you need the function that will do the job:

Code:
function do_backup() {
  filename=$(stat file.*.1 --print="%n")
  backup_file="${filename%.*}".tar.bz2
  tar cfvpf "$backup_file" file.*.1 file.*.2 file.*.3 file.*.4 filel.*.5 file.*.6 && \
    rm -rf file.*.1 file.*.2 file.*.3 file.*.4 filel.*.5 file.*.6
}
Assuming that you want to use the prefix of the .1 file, if not, change it.

100% untested as well.

Last edited by i92guboj; 05-08-2009 at 01:42 PM.
 
  


Reply



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 execute a ssh script on Linux server from Windows through a bat script? wanna13e Programming 13 10-23-2009 02:41 AM
I want to run script on the server from client machine in windows in a perl script vpradeep Linux - Newbie 2 09-01-2008 03:29 AM
ssh - using variables in call to start remote script from local script babag Linux - Networking 2 06-03-2008 04:50 PM
set variables in a bash script; ansi PS1 color script donnied Programming 4 11-21-2007 11:33 AM
linux 9 and java script error - premature end of script header sibil Linux - Newbie 0 01-06-2004 04:21 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:53 AM.

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
Open Source Consulting | Domain Registration