I am trying to rsync multiple directories in a single call inside of a bash script and am running into trouble with the syntax for quoting the paths.
Here is what I am trying:
backuppath='/path/to/backup/folders/'
declare -a backupitems=('folder1' 'folder2')
backupitems=(${backupitems[@]/#/\"$backuppath})
backupitems=(${backupitems[@]/%/\"})
backup=${backupitems[@]}
rsync ${backup} /path/to/destination
I get a link_stat error saying no such file or directory for "/path/to/current/directory"/path/to/backup/folders/folder1"" and then the same error for folder2. So it seems like it is generating the quoted path like I want it to, but rsync is interpreting the paths as relative and adding on the path to the current directory at the front. rsync works correctly if I do
backuppath='path/to/backup/folders/folder1'
rsync "${backuppath}" /path/to/destination
putting the quotes into the rsync command, but I can't do this with multiple folders within one variable because it treats the multiple directories as a long single path. I got the script to work using the second method by looping over the folders and calling rsync on each one, but this method is slightly more awkward because of the way other parts of the script handle the folders so I would like to get the first method to work if there is a quick fix.
Edit:
For the top version above, with no spaces in any of the directory names, I see the following command using set -vx:
rsync '"/path/to/backup/folders/folder1"' '"/path/to/backup/folders/folder2"' /path/to/destination
and I get the following error message:
rsync: link_stat "/path/to/current/directory/"/path/to/backup/folders/folder1"" failed: No such file or directory (2)
If I use the version suggested by @kdubs, then everything works when there are no spaces in the paths.
When there are spaces in the path, kdubs version results in the command:
rsync /path/to/back up/folders/folder1 /path/to/back up/folders/folder2 /path/to/destination
and the errors:
rsync: link_stat "/path/to/back" failed: No such file or directory (2)
rsync: link_stat "/path/to/back up/up/folder1" failed: No such file or directory (2)
My first version needs an additional tweak to work with spaces because expanding the array with [@] and creating a new array by enclosing the expansion with () causes the spaces in the path to break up the path into multiple array elements (see Tonin's answer below).
set -vx; rsync ... ; set +vx. Good luck. - shellter