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 all the --exclude options on line 22, and they are summarized on lines 18-20 of the code snippet.

  • --exclude '.git' excludes the .git directory from being uploaded/synced to the server.
  • --exclude 'bin' excludes the bin directory from being uploaded/synced to the server.
  • --exclude 'tests' excludes the tests directory 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 '/.*' excludes all hidden files from being uploaded/synced to the server. An example of hidden files are filenames beginning with “.git”, such as .gitignore.
  • --exclude '/*.sh' excludes all shell scripts ending with “.sh” from being uploaded/synced to the server.
  • --exclude '/*.xml' excludes all files ending with “.xml”
  • --exclude '/*.dist' excludes all files ending with “.dist”
  • --exclude '/*.md' excludes all files ending with “.md”

(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:

#!/bin/bash
# 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

#config
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 dirs: .git, bin, tests
# --exclude	Exclude *-config.json, all hidden files
# --exclude	Exclude all files ending with .sh, .xml, .dist, .md
# --delete	Delete files on the destination ONLY if the source copy no longer exists.
rsync -rvu --exclude '.git' --exclude 'bin' --exclude 'tests' --exclude '/*-config.json' --exclude '/.*' --exclude '/*.sh' --exclude '/*.xml' --exclude '/*.dist' --exclude '/*.md' --delete ${PWD}/ ${SSHUSER}@${HOST}:$REMOTEPATH

echo "*** FINISHED ***"

See more:

We've One Response

  1. May 30th, 2019 at 3:46 am

    hi isabel,
    i am trying to write same script as this for android(not rooted)
    but rsync is not defined in that.
    the only modules defined in android shell related to synchronization are sync, requestsync.

    with both these modules the script is running but there is no output. nothing is uploaded onto my remote server.

    can you help me out please…….

    tilak

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:

[code]

YOUR CODE HERE 

[/code]