DEV Community

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

Posted on

2 2

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.

Speedy emails, satisfied customers

Postmark Image

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

👋 Kindness is contagious

Engage with a sea of insights in this enlightening article, highly esteemed within the encouraging DEV Community. Programmers of every skill level are invited to participate and enrich our shared knowledge.

A simple "thank you" can uplift someone's spirits. Express your appreciation in the comments section!

On DEV, sharing knowledge smooths our journey and strengthens our community bonds. Found this useful? A brief thank you to the author can mean a lot.

Okay