How to Backup WordPress to DigitalOcean Space with Bash

Introduction

DigitalOcean Spaces is an S3-compatible object storage service that allows you to store and serve a large amount of data. It has a built-in content delivery network (CDN) that minimizes page load times, improves performance, and reduces bandwidth and infrastructure costs. The base rate of a DigitalOcean Spaces subscription is $5.00 per month and gives you the ability to create multiple Spaces.

Setting up a WordPress backup strategy is one of the most essential things for any system or database administrator. If you have your own dedicated server then you’ll need to set up backups yourself. Regularly backing up a WordPress site data and database is a very hard and time-consuming process. In this case, you will need to think about a headache-free backup strategy or a backup tool that allows you to automatic backups from their control panel. This is where SnapShooter comes into the picture. SnapShooter provides a web-based backup solution that provides a user-friendly control panel to manage and automate WordPress backup via a web browser. SnapShooter works with a WordPress server hosted on your own server as well as WordPress managed in a DigitalOcean Managed databases.

This post will show you how to backup WordPress to DigitalOcean Spaces manually and using SnapShooter.

Create Your DigitalOcean Spaces

Before starting, you will need to create a new space on the DigitalOcean to save your backup files. Follow the below steps to create a new space on the DigitalOcean:

Step 1 - Sign in to your DigitalOcean account and click on the Spaces in the left pane.

Step 2 - Click on the Create => Space button. You should see the following screen:

Step 3 - Select your region, Restrict File Listing, Provide your unique space name and click on Create a Space button. Once the Space is created, you should see the following screen:

Create Your DigitalOcean Credentials

In order to create backups with DigitalOcean spaces, you will need the correct access key and secret key to authenticate with it. Follow the below steps to create a DigitalOcean Credentials:

Step 1 - Log in to your DigitalOcean Spaces account or go directly to https://cloud.digitalocean.com/spaces:

Step 2 - Click on the Manage Keys button on the right-hand side of the Spaces screen.

Step 3 - Click on the Generate New Key next to Spaces access keys, provide a name for your key and click the checkmark. A new Key and Secret will be generated as shown below:

Backup WordPress to DigitalOcean Space Manually

In this section, we will show you how to back up a WordPress from your server to the DigitalOcean Space using the S3cmd tool.

Install S3cmd

Before starting, you will need to install the S3cmd tool on WordPress.

S3cmd is a free and open-source command-line tool that allows you to upload, download and manage data from your server to DigitalOcean Space and other cloud storage service providers. Follow the below steps to install the S3cmd to your server.

First, install the Python and other dependencies by running the following command:

apt-get install python3 python3-setuptools curl -y

Next, download the latest version of S3cmd using the following command:

curl -LO https://github.com/s3tools/s3cmd/releases/download/v2.2.0/s3cmd-2.2.0.tar.gz

Next, extract the downloaded file with the following command:

tar -xvzf s3cmd-2.2.0.tar.gz

Next, navigate to the extracted directory and install it using the following command:

cd s3cmd-2.2.0
python3 setup.py install

Once the S3cmd is installed, verify the S3cmd version using the following command:

s3cmd --version

You will get the following output:

s3cmd version 2.2.0

Configure S3cmd

Next, you will need to configure the S3cmd using the DigitalOcean Access key and Secret key. You can configure it using the following command:

s3cmd --configure

You will be asked to provide your DigitalOcean Access Key, Secret Key, Region, and Endpoint as shown below:

Enter new values or accept defaults in brackets with Enter.
Refer to user manual for detailed description of all options.
 
Access key and Secret key are your identifiers for Amazon S3. Leave them empty for using the env variables.
Access Key: Your-Access-Key
Secret Key: Your-Secret-Key
Default Region [US]: Frankfurt 1
 
Use "s3.amazonaws.com" for S3 Endpoint and not modify it to the target Amazon S3.
S3 Endpoint [s3.amazonaws.com]: fra1.digitaloceanspaces.com
 
Use "%(bucket)s.s3.amazonaws.com" to the target Amazon S3. "%(bucket)s" and "%(location)s" vars can be used
if the target S3 system supports dns based buckets.
DNS-style bucket+hostname:port template for accessing a bucket [%(bucket)s.s3.amazonaws.com]: %(bucket)s.fra1.digitaloceanspaces.com
 
Encryption password is used to protect your files from reading
by unauthorized persons while in transfer to S3
Encryption password: password
Path to GPG program:
 
When using secure HTTPS protocol all communication with Amazon S3
servers is protected from 3rd party eavesdropping. This method is
slower than plain HTTP, and can only be proxied with Python 2.7 or newer
Use HTTPS protocol [Yes]:
 
On some networks all internet access must go through a HTTP proxy.
Try setting it here if you can't connect to S3 directly
HTTP Proxy server name:
 
New settings:
Access Key: 6JDVIPGT7U4CU5MKH6J2
Secret Key: ftOa7o3x53IF2j+FhVvLJqnnRVkKscH97OmgyzEGYSw
Default Region: Frankfurt 1
S3 Endpoint: fra1.digitaloceanspaces.com
DNS-style bucket+hostname:port template for accessing a bucket: %(bucket)s.fra1.digitaloceanspaces.com
Encryption password: password
Path to GPG program: None
Use HTTPS protocol: True
HTTP Proxy server name:
HTTP Proxy server port: 0
 
Test access with supplied credentials? [Y/n] Y
Please wait, attempting to list all buckets...
Success. Your access key and secret key worked fine :-)
 
Now verifying that encryption works...
Not configured. Never mind.
 
Save settings? [y/N] y
Configuration saved to '/root/.s3cfg'

Once the S3cmd is configured, you can verify your bucket information using the following command:

s3cmd info s3://backup-wordpressdb/

You will get the following output:

s3://backup-wordpressdb/ (bucket):
Location: fra1
Payer: none
Expiration Rule: none
Policy: none
CORS: none
ACL: 6945323: FULL_CONTROL

Use S3cmd to Backup WordPress to DigitalOcean Spaces

First, log in to your WordPress server and connect to the MariaDB shell with the following command:

mysql -u root -p

Once you are logged in, list all available databases using the following command:

MariaDB [(none)]> show databases;

You should your databases in the following output:

Next, backup the wordpress_db database using the following command:

mysqldump -u root -p wordpress_db > wordpress_db.sql

Next, run the S3cmd command to copy the wordpress_db.sql file to the DigitalOcean Spaces:

s3cmd put wordpress_db.sql s3://backup-wordpressdb/

You should see the following output:

upload: 'wordpress_db.sql' -> 's3://backup-wordpressdb/wordpress_db.sql' [1 of 1]
1076845 of 1076845 100% in 2s 378.20 KB/s done

Next, run the S3cmd command again to copy the WordPress website data to the DigitalOcean Spaces:

tar -czvf - /var/www/html/wordpress | s3cmd put - s3://backup-wordpressdb/wordpress.tar.gz

Output:

upload: '<stdin>' -> 's3://backup-wordpressdb/wordpress.tar.gz' [part 2 of -, 2MB] [1 of 1]
2992157 of 2992157 100% in 0s 4.38 MB/s done

You can now verify all backups on the DigitalOcean Spaces using the following command:

s3cmd ls s3://backup-wordpressdb

You will get the following output:

2022-05-10 15:48 18720797 s3://backup-wordpressdb/wordpress.tar.gz
2022-05-10 15:47 1076845 s3://backup-wordpressdb/wordpress_db.sql

You can also log in to the DigitalOcean Spaces and verify your backup files as shown below:

Scheduled WordPress Backups SnapShooter

Backup a WordPress website, database (MySQL) and the file system

Learn more about WordPress Backups

Get started for free
No credit card required. Cancel anytime!

Was this page helpful?

Thank you for helping us improve!