Skip to content

Instantly share code, notes, and snippets.

@retlehs
Last active January 26, 2022 03:48
Show Gist options
  • Save retlehs/aa47003249623e52b95a3fcd6f69b8fb to your computer and use it in GitHub Desktop.
Save retlehs/aa47003249623e52b95a3fcd6f69b8fb to your computer and use it in GitHub Desktop.
WP-CLI aliases sync example
read -r -p "Would you really like to reset your development database and pull the latest from production? [y/N] " response
if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
wp @development db reset --yes &&
wp @production db export - > sql-dump-production.sql &&
wp @development db import sql-dump-production.sql &&
wp @development search-replace https://example.com https://example.dev
fi
path: web/wp
@production:
ssh: [email protected]/srv/www/example.com/current
@development:
ssh: [email protected]/srv/www/example.com/current
@gitowiec
Copy link

gitowiec commented Apr 13, 2017

I don't know why I get Error: Import file missing: sql-dump-production.sql... It seems the SQL file does not get into @development server for me :/ If I run wp @development db import sql-dump-production.sql --debug I get:


Debug (bootstrap): No readable global config found (0.021s)
Debug (bootstrap): Using project config: /workspace/strefatradera_proj/public_html/wp-cli.yml (0.023s)
Debug (bootstrap): No package autoload found to load. (0.112s)
Debug (bootstrap): Required file from config: /workspace/strefatradera_proj/public_html/config/wp-cli/quiet.php (0.112s)
Debug (bootstrap): SSH host: [email protected] (0.112s)
Debug (bootstrap): SSH port:  (0.112s)
Debug (bootstrap): SSH path: /home/login/webapps/strefatradera_staging (0.112s)
Debug (bootstrap): Running SSH command: ssh -q [email protected] -t cd /home/login/webapps/strefatradera_staging; wp 'db' 'import' './sql-dump-production.sql' '--debug' (0.112s)
Debug (bootstrap): No readable global config found (0.014s)
Debug (bootstrap): Using project config: /home/login/webapps/strefatradera_staging/wp-cli.yml (0.015s)
Debug (bootstrap): No package autoload found to load. (0.169s)
Debug (bootstrap): Required file from config: /home/login/webapps/strefatradera_staging/config/wp-cli/quiet.php (0.169s)
Debug (bootstrap): ABSPATH defined: /home/login/webapps/strefatradera_staging/ (0.169s)
Debug (bootstrap): Running command: db import (0.169s)
Error: Import file missing: ./sql-dump-production.sql

How to debug it more?

I use newest Wordpress and wp-cli

@gitowiec
Copy link

Ok I'm done ! :D it is great script! Here's my version

#!/usr/bin/env bash
read -r -p "Would you really like to reset your development database and pull the latest from production? [y/N] " response

if [[ "$response" =~ ^([yY][eE][sS]|[yY])$ ]]; then
    wp @staging db reset --yes &&
    wp db export sql-dump-dev.sql --add-drop-table &&
    wp @staging db import - < sql-dump-dev.sql &&
    wp @staging search-replace http://strefatradera.l http://strefatradera.ufff.pl
fi

@ttcremers
Copy link

I've always been a bit fuzzy on this, why use the search-replace on the sites url when it's much easier to just set WP_HOME and WP_SITEURL in the wp config

@taylorgorman
Copy link

I would guess because the WordPress editor inserts the site URL when adding internal links and img tags. So you have to change those strings in the post_content field for every post.

Those URLs also exist in the wp_options table, which is from where all the WordPress core functions get them (I'm mostly sure). You could use the wp-config constants in your own themes and plugins, but other plugin devs will still use them. ..Although I suppose you could hook into those functions and switch them to pull from the constants. But you'd still have the post_content problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment