Skip to content

Instantly share code, notes, and snippets.

@podlom
Created November 22, 2023 13:54
Show Gist options
  • Select an option

  • Save podlom/9cc2839b96e3e0919860e7158cd74edb to your computer and use it in GitHub Desktop.

Select an option

Save podlom/9cc2839b96e3e0919860e7158cd74edb to your computer and use it in GitHub Desktop.

Revisions

  1. podlom created this gist Nov 22, 2023.
    47 changes: 47 additions & 0 deletions _sync_uploads_from_stage_to_prod.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    #!/bin/bash

    # Define server addresses and directories
    STAGING_SERVER="user@staging-server-address"
    PROD_SERVER="user@production-server-address"
    REMOTE_DIR="/path/to/staging/dir/"
    LOCAL_DIR="/path/to/local/dir/"
    PROD_DIR="/path/to/production/dir/"


    echo "Sync WP uploads has started at:"
    date

    # Step 1. Check if the directory exists
    if [ -d "${LOCAL_DIR}" ]; then
    printf "Local directory exists ${LOCAL_DIR}.\n"
    ls -alh "${DB_BACKUP_PATH}"
    else
    # Make the local directory if it does not exists
    echo "Local directory does not exists. Make directory ${LOCAL_DIR}..."
    mkdir -pv "${LOCAL_DIR}"
    fi

    # Step 2: Sync from Staging to Local
    echo "Syncing from Staging Server to Local..."
    rsync -avz --progress $STAGING_SERVER:$REMOTE_DIR $LOCAL_DIR
    date

    # Check if rsync was successful
    if [ $? -ne 0 ]; then
    echo "Failed to sync from Staging Server."
    exit 1
    fi

    # Step 3: Sync from Local to Production
    echo "Syncing from Local to Production Server..."
    rsync -avz --progress $LOCAL_DIR $PROD_SERVER:$PROD_DIR
    date

    # Check if rsync was successful
    if [ $? -ne 0 ]; then
    echo "Failed to sync to Production Server."
    exit 1
    fi

    echo "Sync WP uploads has finished at:"
    date