Help me write this bash script

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

#!/bin/bash

# Remote address
REMOTE_ADDRESS=root@hostname.example.com

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

# Upload the other theme
rsync -azvP -e 'ssh -p 1234' --delete --exclude=.git --exclude=node_modules /usr/local/var/www/example.com/wp-content/themes/this-is-another-theme/ $REMOTE_ADDRESS:/home/admin/domains/example2.com/public_html/wp-content/themes/this-is-another-theme/

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/example.com/wp-content/themes/this-is-another-theme/ $REMOTE_ADDRESS:/home/user1/htdocs/example1.com/wp-content/themes/mytheme/ --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/com.apple.xbs/Sources/rsync/rsync/main.c(352) [sender=2.6.9]

Hopefully you are a pro writing bash scripts :grinning:

You could probably do something like this:

#!/bin/bash

# Remote address
REMOTE_ADDRESS=root@hostname.example.com

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

rsynccmd "/usr/local/var/www/example.com/wp-content/themes/mytheme/" "/home/user1/htdocs/example1.com/wp-content/themes/mytheme/"
8 Likes

Awesome. Thank you!

2 Likes

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

1 Like