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
- Create a backups folder outside public_html (e.g. /home/youruser/db-backups) in File Manager so dumps are not web-accessible.
- Open Advanced → Cron Jobs and set a daily schedule, e.g.
30 3 * * *(3:30 am). - Enter a mysqldump command (replace placeholders):
/usr/bin/mysqldump -u DBUSER -p'DBPASS' DBNAME > /home/youruser/db-backups/db-$(date +\%F).sql - Add the job. Each run writes a dated .sql file so you keep a history.
- Prune old dumps with a second weekly cron, e.g. delete files older than 14 days, so they do not fill your disk.
- 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.