I'm going to be using rsync to sync an old home folder onto a backup drive. I'm thinking of using rsync as root. Is there a way to avoid changing the ownership of the files etc I copy?
3 Answers
I use the following command line to preserve 'everything', file content, ownership and permissions of files, directories, symbolic links etc. This way I have been able to copy a system to a new drive and make it work in another computer. OK, I had to fix the bootloader too, but it works well with copying of the file content, ownership and permissions.
- Please notice the trailing slash on the source directory, and read about it in
man rsync.
rsync -avz foo:src/bar/ /data/tmp A trailing slash on the source changes this behavior to avoid creating an additional directory level at the destination. You can think of a trailing / on a source as meaning "copy the contents of this directory" as opposed to "copy the directory by name", but in both cases the attributes of the containing directory are transferred to the contain‐ ing directory on the destination. In other words, each of the follow‐ ing commands copies the files in the same way, including their setting of the attributes of /dest/foo: rsync -av /src/foo /dest rsync -av /src/foo/ /dest/foo Note also that host and module references don’t require a trailing slash to copy the contents of the default directory. For example, both of these copy the remote directory’s contents into "/dest": rsync -av host: /dest rsync -av host::module /dest You can also use rsync in local-only mode, where both the source and destination don’t have a ’:’ in the name. In this case it behaves like an improved copy command.
-n, start with a 'dry run', to check that things look correct.sudo rsync -Havn source/ targetRemove the option (
-n) and letrsyncdo its job.sudo rsync -Hav source/ target
It will check if each directory/file in the target exists and is up to date, and only copy what needs to be updated (in a backup scenario).
-Hkeeps track of hard links (which save drive space), but makes the copy process slower (the reason that it is not included in-a-ais the standard archive option for backup purposes, which preserves 'everything' about the files in the file system (except hard links).-vis the classic verbose option, which prints all files that are to be copied. There are other options to monitor the progress, that you may like better. You may prefer to turn off verbosity, but it is good in the early stages to check that things work are expected.
- I like it. I think I'll go with it.Steve Wright– Steve Wright2019-01-04 10:08:36 +00:00Commented Jan 4, 2019 at 10:08
- @SteveWright, Good luck :-)sudodus– sudodus2019-01-04 10:10:18 +00:00Commented Jan 4, 2019 at 10:10
You can use the -o and -g options. From the rsync manual (man rsync):
-o,--ownerpreserve owner (super-user only)
-g,--grouppreserve group
Going one step further, an option commonly used with rsync is the -a/--archive option. This option implies -rlptgoD, which are the following options:
-r,--recursiverecurse into directories
-l,--linkscopy symlinks as symlinks
-p,--permspreserve permissions
-t,--timespreserve modification times
-Dsame as--devices --specials(preserve device files, preserve special files)
- I'm not a big fan of -a. Back when I used Cygwin, it mwSteve Wright– Steve Wright2019-01-04 10:04:45 +00:00Commented Jan 4, 2019 at 10:04
- It messed up a bunch of file's I was trying to do this same thing with. Of course Cygwin's rsync was probably the same as the one from BSD (I know its tar wSteve Wright– Steve Wright2019-01-04 10:07:04 +00:00Commented Jan 4, 2019 at 10:07
- I know its tar was.Steve Wright– Steve Wright2019-01-04 10:07:31 +00:00Commented Jan 4, 2019 at 10:07
- @SteveWright, I have a good experience of the
-aoption. I use it often for home directories, even as part of themkusbtool for persistent live drives.sudodus– sudodus2019-01-04 10:09:40 +00:00Commented Jan 4, 2019 at 10:09 - 1A also with Cygwin, it didn't help that NTFS, while somewhat close to one, still isn't a Lunix filesystem.Steve Wright– Steve Wright2019-01-04 10:10:59 +00:00Commented Jan 4, 2019 at 10:10
I use
sudo rsync -HavE /SOURCE /DEST since I have executable bash .sh scripts in the backup.
The acroynm `-HavE is good for remembering "HavEverything"
The acroynm -HavEn is good for remembering "HavEverything" safely
-E, --executability preserve executability
Thanks for the answer @sudodus
Kevin