Backup

Backup WordPress on Amazon Lightsail from the Command Line

WordPress, Amazon Lightsail, Amazon S3 and AWS CLI logos

While there are many plugins out there to help you backup your WordPress site, they often rely on the wp-cron system, contain advertisments, and sometimes even cost money. I recently set out to do this from the command line without using a plugin and this is that story.

Background

I have a bunch of WordPress sites running on individual VPS’s in the cloud. I used to launch an instance and install everything myself but have mostly transitioned over to using prebuilt stacks by Bitnami — they come with everything installed so it takes seconds to launch a new site. If you haven’t used Bitnami for WordPress before you can check that out here. I use Amazon Web Services (AWS) as my cloud provider, and have instances running on both EC2 and Lightsail, which is what I’ll be using here, though this should work from any debian based linux server.

Step 1 – Backup WordPress

  1. SSH into your server with the default bitnami user

    ssh -i ~/path/to/key bitnami@example.com
  2. Stop all services

    sudo /opt/bitnami/ctlscript.sh stop
  3. Create full application backup

    sudo tar -pczvf /home/bitnami/backup/wordpress/full-application-backup-$(date +"%F").tgz /opt/bitnami

    Note: You 1st need to create a directory for your backups; I’ve used /home/bitnami/backup/wordpress (you can do mkdir -p /path/to/backup/dir).

  4. Start all services again so your site is back online

    sudo /opt/bitnami/ctlscript.sh start
  5. Change ownership of backup to bitnami user

    sudo chown -R bitnami:bitnami /home/bitnami/backup

    And that’s it! You now have a complete backup of your wordpress site, including database. Because we chose to stop the services the backup was a single command. This is great for many small websites that it’s not a problem to do this late at night. If your site needs to stay up all the time this might not be for you and you’ll want to learn how to backup wordpress without stopping services, which there are many guides available.

Step 2 — Copy to S3 bucket

Now there is one additional step I like to take, which is to move the backup file to safe storage, which for me is an S3 bucket. Let’s do that next.

  1. Install AWS CLI

    sudo apt install aws-cli
  2. Configure for use

    aws configure

    See Amazon’s documentation if you need help with this.

  3. Copy to s3

    aws s3 cp /home/bitnami/backup/wordpress/full-application-backup*.tgz s3://my-bucket

Step 3 — Put all of this into a script to automate

Next time…

Your next project starts here

Let’s build something that works.

A new building, a camera system for the back forty, or a network that needs a fresh start. Tell us what you’re working with — the first conversation is free.

Or email hello@emeraldsecurity.net