← Back to all snippets
BASH

Automating Remote File Deployment with SCP

Deploy files and directories to a remote web server securely using `scp` within a Bash script, streamlining your deployment workflow for web applications.

#!/bin/bash
LOCAL_PATH="./dist/" # Or path to your build artifacts
REMOTE_USER="webuser"
REMOTE_HOST="your_server_ip_or_domain.com"
REMOTE_PATH="/var/www/html/mywebapp/"

echo "Attempting to deploy files from $LOCAL_PATH to $REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

# Ensure remote directory exists (optional, can be done via SSH)
# ssh "$REMOTE_USER@$REMOTE_HOST" "mkdir -p $REMOTE_PATH"

# Copy files/directories recursively
scp -r "$LOCAL_PATH" "$REMOTE_USER@$REMOTE_HOST:$REMOTE_PATH"

if [ $? -eq 0 ]; then
    echo "Deployment successful."
    # Optional: Run remote commands after deployment (e.g., clear cache, restart service)
    # ssh "$REMOTE_USER@$REMOTE_HOST" "cd $REMOTE_PATH && php artisan cache:clear"
else
    echo "Error: Deployment failed."
    exit 1
fi
How it works: This script automates the deployment of local files or directories to a remote server using `scp` (Secure Copy Protocol). It securely transfers content, making it useful for simple web application deployments or synchronizing configuration files to development or production environments efficiently.

Need help integrating this into your project?

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

Hire DigitalCodeLabs