+254725682556 +254725052660 [email protected]
Client Area

Backup/Restore

How to Schedule Automatic MySQL Database Backups Separately From a Full Backup

If your website has a database that changes often, such as an online store taking orders every day or a membership site with daily sign-ups, a once-a-week full account backup might not be enough on its own. You can set up a separate, more frequent backup that captures only your MySQL database, without re-backing up your entire website every time it runs.

Why back up your database on its own schedule

A full account backup covers your files and your database together, but it's usually scheduled weekly or monthly because it takes longer and uses more disk space. Your database, by contrast, is typically small and quick to back up. Running a lightweight database-only backup daily, or even more often, gives you a much more recent recovery point if something goes wrong, while your full backups continue protecting your files on their normal schedule.

What you'll need first

  • Your database name, database username, and database password. You can find these under MySQL Databases in your control panel, or in your website's configuration file.
  • A private folder outside your website's public folder to store the backup files, for example a folder named backups created directly in your home directory.
Never store database backups inside public_html (or your equivalent public web folder). Anything in that folder can potentially be reached over the web, and a backup file full of your data should never be publicly accessible.

Set up the automatic backup

You'll create a cron job that runs a single command on a schedule. The command itself is the same whichever control panel you use; only where you go to add the cron job differs.

In cPanel

  1. Log in to cPanel and, under the Advanced section, click Cron Jobs.
  2. Under Add New Cron Job, choose how often it should run. Daily is a good starting point for most sites.
  3. In the Command field, enter a command similar to this, replacing the capitalized parts with your own details: mysqldump -u DBUSERNAME -pDBPASSWORD DBNAME | gzip > /home/CPANELUSERNAME/backups/DBNAME-$(date +%Y-%m-%d).sql.gz
  4. Click Add New Cron Job to save it.

In DirectAdmin

  1. Log in to DirectAdmin and, under Advanced Features, click Cron Jobs.
  2. Click Add Cron Job and set the schedule the same way.
  3. In the command field, use the same mysqldump command shown above, adjusting the path and username to match your DirectAdmin account: mysqldump -u DBUSERNAME -pDBPASSWORD DBNAME | gzip > /home/DIRECTADMINUSERNAME/backups/DBNAME-$(date +%Y-%m-%d).sql.gz
  4. Click Create to save it.
There must be no space between -p and your password in the command. Double-check your database name and username on the MySQL Databases page first, since a typo here will make the cron job fail silently.

Clean up old backups automatically

Daily database backups add up over time. You can add a second command to the same cron job to automatically delete backups older than a set number of days, so you don't have to clear out the folder by hand:

find /home/USERNAME/backups/ -name "*.sql.gz" -mtime +30 -delete

Add this after your mysqldump command in the same Command field, separated by &&, to keep only the most recent 30 days of backups.

A database-only backup protects your content and settings, not your theme files, images, or other uploads. Pair it with a regular full account backup so both your files and your database stay protected.