BASH

Deploy Static Website Files with rsync

A simple Bash script to efficiently deploy local static website files to a remote server using rsync, ensuring only changed files are transferred.

#!/bin/bash

LOCAL_PATH="./public/"
REMOTE_USER="your_ssh_user"
REMOTE_HOST="your_remote_host.com"
REMOTE_PATH="/var/www/html/your_site/"

# rsync options:
# -avz: Archive mode, verbose, compress
# --delete: Delete extraneous files from dest dir (not in source)
# --progress: Show progress during transfer

rsync -avz --delete --progress "$LOCAL_PATH" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

if [ $? -eq 0 ]; then
    echo "Static website deployment successful to $REMOTE_HOST:$REMOTE_PATH"
else
    echo "Static website deployment FAILED!" >&2
    exit 1
fi
How it works: This script automates the deployment of static website content using `rsync`, a powerful utility for synchronizing files. It transfers files from a local source directory (`LOCAL_PATH`) to a specified remote server and destination path. The `-avz` options enable archive mode (preserving permissions, timestamps, etc.), verbose output, and compression. The `--delete` option ensures that any files on the remote server that are no longer present locally are removed, keeping the deployment clean and efficient.

Need help integrating this into your project?

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

Hire DigitalCodeLabs