Backup an Amazon Lightsail WordPress Instance to S3 from the Command Line
- December 17, 2019
- Posted by: Josh
- Categories: Business Continuity, Cloud Computing, Web design & development
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
- SSH into your server with the default
bitnami
userssh -i ~/path/to/key bitnami@example.com
- Stop all services
sudo /opt/bitnami/ctlscript.sh stop
- 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 domkdir -p /path/to/backup/dir
). - Start all services again so your site is back online
sudo /opt/bitnami/ctlscript.sh start
- 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
).
- Install AWS CLI with apt package manager
sudo apt install aws-cli
- Configure for use
aws configure
See Amazon's documentation here If you need help with this.
- 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...