DEV Community

Cover image for Publish File Changes to Remote Server with Rsync

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:


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

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/
  echo "./dist does not exist"

Run this script       # preview all changes --run # upload all changes

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

chmod +x

All codes in this post is provided under CC0 licence.

Top comments (0)