Home > MySQL and databases > Mysql daily backup through Cronjob

Mysql daily backup through Cronjob

October 6th, 2009

You can use following code to setup daily cron job to take database backup.

Quote:
—————————————–
year=$(date|cut -c25-2Cool
month=$(date|cut -c5-7)
day=$(date|cut -c9-10)
mysqldump -hlocalhost -uusername –password=password –opt yourdbname > /home/username/www/voice/db_voice$year$month$day.sql
echo .
echo DATABASE BACKUPS COMPLETE
echo .
—————————————–

The parts in red you will have to change to your username, password and dbname.
Please note that from mysqldump………. to db_voice$year$month$day.sql is all one line.

The parts in blue is the path/filename of the file you want to save the DB to, in my case db_voice. The $year$month$day just ensures that each backup file has a different name, i.e. the date of the backup is part of it as well.

The cronjob that calls the backup script is:

15 0 * * * /home/username/www/voice/backup_dbs > /dev/null

This runs the backup at 00:15 each morning and the orange is the full path to the script above, so you will need to change this to point to the location of your script.

MySQL and databases

  1. No comments yet.
  1. No trackbacks yet.