Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kalebheitzman/f80cb44e0e7ccc04f8b8ad289c8535a6 to your computer and use it in GitHub Desktop.
Save kalebheitzman/f80cb44e0e7ccc04f8b8ad289c8535a6 to your computer and use it in GitHub Desktop.
sync-wordpress.sh
#!/bin/bash
# Configurable variables
REMOTE_SSH="[email protected]"
REMOTE_PATH="/var/www/yourproductiondir"
LOCAL_PATH="/Users/youruser/Sites/yourlocaldir"
REMOTE_URL="https://yourproductiondomain.com"
LOCAL_URL="http://yourlocaldev.local"
# Step 1: Sync files from production (wp-content only, for faster sync)
echo "🟑 Syncing wp-content via rsync..."
rsync -avz --info=progress2 \
"$REMOTE_SSH:$REMOTE_PATH/wp-content/" \
"$LOCAL_PATH/wp-content/"
# Step 2: Export database from production via SSH
echo "🟑 Exporting database from remote..."
ssh $REMOTE_SSH "cd $REMOTE_PATH && wp db export /tmp/prod.sql --quiet && echo 'βœ… DB exported to /tmp/prod.sql'"
# Step 3: Download the exported DB
echo "🟑 Downloading database file..."
scp "$REMOTE_SSH:/tmp/prod.sql" ./prod.sql
# Step 4: Import DB locally
echo "🟑 Importing DB locally..."
cd "$LOCAL_PATH"
wp db import ../../prod.sql
wp search-replace "$REMOTE_URL" "$LOCAL_URL" --skip-columns=guid
# Step 5: Cleanup
echo "🧹 Cleaning up..."
ssh $REMOTE_SSH "rm /tmp/prod.sql"
rm ../../prod.sql
echo "βœ… Sync complete!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment