如何使用 Perl 或 shell 以非递归方式迁移目录?

发布于 2024-07-10 07:54:01 字数 672 浏览 4 评论 0原文

我们正在将主文件夹迁移到新的文件系统,我正在寻找一种使用 Perl 或 shell 脚本实现自动化的方法。 我在编程语言方面没有太多选择,因为系统是专有存储集群,应尽可能保持不变。

任务:在目录 /home/ 下,我有各种用户的主文件夹 aaa、bbb、ccc...,它们具有某些权限和用户/组所有权,在迁移到 /newhome/ 时需要保持不变。 以下是需要从 /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/

因此,应在 /newhome 下创建具有权限和所有权的完全相同的文件夹。 复制/移动文件不应该是一个问题,因为它将在稍后处理。

有人创作过这样的剧本吗? 我对 Perl 很陌生,所以我需要帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北风几吹夏 2024-07-17 07:54:01

cp 的 -a 标志将维护权限、修改时间等。您应该能够执行以下操作:

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

在一个目录上尝试一下,看看在自动化之前是否满足您的需要。

编辑:您可以使用 Paul 提到的 rsync 或 tar 来禁用递归文件复制。 使用 rsync,子目录仍被保留,但文件不会被复制:

sudo rsync -pgodt /home/ /newhome/

我还没有尝试过 tar 的 --no-recursion,所以无法对其发表评论。

编辑2:另一种方式

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

参考

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

渔村楼浪 2024-07-17 07:54:01

仅当您以 root 身份执行复制操作时,才能保留所有者和组。 大多数给出的命令都可以工作 - tarcp -rp 选项可以。

唯一需要担心的技巧是不可写目录,但这对于非 root 用户来说是一个问题。 然后,我倾向于使用:

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

-深度 选项意味着文件和子目录在目录本身之前被处理,因此只有在目录的所有内容之后才设置目录的不可写权限。目录已复制到其中。 您还可以使用“sort -r”以相反的顺序列出文件,这可确保目录显示在其内容之后。

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.

娜些时光,永不杰束 2024-07-17 07:54:01

这将创建目录并复制所有文件。

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

如果您不想复制所有文件,可以通过在第一个 tar 命令中添加“--no-recursion”来实现。

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.

夏夜暖风 2024-07-17 07:54:01

如果这些目录位于同一文件系统上,为什么不简单地

cp -p /home/* /newhome/

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

cp -p /home/* /newhome/
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文