48

I would like to backup user files from one server to another with rsync. but I noticed that the user folders change to root. how can I keep the user permissions with rsync (running by root)?

edotan
  • 1,786
  • 12
  • 37
  • 57
  • rsync needs to be run as root when you are making a copy of the drive as well as when you are copying the file back onto the drive. If you do either of the steps as a lower permission user, then root files will be demoted to that user. – Eric Leschinski Jan 03 '16 at 01:00

4 Answers4

64

Use the -a flag, which includes among other things, the options -o and -g, which preserves owners and groups. This requires that you run rsync as root.

Also, see man rsync.

Sven
  • 97,248
  • 13
  • 177
  • 225
  • 1
    Is that also possible without being root? Is there an option to store the mapping in a seperate sidecar file? – thomas.mc.work Aug 08 '18 at 14:13
  • 1
    Missed this comment. No, it doesn't work without being `root` because you are not allowed to change the owner to anyone else then your own user. A mapping file wouldn't help at all with this. – Sven Jul 22 '19 at 09:26
25

Keeping the permissions is achieved through "archive" mode, -a. The common example is -avz:

rsync -avz foo:src/bar/ /data/bar

This ensures that symbolic links, devices, attributes, permissions, ownerships, etc. are preserved in the transfer. Additionally, compression will be used to reduce the size of data portions of the transfer.

Jeremy L
  • 1,030
  • 6
  • 8
6

If you're using rsync for backup I can really recommend using rsnapshot instead (it uses rsync). It rotates the backups and uses hard links so you can see differences between your daily backups (but folders still look like they have the complete contents). I use this for backing up both Windows and Linux servers at work. Perfect for us!

0

From rsyncd.conf manual, you have to specify uid and gid both to 0, besides the -a option on client side of rsync.

  • 1
    AFAIK that does the exact opposite of what the OP requests; that forces rsyncd running as root to ***change*** the ownership to a particular UID/GID rather than to ***preserve*** ownership – Bob Mar 15 '21 at 10:09
  • Preserve the same user ownership as on the source OS, do not change it to root. – Yufeng Lan Mar 23 '21 at 04:42