BASH
Automating Web Application Deployment via Rsync over SSH
Streamline web application deployments by securely synchronizing local files to a remote server using rsync over SSH, ensuring efficient updates and version control.
#!/bin/bash
# Configuration
USER="your_ssh_user"
HOST="your_remote_host.com"
REMOTE_PATH="/var/www/your_app"
LOCAL_PATH="./dist/"
# --- DO NOT EDIT BELOW THIS LINE ---
# Check if rsync is installed
if ! command -v rsync &> /dev/null
then
echo "rsync could not be found. Please install it."
exit 1
fi
echo "Starting deployment to $USER@$HOST:$REMOTE_PATH"
# -a: archive mode (preserves permissions, times, etc.)
# -v: verbose
# -z: compress file data during the transfer
# --delete: delete extraneous files from destination dirs (not in source)
# --exclude: exclude specific files/directories (e.g., node_modules, .git)
rsync -avz --delete \
--exclude ".git/" \
--exclude "node_modules/" \
--exclude "*.log" \
"${LOCAL_PATH}" "${USER}@${HOST}:${REMOTE_PATH}"
if [ $? -eq 0 ]; then
echo "Deployment successful!"
else
echo "Deployment failed!"
exit 1
fi
# Optional: Restart a service on the remote server after deployment
# ssh "${USER}@${HOST}" "sudo systemctl restart your_web_service"
# echo "Web service restarted successfully on remote host."
How it works: This bash script automates the deployment of web application files to a remote server using `rsync` over SSH. It's configured with a remote user, host, and paths. The script uses archive mode (`-a`) for comprehensive synchronization, verbose output (`-v`), and compression (`-z`) for efficiency. The `--delete` flag ensures that files removed locally are also removed from the remote, keeping the environments in sync. Important directories like `.git/` and `node_modules/` are excluded from the transfer. After a successful transfer, it can optionally execute commands on the remote server, like restarting a web service, providing a complete deployment pipeline.