使用 rsync 保留文件和文件夹权限

W.M*_*.M. 10 linux backup permissions rsync

我使用以下命令维护我的电子邮件帐户的备份:

sudo rsync -av --delete --progress -e "ssh -p pNumber" --rsync-path="/usr/bin/rsync" /vmail/ user@my_backup_server:/home/user/backups/vmail/

来源:大多数电子邮件文件夹归用户所有vmail

目标(备份服务器):系统没有名为 的用户vmail

我的问题是,即使目标机器没有名为 的用户,上述命令是否会保留文件和目录权限vmail?即使两台机器之间的用户名不同(备份服务器上缺少一些用户名),是否可以将文件和权限从目标完全恢复到源。

HBr*_*ijn 11

rsync 如何保留文件的所有权取决于两件事:

  • 您是目的地的超级用户(root)吗?
    否则,您无法使用自己以外的其他用户创建文件和目录。

  • 您使用的是哪个选项标志?

-a选项包括旨在保留所有权的-o, --owner, -g, --group选项。

在文件系统级别,用户和组所有权分别存储在 UID 中。GID 编号。当没有从 UID/GID 到用户名和组名的映射时,工具将只显示这些数字。
同名的用户和组在不同的系统上可以有不同的 UID/GID 号。

默认情况下,rsync 将尝试通过用户名和名称匹配所有权。组名。换句话说,当用户vmail是源文件的所有者时,rsync 将使用户vmail也是目标文件的所有者(即使他们有不同的 UID/GID 号)。
这对于人类来说通常是非常有弹性和最可预测的,因为我们通常不会以 UID/GID 编号的形式查看所有权。

vmail远程目标上没有匹配的用户时,将发生回退场景。然后,Rsync 将保留实际的底层 UID/GID 号,vmail源上用户的 UID 号将用于设置所有者。

当您反转 rsync 方向并恢复备份时,这应该保留正确的所有权。

man rsync

   -o, --owner
          This  option  causes  rsync to set the owner of the destination file to be the same as the source file,
          but only if the receiving rsync is being run as the super-user (see also the --super  and  --fake-super
          options).   Without this option, the owner of new and/or transferred files are set to the invoking user
          on the receiving side.

          The preservation of ownership will associate matching names by default, but may fall back to using  the
          ID number in some circumstances (see also the --numeric-ids option for a full discussion).


   --numeric-ids
          With  this option rsync will transfer numeric group and user IDs rather than using user and group names
          and mapping them at both ends.

          By default rsync will use the username and groupname to determine what ownership  to  give  files.  The
          special  uid  0 and the special group 0 are never mapped via user/group names even if the --numeric-ids
          option is not specified.

          If a user or group has no name on the source system or it has no match on the destination system,  then
          the  numeric ID from the source system is used instead.  See also the comments on the "use chroot" set?
          ting in the rsyncd.conf manpage for information on how the chroot setting affects  rsync’s  ability  to
          look up the names of the users and groups and what you can do about it.
Run Code Online (Sandbox Code Playgroud)

  • 作为普通用户,您无法创建不属于您的文件,因此您需要在目标上以 root 身份登录,或者您必须在本地以 root 身份并在相反方向运行 rsync 并将文件拉入. (2认同)

Sve*_*ven 5

什么rsync副本是文件的数字用户ID,如果不考虑它的存在在目标系统上。如果具有该 id 的用户不存在ls等,则只会显示该编号而不是名称。如果该用户 ID 属于目标系统上的另一个用户名,则该用户现在将拥有该文件。

在这种情况下,备份和还原将正常工作。

  • 默认情况下,rsync 将使用用户名和组名来确定在远程系统上授予文件的所有权,而不是 UID/GID 号。如果用户或组在源系统上没有名称或在目标系统上没有匹配项,则仅使用来自源系统的数字 ID。 (3认同)