Skip to content

Instantly share code, notes, and snippets.

@podlom
Last active November 22, 2023 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save podlom/4183cdc6de44e1f947c1243ade20009b to your computer and use it in GitHub Desktop.
Save podlom/4183cdc6de44e1f947c1243ade20009b to your computer and use it in GitHub Desktop.
Sync WP uploads from prod to local and then to staging server
#!/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 Prod to Local
echo "Syncing from Prod Server to Local..."
rsync -avz --progress $PROD_SERVER:$PROD_DIR $LOCAL_DIR
date
# Check if rsync was successful
if [ $? -ne 0 ]; then
echo "Failed to sync from Prod Server."
exit 1
fi
# Step 3: Sync from Local to Staging
echo "Syncing from Local to Staging Server..."
rsync -avz --progress $LOCAL_DIR $STAGING_SERVER:$REMOTE_DIR
date
# Check if rsync was successful
if [ $? -ne 0 ]; then
echo "Failed to sync to Staging Server."
exit 1
fi
echo "Sync WP uploads has finished at:"
date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment