1

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 3

2

You can use the -o and -g options. From the rsync manual (man rsync):

-o, --owner preserve owner (super-user only)

-g, --group preserve 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, --recursive recurse into directories

-l, --links copy symlinks as symlinks

-p, --perms preserve permissions

-t, --times preserve modification times

-D same as --devices --specials (preserve device files, preserve special files)

6
  • I'm not a big fan of -a. Back when I used Cygwin, it mw 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 w Jan 4, 2019 at 10:07
  • I know its tar was. Jan 4, 2019 at 10:07
  • @SteveWright, I have a good experience of the -a option. I use it often for home directories, even as part of the mkusb tool for persistent live drives.
    – sudodus
    Jan 4, 2019 at 10:09
  • 1
    A also with Cygwin, it didn't help that NTFS, while somewhat close to one, still isn't a Lunix filesystem. Jan 4, 2019 at 10:10
2

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/ target
    
  • Remove the option (-n) and let rsync do 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).


  • -H keeps track of hard links (which save drive space), but makes the copy process slower (the reason that it is not included in -a
  • -a is the standard archive option for backup purposes, which preserves 'everything' about the files in the file system (except hard links).
  • -v is 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.
2
  • I like it. I think I'll go with it. Jan 4, 2019 at 10:08
  • @SteveWright, Good luck :-)
    – sudodus
    Jan 4, 2019 at 10:10
1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .