I have a Bash script that I plan to run for daily backups (my HOME is /Volumes/Norman Data/me):
#!/bin/bash
# Halt the script on any errors.
set -e
# Remote server where
remote_server="example.com"
# Destination Folder on Remote Server
target_path="backup/"
# User (with sshy key) on remote server
usr="me"
# ${HOME} evaluates to '/Volumes/Norman Data/me'
# A list of absolute paths to backup.
include_paths=(
# "~/.ssh"
# "~/.bash_history"
"${HOME}/.bash_profile"
# "~/.vimrc"
# "~/.gitconfig"
# "~/.gitignore"
# "~/.zshrc"
# "~/Artifacts"
"${HOME}/Documents --exclude=remote"
# "~/Downloads"
# "~/Desktop"
# "~/Pictures"
# "~/Projects --exclude 'remote'"
# "~/Movies"
)
# A list of folder names and files to exclude.
exclude_paths=(
".bundle"
"node_modules"
"tmp"
)
# Passing list of paths to exclude
for item in "${exclude_paths[@]}"
do
exclude_flags="${exclude_flags} --exclude=${item}"
done
# Passing list of paths to copy
for item in "${include_paths[@]}"
do
include_args="${include_args} '${item}'"
done
str="rsync -auvzP ${exclude_flags} ${include_args} ${usr}@${remote_server}:${target_path}"
echo "Running: ${str}"
${str}
Running that results in:
building file list ...
rsync: link_stat "/Volumes/Norman" failed: No such file or directory (2)
rsync: link_stat "/Volumes/Norman Data/me/Data/me/.bash_profile" failed: No such file or directory (2)
rsync: link_stat "/Volumes/Norman" failed: No such file or directory (2)
rsync: link_stat "/Volumes/Norman Data/me/Data/me/Documents" failed: No such file or directory (2)
0 files to consider
sent 29 bytes received 20 bytes 32.67 bytes/sec
total size is 0 speedup is 0.00
rsync error: some files could not be transferred (code 23) at
/BuildRoot/Library/Caches/com.apple.xbs/Sources/rsync/rsync-52/rsync/main.c(996) [sender=2.6.9]
As far as I can tell, the space in the value of my HOME
is causing problems. I thought quoting it like "${HOME}/.bash_profile"
would take care of the spaces. And it seems to, kind of. By this I mean, the value I get from echo "Running: ${str}"
is
rsync -auvzP --exclude=.bundle --exclude=node_modules --exclude=tmp '/Volumes/Norman Data/me/.bash_profile' '/Volumes/Norman Data/me/Documents' --exclude=remote me@example.com:backup/
And when I run that directly in the terminal or paste it in the script (in place of ${str}
, it works as expected. The errors above only happen when using the variable.
Can’t seem to figure out what I’m missing here. Can anyone shed some light?
** script adapted from https://gitlab.com/ramawat/randomhacks/blob/master/backup_script.txt