bash - How do I sync file permissions (chmod +x) with rsync?

16
2014-04
  • Eddy

    I'm having problems synchronising file permissions with rsync. I'm trying to sync my /home/bin folder with the /home/bin folder on a remote machine. The problem is that when I write a new script and make it executable with 'chmod +x' then after performing rsync, the file reverts back to being unexecutable.

    If I use --perms, when I update the file permissions on the remote machine and then use rsync on the host machine, the file permissions don't get updated on the host machine and on the remote machine they get reverted back.

    Is there a way to properly sync the file permissions?

    UPDATE: I forgot to post the script I use:

    #!/bin/bash                                                                     
    
    rsync -e ssh -avz --recursive --progress --update --perms /home/bin [email protected]:/home                                                                              
    rsync -e ssh -avz --recursive --progress --update --perms [email protected]:/home/bin /home
    
  • Answers
  • Caleb

    I think your basic problem here is that rsync is not a good tool for two-way synchronization. I would recommend some solution conceptually different, otherwise you will keep clobbering yourself.

    • You could look into using Unison. It functions similar to rsync but is designed for two say synchronization.
    • Better yet, look into using a source control system like git. This is particularly useful for things like config files and your personal scripts. Each machine you use them on will have a separate checkout, and the versioning takes care of making sure that you always have the latest changes by time including merging multiple different changes rather than just having the data clobbered from any one source.

  • Related Question

    rsync file permissions on windows
  • avguchenko

    I have an rsync service that syncs files from remote machine to a machine that drops them on a network drive.

    I need the copied files to take on the native permissions of the destination folder.

    The sync process runs fine, but after it is finished, I cannot access some of the folders -- Permission Denied.

    I am logged in as domain admin; it won't allow me to modify any permissions on said folders, either. What gives?

    run command:

    rsync.exe  -v -rlt -z --delete "src_path" "dst_path"
    

  • Related Answers
  • avguchenko

    (from http://www.samba.org/ftp/rsync/rsync.html)

    In summary: to give destination files (both old and new) the source permissions, use --perms.

    To give new files the destination-default permissions (while leaving existing files unchanged), make sure that the --perms option is off and use --chmod=ugo=rwX (which ensures that all non-masked bits get enabled).

    If you'd care to make this latter behavior easier to type, you could define a popt alias for it, such as putting this line in the file ~/.popt (the following defines the -Z option, and includes --no-g to use the default group of the destination dir):

        rsync alias -Z --no-p --no-g --chmod=ugo=rwX
    
  • miking

    Cygwin's "posix" security has caused me lots of problems with Windows NTFS file permissions - even using --no-perms with rsync.

    I found that newly-created files/folders don't properly inherit default permissions, but every file/folder ends up with lots of <not inherited> entries in the Windows file/folder Advnanced security tab. (And this problem is not just rsync-related).

    I found this related post and this link both very helpful in how to resolve these problems using the noacl option in cygwin's /etc/fstab file. The downside of this solution is that cygwin loses the ability to set file/folder permissions, but in many cases this is not important.

    (Googling this topic you'll probably find references to setting the CYGWIN=NONTSEC environment variable, but this is for cygwin v1.5 and doesn't work in cygwin v1.7 onwards.)

  • Wernight

    On Windows with DeltaCopy I could make it work with:

    rsync --perms --chmod=a=rw,Da+x ...
    

    It worked even with --recursive