Server Administration

Automated Off-Site WordPress Backups with Restic

Automated Off-Site WordPress Backups with Restic

On servers I manage, I don’t rely on WordPress plugins for backups. Plugins work for many sites, but they run inside WordPress, depend on PHP time limits and can be disabled or tampered with if the site is compromised. For business-critical sites, I want backups that run at the server level, are encrypted, are stored somewhere else entirely and keep enough history to recover from problems discovered late.

Restic is the tool I use most for this. It’s a free, open-source backup program that’s fast, encrypts everything by default and only stores data that changed since the last backup, so daily backups of large sites stay small and quick. It works with many storage backends, including object storage services and SFTP servers.

Here’s how I set up automated off-site WordPress backups with restic.

Why restic suits WordPress backups

  • Encryption by default. Everything in the repository is encrypted with a password you control. The storage provider can’t read your data.
  • Deduplication. Restic splits files into chunks and stores each chunk once. A site with 20 GB of uploads that changes by a few megabytes a day only uploads those few megabytes each day.
  • Snapshots. Each backup is a point-in-time snapshot you can browse and restore from.
  • Flexible retention. Keep daily, weekly and monthly snapshots automatically.
  • Integrity checks. Restic can verify the repository and data.
  • Many backends. Object storage, SFTP, local disks and more, plus additional services through rclone.

What to back up

A WordPress backup needs two parts:

  1. The database, which holds content, settings, users and orders.
  2. The files, especially wp-content, which holds uploads, themes and plugins. WordPress core can be reinstalled from official sources, but backing up the whole site directory keeps restores simple.

Also consider backing up server configuration, like Nginx site files and PHP-FPM pools, so you can rebuild a server quickly.

Step 1: Choose storage

Store backups away from the server, with different credentials. Good options:

  • Object storage from a cloud provider. Affordable, durable and widely supported by restic.
  • A separate backup server reachable over SFTP.

Create a dedicated bucket or account for backups, with credentials that only allow access to that bucket. If the provider supports object versioning or object lock, enabling it protects backups even if an attacker obtains the credentials and tries to delete them.

Step 2: Install restic

On Ubuntu or Debian:

Or download a current release from the project’s official releases. Check the version with restic version.

Step 3: Store credentials securely

Restic needs the repository location, the repository password and storage credentials. Put them in a file readable only by root:

Contents, for an S3-compatible service:

Then:

Store a copy of the repository password somewhere safe outside the server, such as a password manager. If you lose the password, the backups cannot be decrypted. Nobody can recover them for you.

Step 4: Initialise the repository

This creates the encrypted repository in your storage.

Step 5: Write the backup script

The script dumps each database, then backs up the site files and the dumps together:

mysqldump –single-transaction –quick –routines "$DB"gzip > "$DUMP_DIR/$DB.sql.gz"

A few notes:

  • –single-transaction gives a consistent database snapshot without locking tables on InnoDB, so the site keeps running during the dump.
  • Excludes skip cache folders and logs that don’t need backing up.
  • Database credentials for mysqldump can be provided through a protected option file, such as /root/.my.cnf, rather than in the script.
  • Retention in this example keeps 14 daily, 8 weekly and 12 monthly snapshots. Adjust to your needs. Longer history protects against infections discovered late.

Save it as /usr/local/bin/backup-sites.sh and make it executable:

Run it manually once and check the output for errors.

Step 6: Schedule it

Run the script nightly with cron or a systemd timer. With cron, as root:

Choose a quiet time for your sites. For busy stores, consider more frequent database dumps, such as every few hours, so you’d lose fewer orders in a restore.

Step 7: Monitor backups

A backup system that fails silently is worse than useless, because it creates false confidence. Monitor it:

  • Heartbeat monitoring: add a request to a monitoring service’s ping URL at the end of the script, so you’re alerted if a night passes without a successful backup.
  • Check logs for errors periodically.
  • List snapshots to confirm recent ones exist:
<code>sudo bash -c 'source /etc/restic/env &amp;&amp; restic snapshots –compacttail -5'</code>

Step 8: Test restores

Testing restores is the step most people skip. Do it regularly, ideally monthly, on a staging server.

Restore the latest snapshot to a temporary location:

Restore a database dump:

<code>gunzip -c /tmp/restore-test/var/backups/db/site1_wp.sql.gzmysql site1_restore_test</code>

Then bring the restored site up on staging and check it works: pages load, images appear, the admin works and recent content is present.

Time how long a full restore takes. That’s your real recovery time.

You can also browse snapshots by mounting the repository as a filesystem, which is handy for recovering a single file:

Step 9: Verify data integrity periodically

restic check verifies the repository structure. Occasionally, verify a portion of the actual data as well:

This reads and verifies a sample of stored data, catching storage corruption without downloading everything every time.

Protecting backups from attackers

Ransomware and attackers increasingly target backups. Make yours harder to destroy:

  • Use storage credentials limited to the backup bucket.
  • Enable object versioning or object lock where available, so deleted or overwritten backups can be recovered.
  • Consider an append-only setup, where the server can add backups but not delete them, with pruning done from a separate, trusted machine.
  • Keep the repository password outside the server.

Why server-level backups matter in incidents

During security incidents, I’ve seen attackers delete backups stored on the same server, disable backup plugins and even tamper with backup files to hide their tracks. On one compromised site, every backup the plugin had made was stored in a folder inside wp-content, and the attacker had deleted them all before encrypting the uploads folder. The owner had nothing to restore from.

Backups that run at the server level, encrypt data before it leaves the machine and store it with a separate provider under separate credentials are much harder to destroy. With versioning or object lock enabled at the storage provider, even an attacker with the backup credentials can’t permanently delete history.

That’s the main reason I set up restic, or a similar tool, on servers I’m responsible for, even when a backup plugin is also installed. The plugin is convenient for quick restores of a single site; the server-level backup is the safety net for when everything else has gone wrong.

Restoring a full server

Think beyond restoring a single site. If the whole server failed tomorrow, could you rebuild it? Because the backup includes web server and PHP configuration as well as sites and databases, a full rebuild becomes: provision a new server, install the stack, restore configuration and site files from restic, import the databases, point DNS at the new server and test. Write these steps down, with the commands you’d use, and practise them on a test server at least once. The first time you do it shouldn’t be during a real emergency.

Costs

Restic itself is free. The main cost is storage, which for most small and medium WordPress servers is modest thanks to deduplication and compression. Even keeping a year of history for several sites often takes far less space than you’d expect, because unchanged files are stored only once.

Encrypting database dumps at rest

The database dumps sit on the server briefly before restic uploads them. Keep the dump folder readable only by root, and consider deleting dumps after a successful backup, so copies of customer data don’t linger on disk longer than necessary.

Review retention yearly

Once a year, review whether your retention still fits. Busier stores, new legal requirements or longer investigations may justify keeping history longer, while sites with sensitive data may need shorter retention for privacy reasons.

Frequently asked questions

Are plugin backups bad?

No. Plugin backups are convenient and fine for many sites, especially on shared hosting. Server-level backups with tools like restic are more robust and harder to tamper with on servers you control.

How much history should I keep?

At least several weeks of daily backups plus monthly backups for several months. Infections and data problems often go unnoticed for weeks.

What happens if I lose the restic password?

The backups can’t be decrypted. Store the password securely outside the server.

How often should I test restores?

Monthly is a good routine for important sites, and always after changing the backup setup.

Final thoughts

Reliable backups are your last line of defence against hacks, mistakes and hardware failures. With restic, you can back up WordPress databases and files to off-site storage, encrypted and deduplicated, with automatic retention and integrity checks. Store credentials and the repository password securely, schedule nightly runs, monitor every backup, test restores regularly and protect backups from deletion. When you need them, you’ll know they work.