Guide Databases & MySQL

How to back up a database on a schedule

A scheduled export you never have to remember to run is worth more than the best manual backup habit.

Updated 8 min read Intermediate

A manual export is fine right up until the week you forget to do it, which is usually also the week something goes wrong. A scheduled backup removes the human step entirely: the database exports itself on a timer, without anyone needing to remember.

Check what is already covered first

Before building your own backup routine, check whether one already exists. Account-level backups, where offered on your plan, typically capture your databases alongside your files as part of the same routine, which may already give you what you need without any extra setup. See whether your site is backed up for you to check what your plan includes, and where backups should be stored for why relying on a copy that lives on the same server as the original is not enough on its own.

What follows is for when you specifically want your own scheduled database export — as a second, independent copy, or because you need more control over timing and retention than an account-level backup gives you.

Building a scheduled export

This requires cron access, available through the control panel on most plans and standard on a VPS or dedicated server. The core of it is a short script that runs mysqldump and saves the result with a date in the filename:

#!/bin/bash
TIMESTAMP=$(date +%F)
mysqldump -u dbuser -p'dbpassword' --single-transaction dbname | gzip > /home/youraccount/backups/dbname-$TIMESTAMP.sql.gz

--single-transaction takes a consistent snapshot of an InnoDB database while it is still live and being written to, rather than risking a dump that captures some tables mid-change. Piping through gzip keeps each day's file small.

A plaintext password in a script is a real risk

Anyone who can read this script file can read your database password. Set restrictive permissions on it — chmod 600 so only your own account can read it — and better still, avoid putting the password in the script at all. See the credentials file approach below.

A safer way to hold the credentials

Rather than a password on the command line, where it can also show up in process listings while the command runs, create a credentials file the mysql client reads automatically:

# ~/.my.cnf
[client]
user=dbuser
password=dbpassword

Set its permissions to chmod 600 ~/.my.cnf so only you can read it, then drop the -u and -p options from the backup command entirely — the client picks up the credentials from this file automatically:

mysqldump --single-transaction dbname | gzip > /home/youraccount/backups/dbname-$TIMESTAMP.sql.gz

Scheduling it with cron

Once the script is saved and made executable, add it as a cron job to run on whatever schedule suits the site — daily, for most sites, is a reasonable default. Our guide to setting up a cron job covers adding this through your control panel if you have not done it before. A typical daily-at-2am schedule looks like this in crontab syntax:

0 2 * * * /home/youraccount/scripts/backup-db.sh

Managing the backups once they exist

A script that runs forever without ever deleting anything eventually fills up your disk space with old backups. Add a cleanup step — either within the same script or as a separate scheduled job — that removes files older than however long you actually need to keep them:

find /home/youraccount/backups/ -name "*.sql.gz" -mtime +14 -delete

That example keeps two weeks of daily backups and removes anything older automatically.

Copy backups off the server they were taken from

A backup stored only on the same server as the database it protects does not help if that server has a serious failure. Once the schedule is working reliably, add a step that copies each file somewhere else — a separate storage service, or downloaded periodically — so a single server problem cannot take out both the live database and every backup of it at once.

For WordPress specifically

If cron access is not available to you, or you would rather manage this from within WordPress itself, a dedicated backup plugin can schedule database exports from the admin area without needing shell access at all. It is a reasonable alternative to the script-based approach above, particularly on shared hosting plans where cron and SSH access may be more limited. Either route achieves the same goal — a database export that happens on a schedule, without relying on anyone remembering to run it manually — so choose whichever fits the access you actually have rather than assuming the command-line approach is required.

Whichever method you use, test the result occasionally by actually importing a backup file somewhere disposable — a scheduled backup that has silently been failing for weeks is worse than no backup at all, because it creates false confidence. Restoring a database backup covers that side of the process.

Related reading