Working on a WordPress project I always develop locally. For many projects, there is also a staging server. So migrating WordPress installations is a very frequent task for me. Therefore a good process is key.
Easy migrations without plugins
When I do migrations, I usually always use the WP-CLI. The only requirement for that is an SSH access to the the destination server. My migration process is done with these few steps:
1. Dump the source database
A WordPress installation has two parts, the database and the file system. To dump the database, all you need to do is using the following command in the WordPress document root folder:
wp db export
Success: Exported to 'wp_project-staging-2020-03-29-a2aa75c.sql'.
The file name starts with the database name, followed by the current date and a random hexadeciml hash. This has will come in handy with the second step.
2. Pack all files on the source
Now we are ready to pack all files on the source. This can easily be done with zip, gzip or any similar tool:
zip -r wp_project-staging-2020-03-29-a2aa75c.zip .
As the file name, we simply use the same as the database dump created, just with .zip
instead of .sql
as the extension. This will “protect” the file name from easily being guessed. Don’t just name it dump.zip
as otherwise anyone could just download that file.
3. Transfer the file to the new server
Now it’s time to transfer the file to the new server. This can be done in various ways. A very easy one is using curl, wget or similar:
wget https://staging.example.com/wp_project-staging-2020-03-29-a2aa75c.zip
Transfering from a local system you probaly have to use FTP, as you can’t curl/wget from a local domain.
4. Unpack the file on the destination
The next step is obvious. Once all files are transfered, you have to unpack the files. When you have used a zip files, simply run:
unzip wp_project-staging-2020-03-29-a2aa75c.zip
When you have compressed the files in step 2 in the document root, the files should be unpacked in the document root as well and not be in a subfolder.
5. Update the configuration
Before we can use the project on the new project, we have to update the wp-config.php
files. Probably only the values prefixed with DB_
.
6. Import the database
Now we can import the database. If the database on the new server is not yet, created, either do it with the management tool of your hoster, or simply by using the WP-CLI as well (which only works, if the database user has the privileges to do this):
wp db create
Once the database is created, you can import is as simply as exporting it in step 1, with the corresponding command:
wp db import wp_project-staging-2020-03-29-a2aa75c.sql
7. Update the domain in the database
As you probably know, in a WordPress installations, all domain paths are stored as absolute paths in the database. This is why you have to update the paths. You can use a plugins for that or also use the WP-CLI with this command:
wp search-replace "https://staging.example.com" "https://example.com"
This is the most basic replacement you have to do. In some cases, you also have to do some other replacements. I will cover that one in a future blog post.
Conclusion
Migrating a WordPress project should be as easy as fast as possible, if you do it on a regular basis. My approach only takes two commands on the source and 4-5 command on the destination. The parts that usually takes the most times is the packing, transfering and unpacking, which will takes longer, the larger a project’s size is. For a small projects all of that might only take less then 10 seconds each.
I hope with this small guide, you next migrations is done as easily and fast as for me. If you have a different approach, that might be handy, please leave a comment below explaining it.
Try rsync instead of wget/curl for local machines. Bonus: if you don’t zip you can do increments in case the connection drops.
Indeed, rsync is a great altenative, if available. But using it is not a simple as zip/unzip and a download. For a one-time-migration, this usually is enough.
[…] the last blog post I explained how I move a WordPress website to another server. In step 7 I told you, that you have t replace the domain in the database. But this is not the only […]