I have written a backup script that uses
To make sure all files get backed up, I use
#!/bin/bash set -e expect <<- DONE spawn rsync --rsync-path="sudo\\ rsync" -uav <a href="/cdn-cgi/l/email-protection" data-cfemail="f29f8b87819780b2978a939f829e97dc919d9f">[email protected]</a>:/home/myuser/ /backups/home/myuser expect ":" send -- "mypassword\r" expect eof DONE
This does not work as intended. I get the following error message:
bash: sudo rsync: command not found rsync: connection unexpectedly closed (0 bytes received so far) [Receiver] rsync error: error in rsync protocol data stream (code 12) at io.c(226) [Receiver=3.1.1]
I have seen similar questions
with respect to using spaces in a
command line, and have added single quotes, double quotes, and escape backslashes here and there, but nothing has worked yet.
How do I make "
with spaces" work within an
The problem is that you've got this:
Inside Expect/Tcl, this is seen as:
And, because Tcl's quoting rules are not
the same as bash's, that then uses
with the double quotes
as the command to send to the remote side. Which confuses things terribly. The correct fix is to omit the double quotes; the (backslash-quoted) backslash will ensure that it all gets into spawn as one argument, and gets sent to the other side correctly.
I really don't like using HEREdocs with Tcl. Too many things can go weird when different sorts of quoting interact. It's much better to use a single script in the real target language, since then you can use variables to make things clearer:
#!/usr/bin/env expect set remoteRsync "sudo rsync" set from <a href="/cdn-cgi/l/email-protection" data-cfemail="0865717d7b6d7a486d70696578646d266b6765">[email protected]</a>:/home/myuser/ set to /backups/home/myuser set pass "mypassword" spawn rsync --rsync-path=$remoteRsync -uav $from $to expect ":" send -- "$pass\r" expect eof exit
This makes the structure of the code much simpler to see, and easier to debug. The bit with
at the start is just a way to avoid having the bash wrapper.
And no, those variables won't need quoting at use. Tcl is not bash.