Migrate WordPress blog to another Lightsail instance

A few notes on how I semi-automated the migration of this blog to another Lightsail instance.

Setup

This guide assumes a couple of things:

  • You can SSH into your Lightsail instances
  • You own and have access to a domain name
  • The Linux distribution is Ubuntu.
  • You have an AWS account, a Wasabi account, or both

To use the following scripts, you’ll need to install the AWS CLI and configure credentials.

$ sudo apt install awscli
# Wasabi storage
$ aws configure --profile wasabi
# S3
$ aws configure --profile aws

Back up

A rudimentary way of backing up the database and the WordPress files in the source instance. You can also run the script on the instance periodically using cron.

#!/bin/bash

# Back up database
DB_FILE="blog-$(date --date="0 days ago" +%Y-%m-%d).sql"
sudo mysqldump -u root --password=REDACTED blog > $DB_FILE
aws s3 --profile wasabi --endpoint-url=https://s3.us-west-1.wasabisys.com cp $DB_FILE s3://mybucket/database-backups/
aws s3 --profile aws cp $DB_FILE s3://mybucket/database-backups/
rm -v $DB_FILE

# Delete backups older than 30 days
OLD_DB_FILE="blog-$(date --date="30 days ago" +%Y-%m-%d).sql"
aws s3 --profile wasabi --endpoint-url=https://s3.us-west-1.wasabisys.com rm s3://mybucket/database-backups/$OLD_DB_FILE
aws s3 --profile aws rm s3://mybucket/database-backups/$OLD_DB_FILE


# Back up wordpress files
sudo rm -rf wordpress/
mkdir wordpress/
sudo cp -r /var/www/html/* wordpress/

WORDPRESS_FILE="wordpress-$(date --date="0 days ago" +%Y-%m-%d).tar.gz"
tar cvf $WORDPRESS_FILE wordpress/
aws s3 --profile wasabi --endpoint-url=https://s3.us-west-1.wasabisys.com cp $WORDPRESS_FILE s3://mybucket/wordpress-backups/
aws s3 --profile aws cp $WORDPRESS_FILE s3://mybucket/wordpress-backups/
rm -v $WORDPRESS_FILE
sudo rm -rf wordpress/

# Delete backups older than 30 days
OLD_WORDPRESS_FILE="wordpress-$(date --date="30 days ago" +%Y-%m-%d).tar.gz"
aws s3 --profile wasabi --endpoint-url=https://s3.us-west-1.wasabisys.com rm s3://mybucket/wordpress-backups/$OLD_WORDPRESS_FILE
aws s3 --profile aws rm s3://mybucket/wordpress-backups/$OLD_WORDPRESS_FILE

Set up LAMP server on the new instance

sudo apt install mysql-server
sudo apt install php
sudo apt install apache2
sudo apt install php-mysql

NOTE: If MySQL server fails to start with error mysql.service: Failed with result 'oom-kill', it means that your server needs more memory.

Create MySQL user

Newer versions of Ubuntu require sudo to connect to MySQL as the root user. To keep things simple, create a new user.

$ sudo mysql -u root -p
mysql> CREATE USER 'wordpress'@'localhost' IDENTIFIED WITH mysql_native_password BY '<your password goes here>';
mysql> GRANT ALL PRIVILEGEs ON blog.* TO 'wordpress'@'localhost' WITH GRANT OPTION;
mysql> FLUSH PRIVILEGES;

Restore

#!/bin/bash

# Download the latest database backup
DB_FILE="blog-$(date --date="0 days ago" +%Y-%m-%d).sql"
aws s3 --profile wasabi --endpoint-url=https://s3.us-west-1.wasabisys.com cp s3://mybucket/database-backups/$DB_FILE .
sed -i -e "1i DROP DATABASE IF EXISTS blog;\nCREATE DATABASE blog;\nUSE blog;\n\n" $DB_FILE
mysql -u wordpress --password="<your password goes here>" < $DB_FILE


# Download the latest wordpress backup
WORDPRESS_FILE="wordpress-$(date --date="0 days ago" +%Y-%m-%d).tar.gz"
aws s3 --profile wasabi --endpoint-url=https://s3.us-west-1.wasabisys.com cp s3://mybucket/wordpress-backups/$WORDPRESS_FILE .
tar xvf $WORDPRESS_FILE
sudo cp -r wordpress/* /var/www/html/

Set the right permissions

The wp-content/ directory should be owned by the www-data user.

sudo chown -R www-data wp-content/

Install TLS certificate (letsencrypt)

I followed the Certbot Instructions:

sudo snap install --classic certbot
sudo ln -s /snap/bin/certbot /usr/bin/certbot
sudo certbot --apache

Open ports

Don’t forget to open the right ports (80 and 443) on your Lightsail instance.

Update your domain’s A record

If you’re not using static IPs, you’ll need to update your domain’s A record. Go to your domain name registrar and update the A record with the IP of the new instance.

Done

All right, if everything went right, the blog should be running on the new instance.


Posted

in

,

by

Tags: