Backup & Move WordPress Instance to Oracle Cloud Infrastructure

Oracle offer a limited set of free cloud hosting which is ideal for hosting personal blogs and other websites. The system takes some getting used to, and there are two layers of security systems: the operating system, and the virtual network which the OCI instances connect through. I’ll cover this latter but right now want to document the moving of WordPress.

Backup the Working WordPress Instance

This site lived on a Debian GNU/Linux instance hosted on Linode. After manually upgrading the Debian GNU/Linux release I’ve been having intermittent booting issues making the WordPress host unreliable; this is probably why Linode does not recommend doing release upgrades and suggests moving data to a new instance instead. I have chosen to save some dollars and the accompanying foreign currency conversion fees by moving to OCI.

  1. Connect with SSH to the current virtual machine
  2. Become the root user if you are not already: su -
  3. Stop the web service to ensure a clean site backup, making sure that there are no users working with WordPress: service apache2 stop
  4. Backup the WordPress files which are likely located in /var/www, in this example the command is:
    1. cd /var/www
    2. Stop the database server: service mariadb stop
    3. tar -cvzf
    4. mv /home/andrewread/
    5. cp /etc/apache2/sites-available/ /home/andrewread/
    6. cd /home/andrewread
    7. Remove three lines from the .conf file which begin: “Rewrite”, since they are all related to certbot and will need to be regenerated later.
    8. The database server must be started before the WordPress database can be “dumped”: service mariadb start
    9. Now we need to backup the WordPress database and that can be done on a Debian GNU/Linux system like: mysqldump --add-drop-table -h localhost wordpress > database.bak
    10. chown andrewread database.bak

Setting up the New Host System

It’s now time to create a new web host instance on the desired cloud provider, or maybe even your very own server. I’ve chosen Oracle Cloud Infrastructure purely based on the cost: free! If this wasn’t the case I’d host from my own server behind my broadband connection which would easily cope with the traffic that this little site gets.

I have setup an Ubuntu 20.04 (LTS) instance which is one of the platform provided images from Oracle. I recommend Debian GNU/Linux, Ubuntu or similarly available systems for web hosting; they are just so much easier to manage than the likes of Oracle, CentOS, or other Red Hat Enterprise Linux derivatives.

To get this server going I needed to do the following:

  1. Connect to the system with the SSH Key that OCI generated for me; there is no user account password! On my Mac I open the connection with: ssh -i keyfilename.key ubuntu@thepublicipofyourinstance
  2. Update the system:
    1. sudo apt update
    2. sudo apt dist-upgrade
  3. Install mariadb: sudo apt install mariadb-server
  4. Secure the mariadb installation with: sudo mysql_secure_installation #entering a root password then selecting all defaults
  5. Install apache2 and WordPress dependencies with: apt install wordpress curl apache2 #although we are not using the packaged WordPress installing it conveniently allows apt to calculate dependencies that are needed for WordPress to work
  6. Install Certbot for free SSL certificate generation:
    1. sudo snap install core; sudo snap refresh core
    2. sudo snap install --classic certbot
  7. In OCI the Ubuntu 20.04 image has iptables firewall running so we need to enable traffic to reach the hosted sites with:
    1. sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 80 -j ACCEPT
    2. sudo iptables -I INPUT 6 -m state --state NEW -p tcp --dport 443 -j ACCEPT
    3. sudo netfilter-persistent save

Instating Site and Database

Now we copy the backup data from the old host which should still be running, then enable it all.

  1. From the new host use scp to copy the three backup files for example: scp #then for the tar archive and the conf file
  2. sudo mv /etc/apache2/sites-available/
  3. sudo mv /var/www/
  4. Recreate the database with the same username and password as on the previous host system:
mysql -u adminusername -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 5340 to server version: 3.23.54

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> CREATE DATABASE wordpress;
Query OK, 1 row affected (0.00 sec)

mysql> GRANT ALL PRIVILEGES ON databasename.* TO "wordpress"@"localhost"
-> IDENTIFIED BY "wordpressdbpassword";
Query OK, 0 rows affected (0.00 sec)

Query OK, 0 rows affected (0.01 sec) 

mysql> EXIT
  1. The new database can now be populated with your WordPress sites data from the backup file with: mysql -u wordpress -p wordpress < database.bak #enter the wordpress database password when prompted
  2. Now we need to unpack the WordPress files into the location they are to be served from which must be the same as on the previous host. I did the following:
    1. cd /var/www
    2. sudo tar -xvzf
  3. To enable the WordPress site we need to enable it from the .conf file stored in /etc/apache2/sites-available by: sudo a2ensite
  4. Reload the web server with: sudo service apache2 reload
  5. Now is the time to change your DNS records on the internet to point to the new web host, and verify that the new address is resolved.
  6. I was unable to test the non SSL secured site because the version of Safari I browse with defaults to https, so now it’s time to get that working: sudo certbot #follow the prompts and make the site SSL
  7. Try accessing your WordPress site over the internet, make sure that images load, and that you ca save edits

Job Done!

If that’s all working you can get rid of your previous host, and possibly start saving money! I recommend that you regularly backup your WordPress site in a similar fashion to the first stage of this tutorial. You could even automate periodic backups with a scripted cron job.

Happy WordPress Hosting!