简体   繁体   中英

rsync from bash script port failure

NB, the folders and ip have obv. been changed to be able to post on SO ;)

Based on the following post: Rsync syntax error when run from bash script I have constructed the following bash script to attempt to run an rsync command.. but I cannot get things to work.

By either setting the options as an array, this did not pass the port number...

#!/bin/bash
options=("-avz" "--delete" "--chmod=ug=rwx,o=rx" "--exclude '.idea*" "--exclude '*.git*" "--exclude 'node-sass'" "-e'ssh -p 44'")
src="/cygdrive/d/Work/site/"
trg="john@12.12.12.12:/var/node/john.site.com/"
rsync "${options[@]}" "$src" "$trg"

Or as passing the options as a string..

#!/bin/bash
options="-avz --delete --chmod=ug=rwx,o=rx --exclude '.idea*' --exclude '*.git*' --exclude 'node-sass' -e'ssh -p 44'"
src="/cygdrive/d/Work/site/"
trg="john@12.12.12.12:/var/node/john.site.com/"
rsync "$options" "$src" "$trg"

As a string is just returned an error:

rsync: -avz --delete --chmod=ug=rwx,o=rx --exclude '.idea*' --exclude ' .git ' --exclude 'node-sass' -e'ssh -p 44'#015: unknown option

Here is the working rsync:

rysnc -avz --delete --chmod=ug=rwx,o=rx --exclude ".idea*" --exclude "*.git*" --exclude "node-sass" -e "ssh -p 44" /cygdrive/d/Work/site/  john@12.12.12.12:/var/node/john.site.com/

NEW ATTEMPT following help from below: I have created an ssh config entry to bypass the requirement of setting the port number into the rsync options, removed all the excludes, have entered he opts into a single quoted string and passed via herestring:

#!/bin/bash
opts='-avz --delete --chmod=ug=rwx,o=rx'
src="/cygdrive/d/Work/site/"
trg="john@mysite:/var/node/john.site.com/"
rsync <<<$opts "$src" "$trg"

The new command via cli looks like (and works):

rsync -avz --delete --chmod=ug=rwx,o=rx /cygdrive/d/Work/site/ john@mysite:/var/node/john.site.com/

But i get:

rsync: link_stat "/cygdrive/d/Work/site/\#015" failed: No such file or directory (2) 
rsync error: some files/attrs were not transferred (see previous errors) (code 23) at main.c(1165) [sender=3.1.1]

Don't re-group options when quoting. Try this:

options=(-avz --delete --chmod=ug=rwx,o=rx --exclude ".idea*" --exclude "*.git*" --exclude "node-sass" -e "ssh -p 44")
src=/cygdrive/d/Work/site/
trg="john@87.87.87.87:/var/node/john.site.com/"
rsync "${options[@]}" "$src" "$trg"

The subtle issue that is causing trouble is the -e "ssh -p 44" option. When using the -e or --rsh=COMMAND :

Command-line arguments are permitted in COMMAND provided that COMMAND is presented to rsync as a single argument .

Moving -e "ssh -p 44" out of the option string allows it to work as intended. The array approach would look like:

opts=( -avz --delete --chmod=ug=rwx,o=rx --exclude '.idea*' --exclude '*.git*' --exclude node-sass )

As mentioned in the comments, a single variable is not recommended from a best practices standpoint. However, you can confirm the following would also work:

opts='-avz --delete --chmod=ug=rwx,o=rx --exclude .idea* --exclude *.git* --exclude node-sass'

You can specify your source and destination any way you like:

src="/cygdrive/d/Work/site/"

dest="john@12.12.12.12:/var/node/john.site.com/"

The rsync call using an option array becomes:

rsync ${opts[@]} -e "ssh -p 44" "$src" "$dest"

Or, for testing purposes, you can use the single variable as follows:

rsync $opts -e "ssh -p 44" "$src" "$dest"

configure ssh to use port 44 for host 12.12.12.12

Note: if you will be communicating with 12.12.12.12 on port 44 on a regular basis, the it makes much more sense to provide a ~/.ssh/config specification telling ssh (the default transport for rsync ) that 12.12.12.12 communicates on port 44 . The HOST PORT config format in ~/.ssh/config is for example:

Host 12.12.12.12
Port 44

Create or edit the file, add the above options, and you can remove the -e "ssh -p 44" option completely from then on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM