RSS

Take the MySql DB backup in JAVA

28 Sep

This is very impotent blog post for the most application developers
Here I’m talking about how to backup the mysql databasae through JAVA program.
Download MySqlBackup.java you have to download mysql.jar and log4j.jar

1. Following method will create the data base backup without create and drop database commands
It’s only creating tables and included data
When your going to restore the database first you have to create new DB

public boolean backupDataWithOutDatabase(String dumpExePath, String host, String port, String user, String password, String database, String backupPath) {
boolean status = false;
try {
Process p = null;

DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date date = new Date();
String filepath = "backup(without_DB)-" + database + "-" + host + "-(" + dateFormat.format(date) + ").sql";

String batchCommand = "";
if (password != "") {
//only backup the data not included create database
batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + " --password=" + password + " " + database + " -r \"" + backupPath + "" + filepath + "\"";
} else {
batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + " " + database + " -r \"" + backupPath + "" + filepath + "\"";
}

Runtime runtime = Runtime.getRuntime();
p = runtime.exec(batchCommand);
int processComplete = p.waitFor();

if (processComplete == 0) {
status = true;
log.info("Backup created successfully for without DB " + database + " in " + host + ":" + port);
} else {
status = false;
log.info("Could not create the backup for without DB " + database + " in " + host + ":" + port);
}

} catch (IOException ioe) {
log.error(ioe, ioe.getCause());
} catch (Exception e) {
log.error(e, e.getCause());
}
return status;
}

2. This method will create a backup from Specified database with create and drop database commands
When your going to restore the DB you don’t need to create the DB it will handle by the script

public boolean backupDataWithDatabase(String dumpExePath, String host, String port, String user, String password, String database, String backupPath) {
        boolean status = false;
        try {
            Process p = null;

            DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
            Date date = new Date();
            String filepath = "backup(with_DB)-" + database + "-" + host + "-(" + dateFormat.format(date) + ").sql";

            String batchCommand = "";
            if (password != "") {
                //Backup with database
                batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + " --password=" + password + " --add-drop-database -B " + database + " -r \"" + backupPath + "" + filepath + "\"";
            } else {
                batchCommand = dumpExePath + " -h " + host + " --port " + port + " -u " + user + " --add-drop-database -B " + database + " -r \"" + backupPath + "" + filepath + "\"";
            }

            Runtime runtime = Runtime.getRuntime();
            p = runtime.exec(batchCommand);
            int processComplete = p.waitFor();

            if (processComplete == 0) {
                status = true;
                log.info("Backup created successfully for with DB " + database + " in " + host + ":" + port);
            } else {
                status = false;
                log.info("Could not create the backup for with DB " + database + " in " + host + ":" + port);
            }

        } catch (IOException ioe) {
            log.error(ioe, ioe.getCause());
        } catch (Exception e) {
            log.error(e, e.getCause());
        }
        return status;
    }

Advertisements
 
10 Comments

Posted by on September 28, 2012 in java, mysql

 

Tags: , ,

10 responses to “Take the MySql DB backup in JAVA

  1. Karan Ahuja

    September 9, 2014 at 7:14 am

    Thanks, its very nice.
    1 Query for handling bigger database can we use hibernate?

     
  2. Thedath Oudarya

    January 10, 2015 at 6:56 am

    You da man bro!
    Love you much, not gay though! 😀
    I’ve been ransacking the internet to find a code to do this and finally here it is, thanks to you!
    Wish you a good luck!

     
  3. GoesPoer

    May 8, 2015 at 10:47 am

    Reblogged this on Kidungsuwung.

     
  4. akshaykakabalia@gmail.com

    June 22, 2015 at 7:13 am

    Hi! How can we get the path of mysqldump.exe path?

     
  5. S.Kaweendra

    October 5, 2015 at 5:24 am

    String executeCmd = “C:\\Program Files (x86)\\MySQL\\MySQL Server 5.0\\bin\\mysqldump -uroot -p123 librarysy -r D:\\backup.sql”;

     
  6. Pritam

    January 22, 2016 at 12:54 pm

    Thanks…………………. 🙂

     
  7. prasanth

    February 6, 2017 at 3:20 am

    thanks aiye, you make me my day…!

     
  8. Pikyi

    April 23, 2017 at 4:27 pm

    Hay man!!!!!!
    it really works…. i found many ways but i got now
    thank a lot …… u make my day!

     
  9. Swapnil

    May 23, 2017 at 7:18 am

    Hello I m trying to get backup of db but I am unable to get backup
    My batchCommand excutesbut processComplete returns 1
    Please Help

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: