BASH
Automated Website Deployment with Rsync
Synchronize local website files to a remote server securely and efficiently using Rsync over SSH for streamlined automated deployments.
#!/bin/bash
# --- Configuration ---
# Remote user and host (e.g., [email protected])
REMOTE_HOST="[email protected]"
# Remote path where files should be deployed
REMOTE_PATH="/var/www/html/your-website"
# Local path to your website's build or public directory
LOCAL_PATH="./dist/" # Example for a build directory
# Optional: Path to your SSH private key if not using default agent
SSH_KEY_PATH="~/.ssh/id_rsa"
# --- Deployment Function ---
deploy_website() {
echo "Starting deployment to $REMOTE_HOST:$REMOTE_PATH..."
# Rsync command:
# -a, --archive: Archive mode; equals -rlptgoD (no -H,-A,-X)
# -v, --verbose: Increase verbosity
# -z, --compress: Compress file data during the transfer
# --delete: Delete extraneous files from dest dirs (careful!)
# -e "ssh -i $SSH_KEY_PATH": Use SSH for transport, specify key
rsync -avz --delete \
-e "ssh -i $SSH_KEY_PATH" \
"$LOCAL_PATH" \
"$REMOTE_HOST:$REMOTE_PATH"
if [ $? -eq 0 ]; then
echo "Deployment successful!"
else
echo "Deployment failed. Check error messages above."
exit 1
fi
}
# --- Main Execution ---
read -p "Are you sure you want to deploy to production? (y/N) " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
deploy_website
else
echo "Deployment cancelled."
exit 0
fi
How it works: This script automates the deployment of web project files to a remote server using `rsync` over SSH. It configures the remote host, path, and local source, then uses `rsync`'s powerful options for efficient, incremental, and secure file synchronization. A confirmation prompt is included to prevent accidental deployments.