MySQL database backup every day using java code

javaMySQL

We are developing a Java web application using Spring, Hibernate and MySQL.

I want our application to automatically backup the MySQL database daily; is there any way to accomplish that with Java code?

Best Answer

This is easy to do.

  1. Write a script to create the backup using the "mysqldump" tool available at http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html - Google "mysql backup unix/windows script" to find some examples.

  2. Use a task scheduler to run this script. On UNIX this could be "cron", Windows as a "task scheduler". Best to do so at times when the app is under low-usage.

It is worth while putting some backup file cycling into the script so that you keep, say, every backup from the last week and delete the rest.

Also remember to backup to a physically different disk from the database itself so you have a backup if the disk dies.