Wednesday, June 25, 2008

Backup MySQL Database From Java Part 3

This is the last part of MySQL Database Backup from Java tutorial series.

In previous post, already discussed about how to get the backup streams from Process Object to String variable. What are we going to do now is, saving the variable to a file. You should consider about compressing the output file if your data is somewhat like enterprise one.

To compress the file, there is ZipOutputStream class to utilize it.

You need to call setMethod() function to define what will you do with the stream. One of the option available is ZipOutputStream.DEFLATED, meaning you will compress the stream, another is ZipOutputStream.STORED, meaning you just store the stream without compress.

Another function to call if you choose DEFLATED method is setLevel(), which is setting the compression level. The value range from 0 (Deflater.NO_COMPRESSION) to 9 (Deflater.BEST_COMPRESSION).

It will looks like:
byte[] data = getData().getBytes();
byte[] routine = getRoutine().getBytes();
File filedst = new File("example file.zip");

FileOutputStream dest = new FileOutputStream(filedst);
ZipOutputStream zip = new ZipOutputStream(
new BufferedOutputStream(dest));
zip.setMethod(ZipOutputStream.DEFLATED);
zip.setLevel(Deflater.BEST_COMPRESSION);

zip.putNextEntry(new ZipEntry("data.sql"));
zip.write(data);

zip.putNextEntry(new ZipEntry("routine.sql"));
zip.write(routine);

zip.close();
dest.close();
Explanation:
First, we collect all the data needed, see previous post
byte[] data = getData().getBytes();
byte[] routine = getRoutine().getBytes();
File filedst = new File("example file.zip");
Initiating the file stream and the zip stream
FileOutputStream dest = new FileOutputStream(filedst);
ZipOutputStream zip = new ZipOutputStream(
new BufferedOutputStream(dest));
zip.setMethod(ZipOutputStream.DEFLATED);
zip.setLevel(Deflater.BEST_COMPRESSION);
Write the file
zip.putNextEntry(new ZipEntry("data.sql"));
zip.write(data);

zip.putNextEntry(new ZipEntry("routine.sql"));
zip.write(routine);
Close the stream.
zip.close();
dest.close();

5 comments:

neyra07 said...

pertamaxx...

nice info

asep said...

great job,thnaks so much for your code...

BCS-08 said...

The complete working code of this tutorial is :

package Backup;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;


public class Backup
{
public Backup()
{

}

private int BUFFER = 10485760;

public 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();
}

public 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();
}

}

and the TestBackup Class is :




package Backup;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.util.zip.Deflater;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class TestBackup
{
public static void main(String[] args)
{
Backup b = new Backup();
try
{
byte[] data = b.getData("localhost", "3306", "mansoor", "mansoor", "books").getBytes();
byte[] routine = b.getRoutine("localhost", "3306", "mansoor", "mansoor", "books").getBytes();
File filedst = new File("d:\\example file.zip");

FileOutputStream dest = new FileOutputStream(filedst);
ZipOutputStream zip = new ZipOutputStream(
new BufferedOutputStream(dest));
zip.setMethod(ZipOutputStream.DEFLATED);
zip.setLevel(Deflater.BEST_COMPRESSION);

zip.putNextEntry(new ZipEntry("data.sql"));
zip.write(data);

zip.putNextEntry(new ZipEntry("routine.sql"));
zip.write(routine);

zip.close();
dest.close();



}
catch (Exception ex)
{
ex.printStackTrace();
}

}

}


Let me clarify that i did not add any thing to it i just assemble it for the convenience of readers.
Regards,

Murugan Subramanian said...

good jobs...thanks for your code.....is it possible to use mysql command for restoring in java

Heri T. said...

great! we can backup a database into a zip file..How to restore the database in Java, using the zip file?

border=0
Free Advertising