Backup an Amazon Lightsail WordPress Instance to S3 from the Command Line

This process is specific to WordPress sites that have been installed with the Bitnami for WordPress stack on AWS cloud.

While there are many plugins out there to help you backup your WordPress site, they often rely on the wp-cron system, usually contain advertisements, and sometimes cost money. I recently set out to do this from the command-line, using free tools, and without a plugin and this is that story. It should be noted that this process requires your website to be offline for about 1-2 minutes each backup. I chose this because it is by far the easiest way to create an application consistent backup and saves a significant amount of time off the restore process, should you need to recover your site for whatever reason.

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

If you haven't checked out Amazon's Simple Storage (S3) service, you should – it is an object storage service that offers industry-leading scalability, data availability, security, and performance. And the best part... it is very inexpensive, even for large amounts of data. To complete our last step of moving our backup to safe storage in the cloud, we utilize the command-line utility (aws-cli).

  1. Install AWS CLI with apt package manager
    sudo apt install aws-cli
  1. Configure for use
    aws configure

See Amazon's documentation here If you need help with this.

  1. Copy to s3 bucket (assumes a bucket named "my-bucket" has been created)
    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...

Author: Josh
Since I was a small boy – learning to write code while my friends played basketball – computers and technology have been and are my passion. My strong analytical skills, thorough knowledge and understanding of network protocols and technologies, and ability to learn new skills on the fly – enable me to troubleshoot, isolate, and resolve problems quickly and effectively.
how can we help you?

Contact us at the AllcomOne office nearest to you or submit a business inquiry online.

Looking for a top notch IT consultant?