Rsync doesn’t preserve directory ownership even with -a

filespermissionsrsyncusers

I use this rsync invocation to backup my home directory:

rsync -aARrx --info= --force --delete --info=progress2 -F "$USER_HOME" "$BACKUP_MNTPOINT"

rsync man page says that -a implies -g and -o (among other switches), which should preserve ownership. However I've noticed that if a directory does not exist under $BACKUP_MNTPOINT/$USER_HOME, it is created with root:root ownership instead of the correct one. (This only happens with directories right under $BACKUP_MNTPOINT/$USER_HOME). Why is that?

$BACKUP_MNTPOINT is a localy mounted drive. $BACKUP_MNTPOINT/$USER_HOME does have the right ownership and permissions. Neither $USER_HOME nor $BACKUP_MNTPOINT end with a slash.

Both the source and the target filesystems are XFS and running mkdir $BACKUP_MNTPOINT/$USER_HOME creates a directory with the expected ownership.

Best Answer

I had a similar problem when using rsync to backup my system to my server. I used:

rsync -aAXSHPr \
-e ssh \
--rsync-path="sudo /usr/bin/rsync/" \
--numeric-ids \
--delete \
--progress \
--exclude-from="/path/to/file/that/lists/excluded/folders.txt" \
--include-from="/path/to/file/that/lists/included/folders.txt" \
/ USER@SERVER:/path/to/folder/where/backup/should/go/

The solution is that there is not really a problem. I suspect that you aborted the rsync process once you saw that it creates folders with wrong permissions set. The crux is that rsync only sets the permissions of a parent-folder once it is done syncing all subfolders and files of it.

Related Question