← Back to all snippets
BASH

Efficiently Deploy Web Project Files with Rsync

Learn to use rsync in a bash script for efficient, incremental file synchronization and deployment of web project files to a remote server over SSH, saving time and bandwidth.

#!/bin/bash

# Configuration
LOCAL_PATH="/path/to/local/project"
REMOTE_USER="ssh_user"
REMOTE_HOST="your_remote_server.com"
REMOTE_PATH="/var/www/html/mywebsite"

# Optional: Exclude files/directories (e.g., node_modules, .git, .env)
EXCLUDES="--exclude=.git --exclude=node_modules --exclude=.env --exclude=vendor"

# Perform rsync deployment
# -a: archive mode (preserves permissions, timestamps, etc.)
# -v: verbose output
# -z: compress file data during transfer
# --delete: delete extraneous files from dest (not in source)
# --progress: show progress during transfer
rsync -avz --delete --progress $EXCLUDES "$LOCAL_PATH/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

if [ $? -eq 0 ]; then
    echo "Deployment successful to $REMOTE_HOST"
else
    echo "Deployment failed!"
    exit 1
fi
How it works: This script uses `rsync` for robust and efficient deployment of web project files. `rsync` only transfers changed files, making subsequent deployments much faster than a full copy. The script includes common flags like `-avz` for archive mode, verbosity, and compression, along with `--delete` to remove files on the remote that no longer exist locally, and `--exclude` for ignoring specific files or directories like build artifacts or sensitive environment files.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs