Backup WordPress on Amazon Lightsail from the Command Line
Backup WordPress on Amazon Lightsail from the Command Line
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
- 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
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
- Configure for use
aws configure
See [Amazon's documentation](3) here If you need help with this.
- 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…