Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 446
Hussy Avatar asked Jul 05 '11 06:07

Hussy


2 Answers

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
like image 145
wonk0 Avatar answered Oct 25 '22 00:10

wonk0


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
like image 7
KumarA Avatar answered Oct 25 '22 00:10

KumarA