WelcomeWelcome | FAQFAQ | DownloadsDownloads | WikiWiki

Author Topic: How to maintain directory ownership when using cpio -pud /tmp/package < list ?  (Read 22089 times)

Offline bigpcman

  • Hero Member
  • *****
  • Posts: 719
Some preserve upstream directory perms, and others preserve existing target directory perms.  In extensions, we decided to go with preserving upstream extension directory perms to give the extension maker control over created directories.  

Since I am essentially making an extension it would seem rsync would be the best tool to use.

Jason your tar command is quite a master piece.

"tar -C / -T /mnt/sda1/myfilelist.lst -cpf - | (cd /tmp/package ; sudo tar xf -)"

Could you explain how it works.

I get the simple stuff but I don't get the   "- | (...)" and the "tar xf -)" parts.

Sorry to keep bothering you.
« Last Edit: January 27, 2010, 05:02:41 PM by bigpcman »
big pc man

Offline Jason W

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 9730
We used a tar pipe during a release candidate, and from what I understand:

From the / directory, tar up the stuff in the list file, and pipe it to tar to extract it in the /tmp/package directory.

Offline vitex

  • Full Member
  • ***
  • Posts: 113

I get the simple stuff but I don't get the   "- | (...)" and the "tar xf -)" parts.


The "-f" option is used to tell tar where to write or read a tar file; "-" means standard output when creating a tar file and standard input when extracting a tar file.  The default is to create tar files on standard output and read tar files from standard input, so the "-f -" options not needed.

tar -c ... | (cd /tmp/package ; tar -x ...)

creates a tar file on standard output where it is piped into subshell that executes a tar extraction process within the directory /tmp/package.  I believe that that is equivalent to

tar -c ... | tar -x -C /tmp/package ...

You can also do things like

tar -c ... | ssh tc@somewhere.else tar -x -C /tmp/package

transfer files from one machine to the directory /tmp/package on a remote machine.  (Rsync can also transfer files over an ssh link.)

Offline bigpcman

  • Hero Member
  • *****
  • Posts: 719
As it turns out, tar does not maintain directory permissions as I thought it did. I did some more testing as follows:
Code: [Select]
tc@box:/tmp$ sudo rm -R package/                                               
tc@box:/tmp$ mkdir package                                                     
tc@box:/tmp$ sudo tar -C / -T /mnt/sda1/myfilelist.lst -czf /mnt/sda1/restoremys
tc@box:/tmp$ sudo tar xvzf /mnt/sda1/restoremysys.tar.gz -C /tmp/package       
etc/sudoers                                                                     
etc/shadow                                                                     
home/tc/.wbar                                                                   
home/tc/.profile                                                               
tc@box:/tmp$ cd package/home/                                                   
tc@box:/tmp/package/home$ ls -l                                                 
drwxr-xr-x    2 root     root           80 Jan 27 17:45 tc/

The /home/tc directory ownership was changed from tc to root.
                                                                         
So it looks like if directorys don't exist the new ones that are created use the current user name just like cpio does.

However, "rsync" appears to get it right:

Code: [Select]
tc@box:/tmp$ sudo rsync -av --files-from=/mnt/sda1/myfilelist.lst  /  /tmp/package                           
building file list ... done                                                                                 
created directory /tmp/package                                                                               
etc/                                                                                                         
etc/shadow                                                                                                   
etc/sudoers                                                                                                 
home/                                                                                                       
home/tc/                                                                                                     
home/tc/.profile                                                                                             
home/tc/.wbar                                                                                               
                                                                                                             
sent 1373 bytes  received 97 bytes  2940.00 bytes/sec                                                       
total size is 1036  speedup is 0.70                                                                         
tc@box:/tmp$ cd package/                                                                                     
tc@box:/tmp/package$ ls -l                                                                                   
drwxr-xr-x    2 root     root           80 Jan 27 17:38 etc/                                                 
drwxr-xr-x    3 root     root           60 Jan 27 17:28 home/                                               
tc@box:/tmp/package$ cd home/                                                                               
tc@box:/tmp/package/home$ ls -l                                                                             
drwxr-sr-x    2 tc       staff          80 Jan 27 17:39 tc/

This has all been quite a learning experience. Thank you Vitex and Jason for providing all the information.
big pc man

Offline ^thehatsrule^

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 1726
That's because the directories weren't included whereas the rsync one did.

This is probably due to
Code: [Select]
-not -type d

Offline bigpcman

  • Hero Member
  • *****
  • Posts: 719
That's because the directories weren't included whereas the rsync one did.

This is probably due to
Code: [Select]
-not -type d

Could you elaborate on this. Is the above code to be added to the cpio or tar command?
big pc man

Offline Jason W

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 9730
Whem creating the list file, using "find -not -type d" is what excludes the directories.  If you want to preserve upstream directory perms, then do not use "-not -type d" with find and tar will preserve upstream directory perms when working from the list. 

Offline bigpcman

  • Hero Member
  • *****
  • Posts: 719
Whem creating the list file, using "find -not -type d" is what excludes the directories.  If you want to preserve upstream directory perms, then do not use "-not -type d" with find and tar will preserve upstream directory perms when working from the list.  

Thanks for the tip.
I'm not using find to create my file list.

I'm using the tc application "/usr/bin/add2file myfilelist.lst"
This works great for adding files to a list that are scattered around the system.

I assumed this is what is used by the tc system for adding files to the "backup" list which are then "tar'ed"

Here's my file list:
tc@box:/mnt/sda1$ cat myfilelist.lst
home/tc/.profile
home/tc/.wbar
etc/shadow
etc/sudoers

What would have to be different?
« Last Edit: January 28, 2010, 09:54:30 AM by bigpcman »
big pc man

Offline ^thehatsrule^

  • Retired Admins
  • Hero Member
  • *****
  • Posts: 1726
Add any directories that you want to myfilelist.lst

Offline bigpcman

  • Hero Member
  • *****
  • Posts: 719
Add any directories that you want to myfilelist.lst

Vitex brought this up as well.

So if I added:

home
home/tc
etc

to the myfilelist.lst then the directories are created with the correct ownership. Is that what you mean.

myfilelist.lst

home/tc/.profile
home/tc/.wbar
etc/shadow
etc/sudoers
big pc man