BASH
Deploy Project Files Incrementally with rsync
Efficiently deploy web project files to a remote server using rsync, ensuring only changed files are transferred and excluding sensitive directories.
#!/bin/bash
LOCAL_PATH="./dist/" # Local directory to sync
REMOTE_USER="webuser"
REMOTE_HOST="your_server_ip"
REMOTE_PATH="/var/www/html/your_project/"
# Exclude node_modules, .git, and other unnecessary files
EXCLUDES="--exclude 'node_modules/' --exclude '.git/' --exclude '.env' --exclude 'logs/'"
echo "Syncing files from $LOCAL_PATH to $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"
# Use -avz for archive, verbose, compress
# --delete for deleting extraneous files on the receiving side
rsync -avz --delete $EXCLUDES $LOCAL_PATH $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH
if [ $? -eq 0 ]; then
echo "Deployment successful."
else
echo "Deployment failed. Check rsync output for errors."
exit 1
fi
How it works: This script automates the deployment of web project files to a remote server using `rsync`. It leverages `rsync`'s efficiency to only transfer changed files, significantly speeding up deployments. It also includes common exclusion patterns to prevent sensitive or irrelevant files from being deployed, such as `node_modules` or `.git` directories, ensuring a clean and efficient deployment.