大网数据广告活动
服务器运维问答

Preserving file and folder permissions with rsync

· 52CE 我爱测

I maintain a backup of my email accounts using this command: sudo rsync -av --delete --progress -e "ssh -p pNumber" --rsync-path="/usr/bin/rsync" /vmail/ user@my_backup_server:/home/user/backups/vmail/ Source: Most email folders …

问题原文

原帖链接 · W.M. · CC BY-SA 4.0

I maintain a backup of my email accounts using this command:

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

Source: Most email folders are owned by user vmail.

Destination (backup server): System doesn't have a user named vmail.

My question, would the above command preserve file and directory permissions even if destination machine doesn't have a user named vmail? Would it be possible to restore the files and permissions completely from destination to source even if the user names between the two machines are not the same (some missing on backup server).

社区高分解答

原帖链接 · HBruijn · CC BY-SA 4.0

How rsync preserves ownership of files depends on two things:

  • Are you super-user (root) on the destination?
    Otherwise you can't create files and directories with a different user other than your own.

  • Which option flags are you using?

The -a option includes the -o, --owner, -g, --group options designed to preserve ownership.

At the file-system level user and group ownership is stored in UID resp. GID numbers. When there is no mapping from UID/GID's to usernames and groupnames tools will simply display those numbers instead.
Users and groups with the same names can have different UID/GID numbers on different systems.

By default rsync will try to match the ownership by username resp. groupname. In other words when the user vmail is the owner of a file at the source, rsync will make the user vmail also the owner at the destination (even when they have different UID/GID numbers).
That is usually quite resilient and the most predictable for humans as we normally don't look at ownership in the form of UID/GID numbers.

When no matching user vmail is present on the remote destination, then a fall-back scenario will happen. Rsync will then preserve the actual underlying UID/GID numbers and the UID number of the vmail user on the source will used to set the owner.

That should preserver the correct ownership when you reverse the rsync direction and restore the backup.

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2019-04-23)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。