Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Backup MySQL Database From Java Part 2

After we know how to separate the backup process between data, stored routine, and trigger, next thing to do is implementing them in Java.

Consider the following code:
private int BUFFER = 10485760;

private String getData(String host, String port, String user,
String password, String db) throws Exception {
Process run = Runtime.getRuntime().exec(
"mysqldump --host=" + host + " --port=" + port +
" --user=" + user + " --password=" + password +
" --compact --complete-insert --extended-insert " +
"--skip-comments --skip-triggers " + db);
InputStream in = run.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));

StringBuffer temp = new StringBuffer();

int count;
char[] cbuf = new char[BUFFER];

while ((count = br.read(cbuf, 0, BUFFER)) != -1)
temp.append(cbuf, 0, count);

br.close();
in.close();

return temp.toString();
}

private String getRoutine(String host, String port, String user,
String password, String db) throws Exception {
Process run = Runtime.getRuntime().exec(
"mysqldump --host=" + host + " --port=" + port +
" --user=" + user + " --password=" + password +
" --compact --skip-comments --no-create-info " +
"--no-data --routines " + db);
InputStream in = run.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(in));

StringBuffer temp = new StringBuffer();

int count;
char[] cbuf = new char[BUFFER];

while ((count = br.read(cbuf, 0, BUFFER)) != -1)
temp.append(cbuf, 0, count);

br.close();
in.close();

return temp.toString();
}
Now we have two functions, one for dumping only data, the other dumping stored routines and trigger. Both of them will return String variable, that is your backup.

Please note the BUFFER variable, you may customized for your need. Just remember, the bigger value, consumes more heaps but decrease loops. The smaller value, consumes less heaps but increase loops.

The next step is how to store them.



This post first appeared on HowTo For Idiots, please read the originial post: here

Share the post

Backup MySQL Database From Java Part 2

×

Subscribe to Howto For Idiots

Get updates delivered right to your inbox!

Thank you for your subscription

×