BASH
Automate Secure Website File Synchronization with rsync
Efficiently synchronize local website files to a remote server over SSH using rsync, ensuring data integrity and minimizing transfer time for web deployments.
#!/bin/bash
LOCAL_PATH="/path/to/your/local/website"
REMOTE_USER="your_ssh_user"
REMOTE_HOST="your_remote_host.com"
REMOTE_PATH="/var/www/html"
echo "Synchronizing website files from $LOCAL_PATH to $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"
rsync -avz --delete -e "ssh" "$LOCAL_PATH/" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH/"
if [ $? -eq 0 ]; then
echo "Synchronization successful!"
else
echo "Error during synchronization. Check logs."
fi
How it works: This script leverages `rsync` to mirror your local website directory to a remote server, ensuring only changed files are transferred. The `-avz` flags provide archive mode (recursive, preserves permissions, etc.), verbose output, and compression. `--delete` removes files on the remote that no longer exist locally, making it a true mirror. It uses SSH for secure transfer.