BASH

Securely Deploy Website Files with Rsync over SSH

Automate secure deployment of local web files to a remote server using Rsync over SSH, ensuring only changed files are transferred efficiently, saving time and bandwidth.

#!/bin/bash
# Configuration
LOCAL_PATH="/path/to/your/local/website"  # Local project directory
REMOTE_USER="your_ssh_user"              # SSH username on remote server
REMOTE_HOST="your_remote_server_ip_or_domain" # Remote server IP or domain
REMOTE_PATH="/var/www/html/yourwebsite" # Destination path on remote server
SSH_KEY="/home/youruser/.ssh/id_rsa"     # Path to your SSH private key (optional)

echo "Starting deployment to $REMOTE_HOST:$REMOTE_PATH"

# Use rsync for efficient file transfer
# -avz: archive mode, verbose, compress
# --delete: delete extraneous files from dest (not on local)
# -e "ssh -i $SSH_KEY": specify SSH command with key
rsync -avz --delete -e "ssh -i $SSH_KEY" "$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 facilitates secure and efficient website deployment using `rsync` over `SSH`. The `-avz` options enable archive mode (preserving permissions, timestamps, etc.), verbose output, and compression during transfer. `--delete` ensures that files removed locally are also removed from the remote server. The `-e "ssh -i $SSH_KEY"` part specifies the SSH command, including an optional private key for authentication, making it suitable for automated deployments without manual password entry.

Need help integrating this into your project?

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

Hire DigitalCodeLabs