Daily Database backup using Cron Job

Hi i want to take database backup at daily mid night using cron job… and the name of database backup should append with current date… the format of backup file should be mydata_yyyy_mm_dd.sql …
backup file should be placed in /root directory


Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

something like

0 0 * * * /path/to/mysqldump ... > /path/to/backup/mydata_$( date +"%Y_%m_%d" ).sql

should work.

Please read

  • man date
  • man 5 crontab

Method 2

Create a cron.sh file with this content:

 mysqldump -u root -p{PASSWORD} DBNAME 2>> "/filename_`date '+%Y-%m-%d'`.sql"

And give the Read permission or full access permission for that cron.sh file.

and add this line into crontab file ($ crontab -e)

 0 0 * * *   cron.sh

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x