How can I set up a daily MySQL database backup using cron jobs?
Asked on Sep 04, 2025
Answer
To set up a daily MySQL database backup using cron jobs, you'll need to create a cron job that executes a backup script at a specified time each day. This script will use the `mysqldump` command to export your database to a file.
0 2 * * * /usr/bin/mysqldump -u [username] -p[password] [database_name] > /path/to/backup/db_backup_$(date +\%F).sql
Additional Comment:
- Replace `[username]`, `[password]`, and `[database_name]` with your MySQL credentials and database name.
- The cron job is set to run daily at 2 AM. Adjust the timing as needed by modifying the cron schedule.
- Ensure the backup directory `/path/to/backup/` exists and has the appropriate write permissions.
- Consider securing your password by using a `.my.cnf` file instead of including it directly in the cron job.
- Regularly check your backups to ensure they are being created correctly and consider automating the cleanup of old backups.
Recommended Links: