July 30, 2020

Migrate WP with command line

Recently I had to “migrate” my wordpress web page and after believing in the magic of plugins which do everything automagically, I have been confronted with the truth: It is all a big, ugly lie. It either does not work, or there is a bug, or you have to make a backup of your web page in a strange format, download it and upload it, or you have to pay in the critical moment for a special “feature”, after one or two hours of fight.

This is the price to pay for not doing the things in The Only Way: The command line.

In the old server

Dump your database

$ mysqldump -u root -p wordpress --add-drop-table > backup.sql

-p means you will prompted for the password
-u root because, for some reason, mysqldump did not accept the database buser I had defined in wp-config.php.
“wordpress” was the name of the database

You will find the information about the name of the database, the database user, password and hostname in the first lines of

wp-config.php

This is, by far, the best way to dump your database. Forget about phpmyadmin and such things.

After that, simply make a tar.gz of all your wp site.

$ tar cvfz web.tar.gz web/

This two files (backup.sql and web.tar.gz) are a full backup of your web site.

In the new server

I am paying for a webhosting thing which installs a wp template. Allow the thing to do its thing. Require ssh access. This is key.

Log in on to your new virtual machine with ssh, and first create a database. Probably the web hosting will have created automatically a database. To find out the name of the database, either check that phpmyadmin or simply check the file wp-config.php.

Overwrite the database with the old one, but use the new information about database name, database user and hostname:

$ mysql -h "fg345745435.hosting-data.ie" -u dbusername8762345 dbt43567653 -p < backup.sql

where, of course, the hostname after -h is made up, as well as the username, the value after -u and, for some reason, I had to add the name of the database (dbt43567653) before the -p flag for the password.

After this is done, simple rewrite ALL of the wp template with the old contents.

$ mv web/* mynewsite/

Fix wp-config.php

The last step is to fix the data in wp-config.php

You will have to add the right information in the fields


define('DB_NAME', 'wordpress');

/** MySQL database username */
define('DB_USER', 'wordpressuser');

etc. As you can guess, wordpress is, in our case, dbt43567653, and wordpressuser dbusername8762345. This information was included in the file wp-config.php before we overwrote it.

Fix the DNS

Again, in the file wp-config.php you will have to tell wordpress what URL in the DNS are pointing to your new webpage, which will have a random name, such as 2345686573453.online-pages.ie or such. In my case,


define('WP_SITEURL', 'http://astro-gr.org');
define('WP_HOME', 'http://astro-gr.org');

After that, you will have to tell your DNS provider, where you bought the domain, that the new URL to which your domain must point to is 5686573453.online-pages.ie.

The changes will show up first in the US, then slowly progress towards Europe etc. It can take some minutes, up to one hour or so.