There are many options for copying a website between one hosting and another, between different servers, creating a local copy, etc.
The methods I use the most, are first of all GridPane if they are websites hosted on my custom hosting service. ManageWP for other websites, Migrate Guru, WPvivid Backup Plugin or sometimes All-in-One WP Migration.
But without a doubt, the method that I use the most and that never fails, whether it is a 50 MB or a 600 GB web, is the manual method and that is what I am going to describe briefly here.
You can export and import a website manually in several ways (FTP, SSH, Hosting Panel, a previous ManageWP backup, etc…). Here I am going to show the method using WP-CLI and SSH.
If your hosting provider does not offer SSH and WP-CLI access, my recommendation is to look for a new provider before reading this article.
Exporting the web
Once we connect to the origin server via SSH and we are located in the root directory of the web, first we will see the data of the web to verify that it is the correct one, for what we execute:
wp config list
And we will see the data of the same:
If we are on the right web page and everything is in order, we will proceed to export the database:
wp db export --add-drop-table
And it will export the database in the following format {dbname}-{Y-m-d}-{random-hash}.sql
Now we will compress the files to send and according to the web we have several options:
- Compress the SQL file and the complete WordPress installation.
- Compress in one file the database (SQL dump) and in another one the complete WordPress installation.
- Include the SQL file in the folder
wp-content
and compress everything in one file. - Compress in a file the DB (SQL) and in another one the folder
wp-content
.
Depending on the strategy used, we will use one or the other and for this we can use tar and gz or use zip.
tar -czvf web_migrada.tar.gz carpeta_sitio_web
zip -r web_migrada.zip carpeta_sitio_web
Now we upload the file or files with the contents and database to the new server, either by FTP, SFTP, SCP or the file manager of the new web.
For SCP it would be:
scp web_migrada.tar.gz usuario@direccion_servidor:/ruta/destino
Importing the web
Now that we have the file (or files) in the new hosting, we have to unzip it and move the folder wp-content
to its corresponding path or the entire installation if we want to copy the complete WordPress (another version, special folders, etc.), for which we unzip depending on whether it is tar.gz
or zip
:
tar -xzf web_migrada.tar.gz
// Si lo queremos descomprimir a una capeta temporal:
// If we want to decompress it to a temporary folder:
tar -xzf web_migrada.tar.gz -C tmp
unzip web_migrada.zip
// Si lo queremos descomprimir a una capeta temporal:
// If we want to decompress it to a temporary folder:
unzip web_migrada.zip -d tmp
Now that we have the files in their original location, let’s go to the database.
Ideally, you should make a copy of the current DB and delete all tables (this command deletes the entire database and recreates it):
wp db reset
And now we import the Database that we have uploaded to the hosting and unzipped in the previous step:
wp db import prueba-2023-09-10-5a4e7a3e9a3c0f8b2d3df5b9d7e8e1.sql
Once the database is imported, we can replace the old URLs with the new ones and we can also check if there are URLs with http instead of https or with www if our domain does not have it or vice versa:
wp search-replace 'https://web_antigua.test' 'https://web_nueva.test'
wp search-replace 'http://web_antigua.test' 'https://web_nueva.test'
wp search-replace 'https://www.web_antigua.test' 'https://web_nueva.test'
wp search-replace 'http://www.web_antigua.test' 'https://web_nueva.test'
We could even have exported from the previous web with the replacement done (wp search-replace 'web_antigua' 'web_nueva' --export=database_new_export.sql
) or we can test the replacements without actually executing them in the DB (wp search-replace 'https://web_antigua.test' 'https://web_nueva.test' --dry-run
).
Now we have to clear the object cache and reload the rewrite rules:
wp cache flush
wp rewrite flush
Conclusions and possible errors
This method is the fastest, most effective and “safe” method. Sure if you know what you are doing, otherwise it can also be the most dangerous and prone to failures, and even delete a complete installation.
You could also have exported WordPress using a WXR file, which would be very useful for exporting content and not carrying accumulated garbage, but that is not the purpose of this post.
If we have problems, the first thing to check is if we have brought plugins from another hosting that are not compatible with this one, must use plugins (folder mu-plugins
), there are fixed paths that do not match those of the new hosting or the configurations of the file wp-config.php
are not correct.
Good migrations ! And remember: never deploy on a Friday and never migrate on a Friday (although I do it just to be contrary).