rsync with python without extra files

Your goal is to send your python project to your server from your local machine.

Your python project includes a ton of files you wouldn’t want to send to a server so a simple rsync is not enough.

  • Definitely not: *pyc files, __pycache__, DS_Store, venv or env folders
  • Maybe not: secret development .env, any logs, sqlite3 file probably

Option 1: rsync with a bunch of exclude flags

Bash
rsync -avz \
  --exclude='*.pyc' \
  --exclude='__pycache__/' \
  --exclude='.env*' \
  --exclude='*.log' \
  --exclude='*.sqlite3' \
  --exclude='.git/' \
  --exclude='tests/' \
  --exclude='docs/' \
  --exclude='venv/' \
  --exclude='.DS_Store' \
  --exclude='env/' \
  ~/path/to/source root@<ip_address>:/path/to/destination

Option 2: Shell Function

  1. Add this to ~/.bashrc or ~/.zshrc
  2. Run $ source ~/.bashrc or $ source ~/.zshrc
Bash
rsync_python() {
    local source_path=$1
    local destination_path=$2
    
    # Check if the source path is provided
    if [ -z "$source_path" ]; then
        echo "Error: Source path is not provided."
        echo "Usage: rsync_python [source_path] [destination_path]"
        echo "Example: rsync_python /projamming/kale root@<ip_address>:/home/ubuntu"
        return 1  # Exit the function with an error status
    fi
    
    # Check if the destination path is provided
    if [ -z "$destination_path" ]; then
        echo "Error: Destination path is not provided."
        echo "Usage: rsync_python [source_path] [destination_path]"
        echo "Example: rsync_python /projamming/kale root@<ip_address>:/home/ubuntu"
        return 1  # Exit the function with an error status
    fi

    rsync -avz \
        --exclude='*.pyc' \
        --exclude='__pycache__/' \
        --exclude='.env*' \
        --exclude='*.log' \
        --exclude='*.sqlite3' \
        --exclude='.git/' \
        --exclude='tests/' \
        --exclude='docs/' \
        --exclude='venv/' \
        --exclude='.DS_Store' \
        --exclude='env/' \
        $source_path $destination_path
}

Option 3: Shell script

  1. Save this as rsync_python.sh
  2. Activate executable permissions $ chmod +x rsync_python.sh
Bash
#!/bin/bash

source_path=$1
destination_path=$2

# Check if the source path is provided
if [ -z "$source_path" ]; then
    echo "Error: Source path is not provided."
    echo "Usage: rsync_python [source_path] [destination_path]"
    echo "Example: rsync_python /projamming/kale root@<ip_address>:/home/ubuntu"
    return 1  # Exit the function with an error status
fi

# Check if the destination path is provided
if [ -z "$destination_path" ]; then
    echo "Error: Destination path is not provided."
    echo "Usage: rsync_python [source_path] [destination_path]"
    echo "Example: rsync_python /projamming/kale root@<ip_address>:/home/ubuntu"
    return 1  # Exit the function with an error status
fi

rsync -avz \
    --exclude='*.pyc' \
    --exclude='__pycache__/' \
    --exclude='.env*' \
    --exclude='*.log' \
    --exclude='*.sqlite3' \
    --exclude='.git/' \
    --exclude='tests/' \
    --exclude='docs/' \
    --exclude='venv/' \
    --exclude='.DS_Store' \
    --exclude='env/' \
    $source_path $destination_path