1843974 Members
2170 Online
110226 Solutions
New Discussion

query on tar command

 
SOLVED
Go to solution
ng_7
Regular Advisor

query on tar command

hi, friends

Can please tell me how to extract tar backup by extracting files without extracting leading directories. For example, to recover
/prod/applprod/test.txt to /test/applprod, I want tar to extract only test.txt to /test/applprod instead of the whole directory structure /prod/applprod/test.txt

thank you
16 REPLIES 16
Ninad_1
Honored Contributor

Re: query on tar command

Hi,

To be able to extract to a location different than from where you took the backup usingtar - needs backup to be taken with relative path names rather than absolute path names.
Thus if you have take tar like e.g.
tar cvf xyz.tar ./prod/*
Only then you can goto some other directory and extract it there using tar xvf .

Next - if you want to extract only a particular file - then as er your e.g.
cd /test/applprod
tar xvf xyz.tar /prod/applprod/test.txt

But again as I said earlier only if you have taken backup using relative names, then would you be able to extract it to /test/applprod , not otherwise.

Regards,
Ninad
skt_skt
Honored Contributor

Re: query on tar command

These are the limits when we backup the systems using unix commands. It would be been eaiser if there are external backup software/tools like netbackup or DP(data protector)..
Steven Schweda
Honored Contributor

Re: query on tar command

Dennis Handly
Acclaimed Contributor
Solution

Re: query on tar command

No need to relative paths or foreign devil tools.
You can use the POSIX standard pax(1). After decades I finally set up a script to use pax -s to rename the files.
Ninad_1
Honored Contributor

Re: query on tar command

Hi,

Sorry, probably I did not interpret you question correctly.
As Dennis suggested, you can use pax to extract the file you want and to a directory other than from where you backed it up, as below
pax -r -d -s ',/prod/applprod/,/test/applprod/,' -f xyz.tar /prod/applprod/test.txt

Regards,
Ninad
tsf_1
Frequent Advisor

Re: query on tar command

The ways/options available when recover is very much depends on how you backup.

Example 1 (can restore to other directory; no full path is needed as backup is not taken at full path)
During backup
# cd /home/user1
# tar cvf /dev/rmt/0m color1
During recover
# cd /tmp
# tar xvf /dev/rmt/0m color1

Example 2 (restore required full path as backup was taken in full path)
During backup
# tar cvf /dev/rmt/0m /home/user1/color1
During restore
# tar xvf /dev/rmt/0m /home/user1/color1

Hope this make a clear difference.
be willing to do, be happy to bear
Dennis Handly
Acclaimed Contributor

Re: query on tar command

>tsf: The ways/options available when recover is very much depends on how you backup.

This is true if you just use the standard tar. But as I said, you can tar with absolute path and use pax to restore to any path.

# cd /home/user1
# tar cvf /dev/rmt/0m color1

You can also optimize this by:
# tar cvf /dev/rmt/0m -C /home/user1 color1
(Multiple -C paths are possible.)
ng_7
Regular Advisor

Re: query on tar command

hi, I don't know what is the mistake i made, please advise me on below.

1. I do the backup on *.tmp
on /prod/applprod/prodora/8.0.6
- cd /prod/applprod/prodora/8.0.6
- tar -cvf /u01/123.tar -i *.tmp

2. I verify my backup by :
tamcoapp:/u01# tar -tvf /u01/123.tar
rw-rw-rw- 0/3 0 Jul 30 10:02 2007 alson.tmp
rwxr-xr-x 108/102 2251 Jun 11 11:54 2002 forms60.csh.tmp
rwxr-xr-x 108/102 2796 Jun 11 11:54 2002 forms60.sh.tmp
rwxr-xr-x 108/102 5565 Jun 11 11:54 2002 forms60_server.tmp
rwxr-xr-x 108/102 2371 Jun 11 11:54 2002 reports60.csh.tmp
rwxr-xr-x 108/102 2897 Jun 11 11:54 2002 reports60.sh.tmp
rwxr-xr-x 108/102 3139 Jun 11 11:54 2002 reports60_server.tmp

2. then I restore it in u01/applhprod/hprodora/8.0.6

app:#/u01/applhprod/hprodora/8.0.6/pax -r -d -s ',/prod/applprod/prodora/8.0.6/,/u01/applhprod/hprodora/8.0.6/,' -f /u01/123.tar /prod/applprod/prodora/8.0.6/*.tmp


And I get this error message :

tamcoapp:/u01/applhprod/hprodora/8.0.6# a/8.0.6/alson.tmp not found in archive <
pax: /prod/applprod/prodora/8.0.6/alson.tmp not found in archive
pax: /prod/applprod/prodora/8.0.6/forms60.csh.tmp not found in archive
pax: /prod/applprod/prodora/8.0.6/forms60.sh.tmp not found in archive
pax: /prod/applprod/prodora/8.0.6/forms60_server.tmp not found in archive
pax: /prod/applprod/prodora/8.0.6/reports60.csh.tmp not found in archive
pax: /prod/applprod/prodora/8.0.6/reports60.sh.tmp not found in archive
pax: /prod/applprod/prodora/8.0.6/reports60_server.tmp not found in archive
Steven Schweda
Honored Contributor

Re: query on tar command

The shell expands this wildcard, because it's
not quoted:

/prod/applprod/prodora/8.0.6/*.tmp

Try quoting it, or, better yet, leave it out.
Everything in your archive is a *.tmp. If
you specify no pattern, you should get
everything in the archive, which would seem
to be what you want.
Dennis Handly
Acclaimed Contributor

Re: query on tar command

>1. I do the backup on *.tmp on /prod/applprod/prodora/8.0.6
- cd /prod/applprod/prodora/8.0.6
- tar -cvf /u01/123.tar -i *.tmp

Your backup is relative, no need to use pax(1) to restore. Also, I can't find the -i option under tar(1)??

>2. I verify my backup by:
rw-rw-rw- 0/3 0 Jul 30 10:02 2007 alson.tmp

These files are all relative. Those are the names needed when you want to extract some of the files.

>3. then I restore it in /u01/applhprod/hprodora/8.0.6
app:#/u01/applhprod/hprodora/8.0.6/pax -r -d -s ',/prod/applprod/prodora/8.0.6/,/u01/applhprod/hprodora/8.0.6/,' -f /u01/123.tar /prod/applprod/prodora/8.0.6/*.tmp

No need to use -s here. Also, your extract path needs to be quoted, otherwise you're probably looking at the shell expansion in the original directory. And more important of all, the files in the tarfile are relative and so you should just extract all of them:
$ cd /u01/applhprod/hprodora/8.0.6
$ tar -xf /u01/123.tar
ng_7
Regular Advisor

Re: query on tar command

hi, sorry, please allow me to ask more detail as I am new Unix administrator, do you mean my command should be as below ? if yes, I get > sign at the right most of the line

pax -r -d -s ',/prod/applprod/prodora/8.0.6/,/u01/applhprod/hprodora/8.0.6/,' -f /u01/123.tar '/prod/applprod/prodora/8.0.6/*.tmp'

Peter Nikitka
Honored Contributor

Re: query on tar command

Hi,

watch what Dennis told you:
>>
No need to use -s here. Also, your extract path needs to be quoted,
<<

Your filenames in your tarfile are already whithout a leading path - so nothing is required to handle leafnames.

In the case of full pathnames being in yout tarfiles, the commandline you reported seems right to me. Are you shure you didn't miss balancinf the single quotes? The shell output makes me think so.

mfG Peter
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Dennis Handly
Acclaimed Contributor

Re: query on tar command

>do you mean my command should be as below? if yes, I get ">" sign at the right most of the line

Yes, if all on one line it should work. But as Steven, Peter and I have mentioned, you don't need that. What you need is my tar command or this pax command:
$ pax -r -d -f /u01/123.tar

>Peter: In the case of full pathnames being in yout tarfiles, the commandline you reported seems right to me.

Ah, pax(1) does take wildcard patterns for the list of files to extract. (tar(1) doesn't.) This is in the style of find(1) and -path.

$ pax -r -d -f /u01/123.tar "*.tmp"
ng_7
Regular Advisor

Re: query on tar command

sorry, i think i confuse u all already,
that is not the good example. I am trying on the below, and managed to restore the file to the right location. but another problem arise is , the command doesn't extract the subdirectory

I have files and directory as shown below dev:/hld# ll -r
total 16
drwxrwxrwx 2 root sys 8192 Aug 3 16:32 tem
drwxr-xr-x 2 appluat dba 96 Nov 9 2004 lost+found
drwxrwxrwx 3 applhld users 96 Aug 3 16:21 hldenv
dev:/hld# ll -R
total 16
drwxrwxrwx 3 applhld users 96 Aug 3 16:21 hldenv
drwxr-xr-x 2 appluat dba 96 Nov 9 2004 lost+found
drwxrwxrwx 2 root sys 8192 Aug 3 16:32 tem

./hldenv:
total 48
-rw-r--r-- 1 root sys 2871 Aug 1 12:51 HLD_app-806.env
-rw-r--r-- 1 root sys 9847 Aug 1 18:27 HLD_app.env
drwxrwxrwx 2 root sys 96 Aug 3 16:21 alson

./hldenv/alson:
total 0
-rw-rw-rw- 1 root sys 0 Aug 3 16:21 1.txt
-rw-rw-rw- 1 root sys 0 Aug 3 16:21 2.txt

./lost+found:
total 0

I do a backup all the files / directory in hld directory.
#dev:/hld# tar -cvf /tmp/test.tar .
a ./hldenv/HLD_tamcoapp.env 20 blocks
a ./hldenv/HLD_tamcoapp-806.env 6 blocks
a ./hldenv/alson/1.txt 0 blocks
a ./hldenv/alson/2.txt 0 blocks

and trying to extract to /hld/tmp by using the below command
pax -r -d -s ',/hldenv/./,/tem/,' -f /tmp/test.tar './hldenv/*'

I managed to get the files restore to /hldenv/tem as shown below :
pax -r -d -s ',/hldenv/,/tem/,' -f /tmp/test.tar './hldenv/*'

but it is only extract the file, but how about those subdirectory like /hldenv/alson :
please see below:
dev:/hld/tem# ls
HLD_app-806.env HLD_app.env


please advise. Thanks




Dennis Handly
Acclaimed Contributor

Re: query on tar command

>but how about those subdirectory

I think your problem was you used -d, remove it. (I was wondering why you used it.)

If you have gotten some useful answers, please read the following about assigning points:
http://forums1.itrc.hp.com/service/forums/helptips.do?#33
ng_7
Regular Advisor

Re: query on tar command

Sorry for my delay in assigning points. Thanks to all who helping me expecially who suggest to use 'pax' command. I have tried it, and it works fine.

thank you very much and hope you all will continue to give me guidance on my future threads.


Regards
ng