views:

777

answers:

4

Hello,

We're migrating home folders to a new filesystem, and I am looking for a way to automate it using Perl or a shell script. I don't have much choice in programming languages as the systems are proprietary storage clusters that should remain as unchanged as possible.

Task: Under directory /home/ I have various users' home folders aaa, bbb, ccc, ... and they have certain permissions and user/group ownership that need to remain intact upon migration to /newhome/. Here's example of what needs to be migrated from /home:

drwxr-xr-x    3 aaaaa    xxxxxxxxx   4096 Feb 26  2008 aaaaa/
drwxrwxrwx   88 bbbbbbb  yyyyyy      8192 Dec 16 16:32 bbbbbbb/
drwxr-xr-x    6 ccccc    yyyyyy      4096 Nov 24 04:38 ccccc/
drwxr-xrwx   36 dddddd   yyyyyy      4096 Jun 20  2008 dddddd/
drwxr-xr-x   27 eee      yyyyyy      4096 Dec 16 02:56 eee/

So, exact same folders with permissions and ownerships should be created under /newhome. Copying/moving files should not be a concern, as it will be handled later.

Anyone has worked on such script? I am really new to Perl, so I need help.

+8  A: 

cp's -a flag will maintain permission, modification times etc. You should for be able to do something like:

for a in `ls /home`; do cp -a "/home/$a" "/newhome/$a" ; done

Try it with one directory to see if does what you need before automating it.

EDIT: You can disable recursive file copying by using rsync or tar as mentioned by Paul. With rsync, subdirectories are still preserved, but files aren't copied:

sudo rsync -pgodt /home/ /newhome/

I haven't tried tar's --no-recursion, so can't comment on it.

EDIT 2: Another way

find /home/ -maxdepth 1 -print | sudo cpio -pamVd /newhome

Reference

codelogic
Great 1-liner, thanks! Is there a way to tell cp not to copy sub-folders and files of each user directory? In other words, create just the top user directories under /home without copying content.
DV
@DV: The man page states cp -a is equivalent to cp -dpr ... since the -r handles recursion, cp -dp should do what you want without recursion.
Adam Bellaire
Adam, cp omits directories if -r is not specified, so -dp would only apply to files AFAIK.
codelogic
codelogic is right, I can't get it to work as 'cp -dp' :( It seems that there's no way to have cp only copy the top folders below /home without copying all of their contents... :/
DV
cpio also seems to do the trick.
codelogic
@codelogic: Gotcha, I didn't realize DV wanted to create the top-level directories and leave them empty. My bad.
Adam Bellaire
Holy crap, the power of knowing the right commands. Big thanks, codelogic! Your last edit works perfectly!
DV
`-print` is unnecessary so it could be dropped or better yet replaced by `-print0`.
J.F. Sebastian
So my first part of the command is:"find /home -maxdepth 1 -type d"and then I pipe the output to cpio.
DV
+3  A: 

This will create the directories and copy all the files.

cd /home; tar cvBf - . | (cd /newhome; tar xvpBf -)

If you don't want to copy all the files, you might be able to do that by adding a "--no-recursion" to the first tar command.

Paul Tomblin
I'll try that. I'm looking for no recursion, thanks for the tip!
DV
Unfortunately it didn't copy permissions/ownership when I tried it. Maybe I wasn't doing something right.
DV
@DV: add `sudo` before `tar`.
J.F. Sebastian
@DV - or do it as root. Make sure you include that "p" in the second tar, that copies the permissions.
Paul Tomblin
+1  A: 

If these directories are on the same filesystem, why not simply

cp -p /home/* /newhome/
Nikron
I'll try that - I can mount the /newhome/ through the NFS. Thanks!
DV
That does it, but there's still recursion (i.e. it copies all sub-directories and files as well). Is there a way to tell cp not do recursion?
DV
+4  A: 

You can only preserve the owner and group if you do the copying operation as root. Most of the commands given will work - the tar and the cp -rp options will.

The only trick to worry about is non-writable directories, but that's an issue for non-root users. Then, I tend to use:

(cd /home; find . -depth) | cpio -pvdumB /newhome

The -depth option means that file and sub-directories are processed before the directories themselves, so the no-write permission on the directory is only set after all the contents of the directory have been copied into it. You can also use a 'sort -r' to list files in reverse order, which ensures that directories appear after their contents.

Jonathan Leffler