Skip to content

Docker image to automate the wordpress search and replace of URL in wordpress database dump

Notifications You must be signed in to change notification settings

Kedu-SCCL/docker-wordpress-search-and-replace

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

35 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

📖 Table of Contents

Instructions

  1. Place the database dump in the 'db' directory and name as 'wordpress.sql', or whatever variable 'DB_FILENAME' is set to. Example:
db/wordpress.sql
  1. Choose if you want to use the sample 'search-and-replace.sh' script for a very basic usage or follow manual instructions for more detailed steps.

Using 'search-and-replace.sh' script

  1. Execute 'search-and-replace.sh' script passing as a parameters origin and destination URLs. Example:
./search-and-replace.sh http://localhost:8000 http://example.com

There's an optional third parameter in case that you want to increase the PHP setting 'memory_limit', 128 MB by default:

./search-and-replace.sh http://localhost:8000 http://example.com 1024m
  1. Expected a file 'out.sql' (or whatever 'OUTPUT_FILE' variable is set to)

Manual

  1. (Optional) In case that is not the first time that you use the script, just in case bring down the environment
docker-compose down
  1. Start up the environment
docker-compose up -d --build --force-recreate
  1. Wait until db is ready
watch -n 5 "docker exec -i db mysqladmin ping -u wordpress -pwordpress"

Usualy it takes less than 1 minute, wait until you see below line:

mysqld is alive

Then press CTRL + C

  1. Perform the replace. In this example we will replace old URL 'http://localhost:8000' by the new one 'http://example.com':
docker exec -ti app php /wordpress-search-and-replace/srdb.cli.php -h db -n wordpress -u wordpress -p wordpress -s "http://localhost:8000" -r "http://example.com"

Expected output similar to:

[09-Jul-2020 09:06:33 Europe/London] PHP Warning:  "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /wordpress-search-and-replace/srdb.class.php on line 974
 wp_woocommerce_shipping_zone_locations: 0 rows, 0 changes found, 0 updates                     wp_woocommerce_downloadable_product_permissions: 0 rows, 0 changes found, 0 updates           
Replacing http://localhost:8000 with http://example.com 
on 46 tables with 6661 rows 

85 changes were made 
75 updates
Execution Time: 0 mins 0 secs
  1. Test it

We would check if 'siteurl' variable has been replaced:

docker exec db mysql -u wordpress -pwordpress -e "use wordpress; select option_value from wp_options where option_name='siteurl';"

Expected output similar to:

http://example.com
  1. Dump the database content with changes applied
docker exec -i db mysqldump wordpress -u wordpress -pwordpress > out.sql

A file called out.sql should be created, containing the database content with the changes applied.

  1. Cleanup
docker-compose down

Troubleshooting

Error launching search and replace script

If you found an error similar to:

wp_woocommerce_shipping_zone_locations: 0 rows, 0 changes found, 0 updates                     wp_woocommerce_downloadable_product_permissions: 0 rows, 0 changes found, 0 updates           
Replacing http://handytec.es/v1 with localhost 
on 89 tables with 49861 rows 

0 changes were made 
0 updates

results: The table "wp_icl_locale_map" has no primary key. 
Changes will have to be made manually.

Execution Time: 0 mins 1 secs

You should ignore table "wp_icl_locale_map". Currently the only chance is specifying as "-t" parameter the whole list of tables excluding the offending one, "wp_icl_locale_map" in this case

db: Connection refused

If you found an error similar to:

[09-Jul-2020 09:24:05 Europe/London] PHP Warning:  "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /wordpress-search-and-replace/srdb.class.php on line 974
mysqli_connect(): (HY000/2002): Connection refused

db: Connection refused

Execution Time: 0 mins 0 secs

Just wait some seconds before re-launching the command, since the database is not yet ready.

Allowed memory size of 134217728 bytes exhausted

If the script:

./search-and-replace.sh http://localhost:8000 http://example.com

Failed with an error similar to:

[14-Sep-2021 13:34:19 Europe/London] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 20480 bytes) in /wordpress-search-and-replace/srdb.class.php on line 1272
/usr/local/etc/php/php.ini

We need to increase PHP setting 'memory_limit' over its default value, 128 MB.

Before doing so we need to clean up the docker containers (app and db) which are currently running.

After that:

./search-and-replace.sh http://localhost:8000 http://example.com 1024m

About

Docker image to automate the wordpress search and replace of URL in wordpress database dump

Resources

Stars

Watchers

Forks

Packages

No packages published