Skip to content

Scheduling automatic per-database backups on cPanel with cron and mysqldump

JetBackup covers the whole account nightly, but you often want a per-database dump on its own schedule. Here is a cron + mysqldump routine with rotation that any cPanel account can run.

Account-level backups — JetBackup, the WHM backup system — run on the server’s schedule and capture everything: files, mail, and databases together. That is the right safety net, but it is not always the right tool for a single database. Before a risky migration, for a database that changes far more often than the nightly window, or when you simply want a portable .sql.gz you can grab without restoring a whole account, a per-database mysqldump on its own cron schedule is the cleaner answer. The routine below is one any cPanel account can set up through the cron interface, with rotation so the dumps do not quietly eat the disk quota.

What’s happening

mysqldump produces a single SQL file that recreates a database from nothing — schema and data, restorable with the mysql client. Run by hand it is a one-off; run from cron it becomes a scheduled backup. cPanel exposes per-user cron in cPanel » Cron Jobs, which is the standard place to register the job so it runs as the account user, not root.

The pieces that make this production-worthy rather than a naked mysqldump line: credentials kept out of the command (in a ~/.my.cnf so the password is not visible in the process list or the cron mail), a timestamped filename so each run is distinct, compression so the files are small, and rotation so old dumps are deleted on a schedule. Without rotation, a daily dump of a growing database will fill the account’s disk quota within weeks and start failing silently. cPanel accounts share the host’s disk, so an unbounded backup directory is a real way to take down your own site with a full home directory. The whole thing is a few lines, but each line earns its place.

How to verify

Confirm mysqldump works for this account and the database name, before scheduling anything:

# A manual dump first — prove the credentials and DB name are right
mysqldump -u cpuser_dbuser -p cpuser_dbname > /home/cpuser/test_dump.sql
ls -lh /home/cpuser/test_dump.sql

# Where will backups live, and is there quota headroom?
df -h /home
quota -s 2>/dev/null

Set up credential-free auth so the password is not on the command line:

# Create ~/.my.cnf so cron mysqldump needs no -p on the command line
# (file must be chmod 600 — readable only by the account user)
cat ~/.my.cnf       # confirm it has [client] user= and password=
ls -l ~/.my.cnf     # must show -rw------- (600)

Confirm the cron environment can find mysqldump (cron has a minimal PATH):

# Full path to mysqldump — use this in the cron command
which mysqldump
mysqldump --version

The fix

  1. Store credentials in a locked-down ~/.my.cnf — so the password never appears in the cron command or the process list. Create ~/.my.cnf with a [client] section containing user=cpuser_dbuser and password=..., then chmod 600 ~/.my.cnf. With this in place, mysqldump cpuser_dbname runs without -u/-p on the command line. A password passed as -pSECRET is visible to anyone running ps — this avoids that.
  2. Write a small backup script rather than a one-line cron — a script is easier to test and rotate than a cron one-liner. Create ~/db-backup.sh that timestamps the file, dumps, compresses, and prunes old copies:
    #!/bin/bash
    DB="cpuser_dbname"
    DIR="/home/cpuser/db-backups"
    mkdir -p "$DIR"
    STAMP=$(date +%F-%H%M)
    /usr/bin/mysqldump --single-transaction --quick "$DB" | gzip > "$DIR/${DB}-${STAMP}.sql.gz"
    # keep the last 14 daily dumps, delete older
    find "$DIR" -name "${DB}-*.sql.gz" -mtime +14 -delete
    Make it executable with chmod +x ~/db-backup.sh and run it once by hand to confirm it produces a file.
  3. Use --single-transaction for InnoDB consistency — this dumps the database from one consistent snapshot without locking the tables, so the site keeps working during the backup. It is the right flag for a live WordPress/WooCommerce database (InnoDB). For MyISAM tables, which --single-transaction cannot snapshot, you need --lock-tables instead — but modern databases are InnoDB, so prefer the lock-free path.
  4. Register the job in cPanel » Cron Jobs — add a cron entry pointing at the script, e.g. 30 3 * * * /home/cpuser/db-backup.sh for 3:30 a.m. daily. Setting it through the cPanel UI runs it as the account user with the right environment. Avoid :00 exactly — every account scheduling at the top of the hour creates a load spike on a shared host; an offset minute spreads the work.
  5. Keep rotation honest so the disk does not fill — the find ... -mtime +N -delete line is what stops backups from filling the quota. Pick a retention that fits the disk: 14 daily dumps of a 200 MB database is manageable; 90 of a 5 GB database is not. Confirm the math against quota -s and the dump size, and revisit it as the database grows.
  6. Push a copy off the server — a backup that lives only on the same disk as the database is gone if the disk is. Have the script (or a second cron) copy the newest dump offsite: rsync to another host, rclone to object storage, or download via SFTP. A local dump protects against an accidental DROP TABLE; an offsite copy protects against losing the server.
  7. Make failures visible — a cron job that fails silently is worse than no backup, because you think you are covered. Have the script exit non-zero on a failed dump (check ${PIPESTATUS[0]} after the mysqldump | gzip) so cron emails the account on failure, or write a status line to a log you actually check. Periodically restore a dump into a throwaway database to confirm it is genuinely restorable — an untested backup is a hope, not a backup.

A per-database cron dump complements the account backup rather than replacing it: account backups for whole-site recovery, this for fast, frequent, portable database snapshots. Keep the credentials locked, the rotation bounded, and a copy offsite, and it runs untouched for years. Stack Harbor sets up and monitors database backup routines under cPanel server management — start a conversation to put a tested backup schedule behind a critical database.