So, its been a struggle but got it sorted now. I can port from one address to another. Within the docker containers under my charge anyway. Some pointers I found useful
- Use a separate container for each DB unless you want to increase complexity. Although now I have the process sorted out, it should be fine even with multiple databases in the mysql instance
- Get your wordpress / database environment up and running before “overwriting” it. Allows you to check everything is working first.
- Keep the default wordpress table prefix’s. Otherwise it will require further search/replace actions.
So. My starting point.
I created a new wordpress instance within my docker compose setup. Set it up under Caddy so it gets https. My website is technically two containers. One is WordPress:apache. The other is MySQL. These work in cooperation along with Caddy (Reverse proxy).
I have one additional container I am using for maintenance at the moment. I am running an instance of Myphpadmin for visual inspection of databases.
Step 1. Copying all the website data.
#Login to the remote server
ssh name@server
#Check the name of the running #containers
docker ps
#login to the relevant container
docker exec -it wp-server bash
You are now logged into the wordpress server in a bash terminal. By default, it pops you into
/var/www/html/
We need a copy of the html folder and all subfolders. One issue is that this path also included the wordpress and database backups. These can be quite huge. It helps to exclude these backups.
cd ..
#Lets make a copy of the site data. Lets also exclude the
#backups folder and all sub folders.
tar --exclude="*/backups/*" -czvf site.tar.gz html
#Just for reference, how big is it?
du -h site.tar.gz
#Confirm path
pwd
exit
#We are now back in the hosting server
#Copy the file from the docker webserver to the hosting server
docker cp old-wp-server:/var/www/site.tar.gz .
Step 2. Copying the database data.
Next, we need to create a copy of the database itself. The database does contain some references regarding the address of the website. These will need handled separately.
First, lets get this copied.
There are a couple of options here. I had access to a PHPmyadmin instance to do things visually. Probably overkill though.
The default MySQL installation has a useful command line utility which can be used in this situation. Especially as it can be done over a ssh session with the server.
#Assuming we are still logged into a ssh session running on the hosting server
#The following command may take a short while to run,
#depending on the size of the DB
mysqldump -u root -p database_name > /backup.sql
#Again, as this is moving to another container, it can be pulled back to the hosting server
docker cp old-SQL-container:/backup.sql .
Step 3. Search/Replace website references.
At this stage, the database dump file still refers to the old website and if uploaded will cause problems. We need to replace all occurrences to the new (or target) website address. This can be achieved using the sed command
#Replacing the references to the old website
#with the address of the target website
sed -i 's/oldwebsite/newwebsite/g' backup.sql
Step 4. Unpacking the website data.
We are now ready to get the website data from the old site, to the new one, Again, this is a container based movement.
#Lets also get the wordpress copy into the container
docker cp ./site.tar.gz new-wp-server:.
#lets unpack the website data first
docker exec -it new-wp-server bash
#unpack the data into the www folder
mv ./site.tar.gz /var/www
cd /var/www
tar -xzvf site.tar.gz .
exit
#back in the remove server
Step 5. Preparing the new database.
To deal with the database, i found it easier to dump the original and recreate a blank one ready for import
#first lets move the database file
docker cp backup.sql new-SQL-container:.
#Now lets process it
docker exec -it new-SQL-container bash
mysql -u root -p
#enter password
DROP wordpress;
CREATE wordpress;
exit
#Now import the database info
mysql -uroot -p < backup.sql
exit
Step 6. Try it out.
At this point we need to test it out. use the apporpriate command for getting things up and running. In my case, I was using docker compose.
docker compose --profile appropriate-profile up -d
In the event things dont work properly, I can leave out the -d switch to view the messages during the startup of the containers for clues.
