Web & Software Developer

Bash Shell Script To Sync Local Directory To Remote Server

Place this bash shell script in any local directory that you want to sync to a remote server. This script is for copying FROM your local directory TO a remote server. When you run the script, it will update the remote server to match your local directory, EXACTLY *. This means that if you delete a file in your local directory, then you sync with this script, that file will be deleted from the remote directory. Thus, the remote directory will be made to match the local directory *.

*There are a few exceptions. Certain files that exist in the local directory will NOT be synced at all. They will be excluded from syncing. They remain only in the local directory and will not be uploaded to the server. You can see the four --exclude options on line 20.

  • --exclude '.git' excludes the .git directory from being uploaded/synced to the server.
  • --exclude '/.gitignore' excludes the .gitignore file from being uploaded/synced to the server.
  • --exclude '/*-config.json' excludes all files ending with “-config.json” from being uploaded/synced to the server.
  • --exclude '/*.sh' excludes all files ending with “.sh” from being uploaded/synced to the server.

(Note: this script will not do anything if it just sits in your local directory. You have to run the script to actually sync your local directory to your remote one. Steps are below.)

In the script below, edit lines 9–11 to set the 3 variables.

Line 9 is your user name that you use to access your server (may be the same as your FTP user name or cPanel user name). This examples uses user123.

Line 10 is the remote server (site) that you want to keep in sync. This example uses domain.com.

Line 11 is your server’s absolute path. This example uses /home/user123/public_html/wp-content/plugins/cool-plugin/. You should be able to get your server’s absolute path by logging in to your hosting account.

Save this as rsync.sh and place it inside the local directory. Don’t worry, this file will not be uploaded to your server. The script excludes all shell scripts from being synced.

Whenever you want to sync the local directory to the remove server, run the script like this: navigate to the local directory in your terminal and enter: ./rsync.sh

If it works, you should see: *** FINISHED ***

The script:

# Sync local plugin dir to remote server
# Place this script in root of local dir to push.
# Configure 3 variables at the top.

set -e

SSHUSER="user123" # user
HOST="domain.com" # remote server
REMOTEPATH="/home/user123/public_html/wp-content/plugins/cool-plugin/" # remote path

# Push to remote with these options and flags:

# -r 		recursive
# -v 		verbose
# -u 		Update files at the destination ONLY if the source copy has been modified more recently
# --exclude	Exclude *.sh and *-config.json
# --delete	Delete files on the destination ONLY if the source copy no longer exists.
rsync -rvu --exclude '.git' --exclude '/.gitignore' --exclude '/*-config.json' --exclude '/*.sh' --delete ${PWD}/ ${SSHUSER}@${HOST}:$REMOTEPATH

echo "*** FINISHED ***"


Questions and Comments are Welcome

Your email address will not be published. All comments will be moderated.

Please wrap code in "code" bracket tags like this: