BASH

Efficient Static Site Deployment with rsync

Master efficient website deployments using rsync for static sites or build artifacts. This bash script ensures only changed files are transferred, saving bandwidth and time.

#!/bin/bash

# Configuration
LOCAL_BUILD_DIR="./dist"
REMOTE_USER="deployuser"
REMOTE_HOST="your-webserver.com"
REMOTE_PATH="/var/www/mywebsite.com"

# Check if local build directory exists
if [ ! -d "$LOCAL_BUILD_DIR" ]; then
  echo "Error: Local build directory '$LOCAL_BUILD_DIR' not found. Please build your project first."
  exit 1
fi

echo "Starting rsync deployment from $LOCAL_BUILD_DIR to $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

# rsync options:
# -avz: Archive mode, verbose, compress
# --delete: Delete extraneous files from dest dir (not in source)
# --progress: Show progress during transfer
# --exclude: Exclude files/directories (e.g., .git, node_modules)
rsync -avz --delete --progress "$LOCAL_BUILD_DIR/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

if [ $? -eq 0 ]; then
  echo "Deployment successful!"
else
  echo "Deployment failed! Check rsync output above."
  exit 1
fi
How it works: This script uses `rsync` for incremental and efficient deployment of static websites or application build artifacts to a remote server. It transfers only the files that have changed, saving significant time and bandwidth compared to full uploads. The `--delete` option ensures the remote directory mirrors the local source, removing obsolete files, making it perfect for CI/CD pipelines.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs