Help me write this bash script

I want to reduce this script in Bash. All it does is upload files via rsync.


# Remote address
[email protected]

# Upload mytheme
rsync -azvP -e 'ssh -p 1234' --delete --exclude=.git --exclude=node_modules /usr/local/var/www/ $REMOTE_ADDRESS:/home/user1/htdocs/

# Upload the other theme
rsync -azvP -e 'ssh -p 1234' --delete --exclude=.git --exclude=node_modules /usr/local/var/www/ $REMOTE_ADDRESS:/home/admin/domains/

What would be the best way?

I tried with

RSYNC_CMD="rsync -azvP -e 'ssh -p 1234' --delete --exclude=.git --exclude=node_modules"

# Upload mytheme
$RSYNC_CMD /usr/local/var/www/ $REMOTE_ADDRESS:/home/user1/htdocs/ --dry-run

But it doesn’t work. Terminal says:

Missing trailing-’ in remote-shell command.
rsync error: syntax or usage error (code 1) at /AppleInternal/Library/BuildRoots/810eba08-405a-11ed-86e9-6af958a02716/Library/Caches/ [sender=2.6.9]

Hopefully you are a pro writing bash scripts :grinning:

You could probably do something like this:


# Remote address
[email protected]

rsynccmd () {
    rsync -avz --delete --exclude-from=exclude.txt --exclude=node_modules "$1" "${REMOTE_ADDRESS}:${2}"

rsynccmd "/usr/local/var/www/" "/home/user1/htdocs/"

Awesome. Thank you!


Bookmarked. Thank you too. Nice way of putting it.

1 Like