BASH

Automate Remote File Deployment with Rsync

Efficiently synchronize local files to a remote server over SSH. This snippet uses rsync for fast, reliable web deployment and secure, incremental backups.

#!/bin/bash

LOCAL_PATH="./dist/"
REMOTE_USER="user"
REMOTE_HOST="your_server.com"
REMOTE_PATH="/var/www/html/"

rsync -avz --delete -e "ssh" "${LOCAL_PATH}" "${REMOTE_USER}@${REMOTE_HOST}:${REMOTE_PATH}"

if [ $? -eq 0 ]; then
  echo "Deployment successful!"
else
  echo "Deployment failed."
fi
How it works: This script uses `rsync` to efficiently transfer and synchronize files from a local directory to a remote server over SSH. The `-avz` flags enable archive mode, verbose output, and compression. `--delete` removes extraneous files from the destination, making it ideal for deployments. It includes basic error checking.

Need help integrating this into your project?

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

Hire DigitalCodeLabs