Note that there are some explanatory texts on larger screens.

plurals
  1. POrsync run from bash script not preserving ownership
    text
    copied!<p>I'm trying to create a bash script which will sync a directory specified as a command line parameter to a remote server (also specified by a parameter). At the moment, I'm using <code>eval</code>, which solves a parameter expansion problem, but for some reason causes rsync not to preserve ownership on the remote files (apart from being Evil, I know). Running the rsync command with all the same flags and parameters from the command prompt works fine.</p> <p>I tried using <code>$()</code> as an alternative, but I got into a real mess with variable expansion and protecting the bits that need protecting for the remote rsync path (which needs both quotes <em>and</em> backslashes for paths with spaces).</p> <p>So - I guess 2 questions - is there a reason that <code>eval</code> is preventing <code>rsync</code> from preserving ownership (the bash script is being run as root on the source machine, and <code>ssh</code>ing to the remote machine as root too - just for now)? And is there a way of getting <code>$()</code> to work in this scenario? The (trimmed) code is below:</p> <pre><code>#!/bin/bash RSYNC_CMD="/usr/bin/rsync" RSYNC_FLAGS="-az --rsh=\"/usr/bin/ssh -i \${DST_KEY}\"" # Protect ${DST_KEY} until it is assigned later SRC=${1} # Normally this is sense checked and processed to be a canonical path # Logic for setting DST based on command line parameter snipped for clarity - just directly assign for testing DST='root@some.server.com:' DST_KEY='/path/to/sshKey.rsa' TARG=${DST}${SRC//' '/'\ '} # Escape whitespace for target system eval ${RSYNC_CMD} ${RSYNC_FLAGS} \"${SRC}\" \"${TARG}\" # Put quotes round the paths - even though ${TARG} is already escaped # All synced OK - but ownership not preserved despite -a flag </code></pre> <p>I've tried changing <code>RSYNC_CMD</code> to <code>sudo /usr/bin/rsync</code>, and also adding <code>--rsync-path="sudo /usr/bin/rsync</code> to <code>RSYNC_FLAGS</code>, but neither made any difference. I just can't see what I'm missing...</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload