BASH
Automate Website Deployment with Rsync
Streamline web deployments by synchronizing local changes to a remote server efficiently using rsync, ensuring only modified files are transferred, saving time and bandwidth.
#!/bin/bash
# Configuration variables
LOCAL_PATH="/path/to/your/local/project" # Local project directory
REMOTE_USER="ssh_user" # SSH username for remote server
REMOTE_HOST="your-server.com" # Remote server hostname or IP
REMOTE_PATH="/var/www/html/your-site" # Destination path on remote server
# Files/directories to exclude from deployment
# Add any sensitive files or build artifacts that shouldn't be deployed
EXCLUDES="--exclude 'node_modules/' --exclude '.git/' --exclude '.env' --exclude 'tmp/' --exclude 'README.md'"
echo "Starting deployment from $LOCAL_PATH to $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"
# Execute rsync command
# -a: archive mode (preserves permissions, timestamps, etc.)
# -v: verbose output
# -z: compress file data during transfer
# --delete: delete extraneous files from destination dirs (not in source)
# $LOCAL_PATH/ (trailing slash is crucial to sync contents, not the directory itself)
rsync -avz --delete $EXCLUDES $LOCAL_PATH/ $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/
# Check the exit status of rsync
if [ $? -eq 0 ]; then
echo "Deployment successful!"
else
echo "Deployment failed. Please check logs for errors."
exit 1
fi
How it works: This script automates website deployment using `rsync`, a powerful utility for synchronizing files. It transfers only modified or new files from a local project directory to a remote server, significantly speeding up deployments. The `--delete` flag removes files on the remote that no longer exist locally, keeping the remote synchronized, and `EXCLUDES` prevents sensitive or unnecessary files (like `.git` folders or `node_modules`) from being deployed.