This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Linux / UNIX and the fly likes How to attach timestamp to backup files in unix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "How to attach timestamp to backup files in unix" Watch "How to attach timestamp to backup files in unix" New topic
Author

How to attach timestamp to backup files in unix

jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
I am creating a batch file to take the backup.
Whenever batch file is run, it will copy the existing file in a backup folder, eg with a timestamp on the newly created file.

cp /iw-home/local/config/templating.cfg /iw-home/local/config/backup/timestamp_templating.cfg

with date command, I can output date as I like
e.g. date '+%d_%m_%y_%H_%M'
this will output current time.

can I embed this in place of timestamp ?
cp /iw-home/local/config/templating.cfg /iw-home/local/config/backup/date '+%d_%m_%y_%H_%M'_templating.cfg

I guess its an error. How can I get the timestamp on the file ?

Can I place this batch file anywhere ? Or is there a particular location where this batch file need to be placed in order to run it ?

thanks
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

It is often a good idea to break the script into smaller, more readable, chunks. It can then become easier to see what each part is doing, and it can also be easier to modify and enhance the script later. As an example, it is easy enough to fix the one-liner you have listed below, but breaking it out into multiple lines might make it easier to add to the script later to make it more generic.

First the one-liner fix:


Placing the code for getting the current timestamp inside the $() tells the shell to execute the commands within the brackets and then use the result inside the larger command.

If I were working on this, I would probably start by putting the current date and time into a field all of it's own:

You could then use this directly in the line you have:

However like I said, I would prefer to have a far more generic function:

Now it can be used for just about any config file you need to backup!

Personal preference - I find it a little hard to read the date-time string as 23_05_09_11_40. I would probably have it as 2009-05-23_1140. But this is purely a personal preference - you should do whatever makes the most sense to you when trying to read the file.

jignesh soni wrote:Can I place this batch file anywhere ? Or is there a particular location where this batch file need to be placed in order to run it ?

You can place this anywhere at all.

There are conventions for where most system administrators would go to look for it.

If this is being run from root's cron job, I would probably put it in /usr/local/sbin (or more accurately, I would probably put it in /usr/local/interwoven-scripts with a symbolic link to it in /usr/local/bin - that way I can put README files etc., with the real script, which the system administrator can find easily enough if they need to).

If this is being run from my own personal cron job, I might put it in ~/bin

If this were being run on-demand from within the TeamSite application I might put it in /usr/local/interwoven, possibly with a symbolic link in /usr/local/bin. That should keep it separate from the Interwoven application itself (so if you do an uninstall or an upgrade you don't have to worry about possibly loosing your personalized scripts), while still keeping it in a common area that all developers can get access to.


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
jignesh soni
Ranch Hand

Joined: Dec 10, 2007
Posts: 150
thanks, that is really helpful.

I have posted another post of the same nature. Please excuse the duplication.

thanks
Andrew Monkhouse
author and jackaroo
Marshal Commander

Joined: Mar 28, 2003
Posts: 11278
    
  59

Since you have split this into multiple topics, I have taken it a step further. You now have 2 additional topics:
  • Copying file using bat file only when there are changes to the file
  • Executing shell script


  • While the first of these might have equally stayed in this topic, since you had a new topic, I went the logical route (at least in my mind) of splitting it totally.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: How to attach timestamp to backup files in unix
     
    Similar Threads
    Batch file to exit if server is not started
    need to fix" noClssDefFoundError"
    Copying file using bat file only when there are changes to the file.
    org.springframework.batch.core.repository.JobExecutionAlreadyRunningException
    Executing shell script