BASH

Deploy Static Site Content with rsync

Efficient Bash script to synchronize local static website files to a remote server using rsync, ideal for continuous deployment and updates.

#!/bin/bash

# Configuration
LOCAL_PATH="./dist/" # Path to your built static files
REMOTE_USER="webmaster"
REMOTE_HOST="your_server_ip_or_domain"
REMOTE_PATH="/var/www/your_website/" # Destination path on the remote server

# Check if local path exists
if [ ! -d "$LOCAL_PATH" ]; then
  echo "Error: Local path '$LOCAL_PATH' does not exist. Build your project first?"
  exit 1
fi

echo "Synchronizing files from '$LOCAL_PATH' to '$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH'..."

# rsync command:
# -a: archive mode (recursively copy files, preserve permissions, etc.)
# -v: verbose
# -z: compress file data during transfer
# --delete: delete extraneous files from dest dir (receiver) that are not in src dir
# --exclude: exclude specific files/directories
rsync -avz --delete --exclude 'node_modules/' --exclude '.git/' "$LOCAL_PATH" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

if [ $? -eq 0 ]; then
  echo "Deployment successful."
else
  echo "Deployment failed."
  exit 1
fi
How it works: This script leverages `rsync` for efficient deployment of static website content. It synchronizes files from a local build directory to a remote server, only transferring changed files, deleting outdated files on the destination, and compressing data during transfer. This makes it a powerful tool for rapid and reliable updates, significantly faster than a full copy for subsequent deployments.

Need help integrating this into your project?

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

Hire DigitalCodeLabs