Operating System - HP-UX
1824808 Members
4185 Online
109674 Solutions
New Discussion юеВ

Create a Tar with date stamp

 
SOLVED
Go to solution
Alvin_15
Occasional Contributor

Create a Tar with date stamp

I am trying to two things actually.
I want to be a able to tar and then gz a file but I want to add a date stamp to the end of my file.
Exmp: fred.tar.gz.*Date* (day,month,year)

Thanks for the help!!
11 REPLIES 11
Steven E. Protter
Exalted Contributor

Re: Create a Tar with date stamp

Download and install this:

http://www.hpux.ws/merijn/caljd.sh

It provides utility for getting dates into variables quite easily. Also calculations.

Install it and call it directly in your script that creats the tar

Then

filename="filename${day}${month}${year}.tar"

tar cvf $filename *
gzip $filename

Done.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
A. Clay Stephenson
Acclaimed Contributor
Solution

Re: Create a Tar with date stamp

I would suggest that you add the date BEFORE the .gz suffix so that gunzip sees the correct suffix.

FNAME=fred.tar
tar cvf ${FNAME} . # create the tar archive
DT=$(date '+%d%m%Y')
NEW_FNAME="${$NAME}.${DT}"
gzip ${NEW_FNAME}


as a Plan B you could leverage the -S option of the gzip comand but agin I would put the date before the .gz

DT=$(date '+%d%m%Y')
SUF=".${DT}.gz"
gzip -S "${SUF}" fred.tar
If it ain't broke, I can fix that.
Rick Garland
Honored Contributor

Re: Create a Tar with date stamp

To make the tarball;
tar -cvf *
(assuming you are in correct directory)

gzip the tarball;
gzip

move the file to new name with date stamp;
mv .gz .gz.`date +%d%m%Y`

Can be done in fewer steps but this is the basic

Geoff Wild
Honored Contributor

Re: Create a Tar with date stamp

mv fred.tar.gz fred.tar.gz.`date +%d%m%Y`


Rgds...Geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Alvin_15
Occasional Contributor

Re: Create a Tar with date stamp

I appreciate all the help on this.
I did make one change. The end result that I am looking for is as follows:
05pIBM.date.tar

then I will do a gzip to make it:
05pIBM.date.tar.gz

Here is my current script which is not working. Thanks again for your feedback.

#!/bin/sh

$date=$(date '+%d%m%Y')

# create tarfile for /opt/IBM/05ptar

tar -cvf /tmp/05pIBM.$date.tar /opt/IBM

# end script

My file ends up looking like this:
05pIBM..tar
Geoff Wild
Honored Contributor

Re: Create a Tar with date stamp

Uh...remove the $ from date:

date=$(date '+%d%m%Y')

Rgds...geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
A. Clay Stephenson
Acclaimed Contributor

Re: Create a Tar with date stamp

This ain't Perl:

your:
$date=$(date '+%d%m%Y')
should be:
DT=$(date '+%d%m%Y') # no dollar sign and it ain't too smart to use a variable name (date) that could easily be confused with a common command.

Also, it's wise to always enclose shell variables in {}'s. Most of the time they are not required to avoid parsing errors but sometimes they are. I hate code that branches (even inside my head) so I always use them.

If it ain't broke, I can fix that.
Alvin_15
Occasional Contributor

Re: Create a Tar with date stamp

Thank You all for your help on this !!
As you can see I am a major novice at this whole scripting thing.

My next piece to this puzzle is:
I am doing a scp of this file to another server and placing it in /adv/adm/backups.

I need to then create another script to remove the third archive in the /adv/adm/backups folder.
05pIBM.25102005.tar
05pIBM.26102005.tar
05pIBM.27102005.tar

I have tested my scp and that works great. Just not sure how to remove that first one and only leave two in the folder each day.
Thanks!!
Geoff Wild
Honored Contributor

Re: Create a Tar with date stamp

2 ways - you could write some complicated code with perl to figure out the file to be removed....or easy way - just do a find command on the remote system:


#!/bin/sh
# Script to remove old backup jobs
find /adv/adm/backups -name '05pIBM.*' -mtime +3 -exec rm {} \; >/tmp/rmoldbakup.log


Run from cron each day...

Rgds...geoff
Proverbs 3:5,6 Trust in the Lord with all your heart and lean not on your own understanding; in all your ways acknowledge him, and he will make all your paths straight.
Alvin_15
Occasional Contributor

Re: Create a Tar with date stamp

Thanks Once again !!!

You have really helped me out a ton..

Have great day !!
Alvin_15
Occasional Contributor

Re: Create a Tar with date stamp

Thanks to the help I received. I able to close this string.

Thanks to all...