← Back to all snippets
BASH

Deploying Static Website Files with Rsync

Streamline your static website deployments using a Bash script with `rsync` to efficiently synchronize local files to a remote server over SSH.

#!/bin/bash
# --- Configuration ---
LOCAL_PATH="/path/to/your/local/website/build"
REMOTE_USER="your_ssh_user"
REMOTE_HOST="your_remote_server.com"
REMOTE_PATH="/var/www/your_website"

# --- Rsync Command ---
# -a: archive mode (recursively copy, preserve symlinks, permissions, ownership, timestamps)
# -v: verbose output
# -z: compress file data during transfer
# --delete: delete extraneous files from destination dirs (not in source)
# --exclude: exclude specific files/directories (e.g., .git, node_modules)

echo "Deploying from ${LOCAL_PATH} to ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}..."

if rsync -avz --delete \
    --exclude='.git/' \
    --exclude='node_modules/' \
    "${LOCAL_PATH}/" "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}/"; then
    echo "Deployment successful."
else
    echo "Error: Deployment failed." >&2
    exit 1
fi
How it works: This script leverages `rsync` to efficiently deploy static website files from a local build directory to a remote server. The `-avz` flags enable archive mode (preserving file attributes), verbose output, and compression during transfer. `--delete` ensures that files removed locally are also removed from the remote destination, keeping the remote synchronized. `--exclude` prevents specific directories (like `.git` or `node_modules`) from being transferred. It's a powerful tool for quick and incremental deployments.

Need help integrating this into your project?

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

Hire DigitalCodeLabs