BASH

Efficient Remote File Deployment with Rsync

Automate the deployment of your static website files or build artifacts to a remote server using rsync, ensuring only changed files are transferred efficiently.

#!/bin/bash
LOCAL_PATH="./dist/" # Source directory on local machine
REMOTE_USER="webuser"
REMOTE_HOST="your_server_ip"
REMOTE_PATH="/var/www/html/" # Destination directory on remote server

# Ensure the local path exists
if [ ! -d "$LOCAL_PATH" ]; then
    echo "Error: Local source directory '$LOCAL_PATH' does not exist." >&2
    exit 1
fi

echo "Starting rsync deployment to $REMOTE_HOST:$REMOTE_PATH"
rsync -avz --delete "$LOCAL_PATH" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

if [ $? -eq 0 ]; then
    echo "Rsync deployment successful."
else
    echo "Rsync deployment failed!" >&2
    exit 1
fi
How it works: This script uses `rsync` to efficiently synchronize local files (e.g., a static site's `dist` folder) with a remote server. The `-avz` flags enable archive mode (preserves permissions, timestamps, etc.), verbose output, and compression. `--delete` removes files on the remote that are no longer present locally, ensuring a clean sync. It's ideal for CI/CD pipelines.

Need help integrating this into your project?

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

Hire DigitalCodeLabs