BASH
Automated Static Website Deployment with SCP/RSYNC
Learn how to write a Bash script for quickly deploying static website files to a remote web server using secure copy (scp) or rsync for efficiency.
#!/bin/bash
# Configuration
LOCAL_DIR="./public"
REMOTE_USER="deployuser"
REMOTE_HOST="your.webserver.com"
REMOTE_PATH="/var/www/html/your_site"
# Check if local directory exists
if [ ! -d "$LOCAL_DIR" ]; then
echo "Error: Local deployment directory '$LOCAL_DIR' not found."
exit 1
fi
echo "Starting deployment of '$LOCAL_DIR' to ${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"
# Deploy using rsync for efficiency (preferable for incremental updates)
# Use -avz for archive mode, verbose, compressed transfer
# Use --delete to remove extra files from destination (sync behavior)
rsync -avz --delete "${LOCAL_DIR}/" "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}" || { echo "Deployment failed with rsync!"; exit 1; }
# Alternative: Deploy using scp (simpler for first-time or full overwrite)
# scp -r "${LOCAL_DIR}/." "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}" || { echo "Deployment failed with scp!"; exit 1; }
echo "Deployment completed successfully."
How it works: This script automates the deployment of a static website. It takes files from a specified local directory (`./public`) and transfers them to a remote web server using `rsync`. `rsync` is preferred for its efficiency, as it only transfers changed files and can optionally delete files on the remote server that no longer exist locally, ensuring synchronization. An `scp` alternative is also commented out for simpler use cases.