Economicalhost · Knowledge Base
Knowledge Base › Cron & Automation › How to Schedule a Database Backup with Cron
Cron & Automation

How to Schedule a Database Backup with Cron

Automated database backups protect dynamic sites between your full backups. A short cron job dumps your database to a file on a schedule.

Step-by-step

  1. Create a backups folder outside public_html (e.g. /home/youruser/db-backups) in File Manager so dumps are not web-accessible.
  2. Open Advanced → Cron Jobs and set a daily schedule, e.g. 30 3 * * * (3:30 am).
  3. Enter a mysqldump command (replace placeholders):
    /usr/bin/mysqldump -u DBUSER -p'DBPASS' DBNAME > /home/youruser/db-backups/db-$(date +\%F).sql
  4. Add the job. Each run writes a dated .sql file so you keep a history.
  5. Prune old dumps with a second weekly cron, e.g. delete files older than 14 days, so they do not fill your disk.
  6. Download copies off-server periodically — a backup on the same server is not a true backup. See full backups.

💡 Good to know

  • Get the exact DB name/user from your app config; the user needs access to that database.
  • Escape the % signs as \% in the cron command, or the date will not format correctly.
  • Combine with scheduled full backups for complete protection.