Operating System - HP-UX
1827066 Members
4202 Online
109713 Solutions
New Discussion

How to use TAR to restore files in a different place.

 
Simon Qu
Frequent Advisor

How to use TAR to restore files in a different place.

On hp-ux 11i, I want to use TAR to restore some data, but I do not want to restore to the original place. I remember that TAR restores data only to the original place. How to use TAR to restore files in a different place ?
Thanks.
8 REPLIES 8
Simon Hargrave
Honored Contributor

Re: How to use TAR to restore files in a different place.

It depends how the tar was created. It's good practice to create tars with relative paths, eg

cd /etc ; tar cvf fred.tar .

rather than

tar cvf fred.tar /etc

In the latter case, files will be stored as /etc/hosts, /etc/passwd etc, whereas in the former they will be ./passwd, ./hosts etc, meaning they can be restored anyway.

tar tvf fred.tar will show you whether the tar is relative or absolute. If filenames are prefixed with ./ then it's relative and you're fine.

If it's an absolute tar, you only have 2 options - tar through symbolic links, or create a chroot jail.

In the first case, it depends what you are restoring, and whether you can rename things out of the way. If you can create a symbolic link where the directory would be to where you want it to, you're set. If not then: -

The other option is a chroot jail. Basically you need to create a small environment somewhere, eg under /var/tmp/jail, under which you'll need the tar binary, libc libraries, a shell etc. Then chroot to this, and you can then untar under this.

It's messy, its complicated, but it makes you quickly realise the benefit of relative pathed tars ;)
Geoff Wild
Honored Contributor

Re: How to use TAR to restore files in a different place.

Depends how you created the archive - absolute or relative paths...

cd /
tar cvf /tmp/sometarfile.tar home

then cd /tmp
tar xvf sometarfile.tar

that will dump archive of home all to tmp...

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.
Rodney Hills
Honored Contributor

Re: How to use TAR to restore files in a different place.

If you saved with relative names, then just cd to where you want to restore.

If you used absolute path names (begins with "/"), then either use gnu tar or use the "pax" command.

HTH

-- Rod Hills
There be dragons...
Sanjay_6
Honored Contributor

Re: How to use TAR to restore files in a different place.

Hi Simon,

you can use pax to do that,

http://www2.itrc.hp.com/service/cki/docDisplay.do?docLocale=en_US&docId=200000062772639

The itrc doc id is UBACKKBRC00008718

Hope this helps.

Regds
Steven E. Protter
Exalted Contributor

Re: How to use TAR to restore files in a different place.

the tar -C command should redirect output to another location.

-C directory name causes tar to chdir and unarchive relative to that location.

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
RAC_1
Honored Contributor

Re: How to use TAR to restore files in a different place.

If you used absolute path names in tar backups, you can restore to original path.

BUT, you have following options.

1. Use GNU tar. Get it from http://hpux.connect.org.uk. You need to know block size
of hp's tar.

2. Use pax as follows.
pax -rv -s '/^\///' < tar_file

3. Use of chroot. This did not work for me. Try it.

/usr/bin/cp /usr/sbin/static/tar /tmp
/usr/bin/dd if=file.tar | /usr/bin/chroot /tmp ./tar xf -
There is no substitute to HARDWORK
Simon Hargrave
Honored Contributor

Re: How to use TAR to restore files in a different place.

Actually I'll be a little clearer on the chroot jail, it's not as bad as it sounds really: -

Lets say you have a tar which contains a set of database files, eg /u01/oradata/db/system.dbf etc etc

To restore this to /u01/oradata, you would do: -

mkdir /u02/jail
cd /u02/jail
cp /sbin/tar /sbin/ls /sbin/sh . (you don't need libraries as these are statically linked)
cp /your/tar/file . (if you want to restore from tape, copy the device eg cp -r /dev/rmt/0mn .)
chroot /u02/jail sh (you are now in the jail)
./ls -l (./ needed as no path defined)
./tar cvf tarfile_or_devicename

This will then untar under the chroot, which will put the files under /u02/jail/u01/oradata etc. You can then exit the shell and move the files as required.
Rodney Hills
Honored Contributor

Re: How to use TAR to restore files in a different place.

Steven,

I think the "-C" option is for only backing up, not restoring.

HTH

-- Rod Hills
There be dragons...