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





5 comments:
pertamaxx...
nice info
great job,thnaks so much for your code...
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,
good jobs...thanks for your code.....is it possible to use mysql command for restoring in java
great! we can backup a database into a zip file..How to restore the database in Java, using the zip file?
Post a Comment