Securing VPS/Servers Backups

WordPress Backup on a VPS Without a Plugin: Script and Restore

Back up WordPress on your own VPS with a short shell script: database dump, wp-content archive, retention, an off-site copy with rclone or restic, and a restore test you can run.

A WordPress backup is two things: a dump of the database and a copy of the files that are unique to your site. On a VPS you can produce both with a short shell script, run it every night from cron, and send the result to storage outside the server. No plugin is needed, and the backup does not run inside a web request that can time out.

A backup counts only when a restore has been tested. The last section restores the latest backup into a scratch directory and a scratch database, which shows whether the files are usable without touching the live site.

The commands follow the WP-CLI, MariaDB, rclone and restic documentation. They were not run on a test server for this article; test the script and the restore on your own server before relying on them.

What has to be backed up

WhatWhyHow
The databasePosts, pages, users, settings, orderswp db export or mariadb-dump
wp-content/Uploads, plugins, themes, language filestar
wp-config.phpDatabase credentials, salts, your own constantsIn the same archive
nginx and PHP-FPM configurationSaves time when rebuilding the serverCopy of /etc/nginx and the PHP pool file

WordPress core (wp-admin, wp-includes and the PHP files in the root) does not need a backup. It can be downloaded again with wp core download.

The backup script

Save this as /usr/local/bin/wp-backup.sh. It needs WP-CLI; see the WP-CLI guide for installing it.

#!/bin/bash
set -euo pipefail
umask 077

SITE=/var/www/example.com
DEST=/var/backups/wordpress
STAMP=$(date +%F-%H%M)

/usr/local/bin/wp --path="$SITE" db export "$DEST/db-$STAMP.sql" --single-transaction
gzip "$DEST/db-$STAMP.sql"

tar -czf "$DEST/files-$STAMP.tar.gz" -C "$SITE" --exclude='wp-content/cache' wp-content wp-config.php

# keep 7 days locally
find "$DEST" -type f -name '*.gz' -mtime +7 -delete

What the lines do:

  • set -euo pipefail stops the script at the first failed command, so a failed dump is not followed by a cleanup of older, good backups.
  • umask 077 makes the new files readable by their owner only. The dump contains password hashes and personal data.
  • wp db export reads the credentials from wp-config.php and runs the database dump tool. Extra flags are passed through to it. --single-transaction gives a consistent dump of InnoDB tables without locking the site during the export.
  • find ... -mtime +7 -delete removes backups older than seven days. Run it once without -delete to see what it would match.
  • The full path to wp is used because cron runs with a short PATH. Check yours with command -v wp.

Without WP-CLI, replace the export line with the dump tool itself, run as root (on MySQL the command is mysqldump):

mariadb-dump --single-transaction wordpress | gzip > "$DEST/db-$STAMP.sql.gz"

Run it as the right user, every night

Run the script as the user that owns the site files, so that it can read wp-config.php and every upload. On a default setup that is www-data on Ubuntu and Debian and nginx or apache on AlmaLinux and Rocky Linux; if you use a separate site user, use that one. WP-CLI refuses to run as root unless forced, and should not be.

One limit of this setup: the local backups belong to the same user that runs PHP, so an attacker who takes over WordPress can read or delete them. The off-site copy below is what protects you in that case, which is why it is not optional.

# Ubuntu, Debian (use nginx or your site user on AlmaLinux, Rocky Linux)
sudo chmod 755 /usr/local/bin/wp-backup.sh
sudo install -d -o www-data -g www-data -m 700 /var/backups/wordpress
sudo -u www-data /usr/local/bin/wp-backup.sh
sudo ls -lh /var/backups/wordpress

The listing should show one db- and one files- archive with plausible sizes. Check both are readable:

sudo sh -c 'gzip -t /var/backups/wordpress/db-*.sql.gz && echo ok'
sudo sh -c 'tar -tzf /var/backups/wordpress/files-*.tar.gz | head'

Then schedule it with sudo crontab -u www-data -e:

15 3 * * * /usr/local/bin/wp-backup.sh >> /var/backups/wordpress/backup.log 2>&1

A systemd timer does the same job and records each run in the journal. Create wp-backup.service with Type=oneshot, User=www-data and ExecStart=/usr/local/bin/wp-backup.sh, and wp-backup.timer with OnCalendar=*-*-* 03:15:00 and Persistent=true under [Timer] and WantedBy=timers.target under [Install]. Enable it with sudo systemctl enable --now wp-backup.timer and confirm with systemctl list-timers wp-backup.timer. For WordPress's own scheduled tasks, see WordPress cron jobs.

Copy the backups off the server

Archives that exist only on the VPS are lost together with it: a deleted server, a failed disk or an attacker with access removes the site and its backups at once. Send a copy to object storage (storage for files reached over an API, such as S3-compatible buckets) or to another machine.

rclone copies files to many storage services. Run rclone config once, as the backup user, to create a named remote (called offsite here), then add to the end of the script:

rclone copy /var/backups/wordpress offsite:my-bucket/example.com --include '*.gz'
rclone lsl offsite:my-bucket/example.com | tail -3     # verify

Use rclone copy, not rclone sync. The rclone documentation states that sync makes the destination identical to the source, deleting files if necessary, so local retention or an emptied local folder would remove the off-site copies as well. Since copy never deletes, limit the off-site history with a lifecycle (expiry) rule on the bucket.

restic is a backup program that encrypts and deduplicates, so many versions take little extra space, and it handles retention itself. With an S3-compatible bucket:

export AWS_ACCESS_KEY_ID=YOUR-KEY-ID
export AWS_SECRET_ACCESS_KEY=YOUR-SECRET
export RESTIC_REPOSITORY=s3:https://s3.example.net/my-bucket
export RESTIC_PASSWORD_FILE=/var/backups/.restic-password

restic init                                   # once
restic backup /var/backups/wordpress
restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune
restic snapshots                              # verify

Store the restic password somewhere other than this server. The restic documentation is explicit that a lost password means the data cannot be recovered. With restic, the local retention can be shortened to one or two days.

If you can, give the storage key permission to write but not to delete, so that a compromised server cannot erase its own backups.

Provider snapshots are a complement

A snapshot from your VPS provider captures the whole disk and is the fastest way back after a failed upgrade. It does not replace the script. Snapshots are usually stored with the same provider and account as the server, they restore the whole machine and not one site or one table, and a snapshot taken while the database is writing may not be consistent. Use both; see VPS backup strategy.

Test a restore

This uses a scratch directory and a scratch database, so the live site is not touched. Replace the file names with your latest pair.

mkdir -p /tmp/restore-test && cd /tmp/restore-test
sudo tar -xzf /var/backups/wordpress/files-2026-11-22-0315.tar.gz
ls wp-content/uploads | tail -3

sudo mariadb -e "CREATE DATABASE wp_restore_test;"
sudo sh -c 'gunzip -c /var/backups/wordpress/db-2026-11-22-0315.sql.gz | mariadb wp_restore_test'
sudo mariadb -e "SELECT COUNT(*) FROM wp_restore_test.wp_posts; SELECT option_value FROM wp_restore_test.wp_options WHERE option_name='siteurl';"

On MySQL, use mysql in place of mariadb. The restore passes if the newest uploads are present, the post count is plausible and siteurl is your address. Adjust wp_ if your tables use another prefix. Clean up:

sudo mariadb -e "DROP DATABASE wp_restore_test;"
sudo rm -rf /tmp/restore-test

A real restore is the same steps aimed at the live paths: download a fresh core with wp core download, unpack the archive into the site directory, import the dump with wp db import, and fix ownership. Moving to a new server adds DNS and certificates; see migrating from one VPS to another. After a compromise, read cleaning up a hacked WordPress site before restoring anything.

How often and where: a rule of thumb

WhatHow oftenWhere kept
DatabaseDaily; hourly for a shop or busy forum7 days on the server, 30 days or more off-site
wp-content and wp-config.phpDailySame as the database
Server configuration (/etc)Weekly, and after changesOff-site
Provider snapshotWeekly, and before upgradesWith the provider
Restore testA few times a yearScratch directory and database

These intervals are common practice, not a standard. Choose them by asking how much work you can afford to lose.

When a backup plugin is a reasonable choice

Backup plugins such as UpdraftPlus or Duplicator do the same job from the dashboard. They are a reasonable choice when the people who look after the site do not use SSH, when a restore has to be possible from the admin area, or when you move sites between hosts where you have no shell. Their limits are that they run inside PHP, so large sites can hit time and memory limits, and that a broken or compromised WordPress also breaks its own backup tool.

Share story:

Continue reading other guides on How VPS.