DEV Community

Cover image for Publish File Changes to Remote Server with Rsync
NETIC
NETIC

Posted on

Publish File Changes to Remote Server with Rsync

I am sharing a small script to automate file upload from local to remote server with preview option.

I use rsync to mirror both directories. It will only upload changes and will delete any file from remote directory that are not in local.

This script will default to dry-run (no changes will be made). Just add --run option to upload and write changes.

Script File: rsync-www.sh

#!/bin/zsh

# default to --dry-run to prevent accidental sync
SYNC_OPTION="--dry-run"
if [[ $1 == "--run" ]]; then
  # remove dry-run option and sync
  SYNC_OPTION=
fi

if [[ -d "./dist" ]]; then
  # sync/mirror ./dist/ with remote dir, delete extranous file in remote dir
  rsync -avz $SYNC_OPTION --delete ./dist/ you@yourser.ver:/var/www/html/
else
  echo "./dist does not exist"
fi

Run this script

rsync-www.sh       # preview all changes
rsync-www.sh --run # upload all changes

Note -- You might need to change file mode to executable

chmod +x rysnc-www.sh

All codes in this post is provided under CC0 licence.

Oldest comments (0)