MySQL Database Backup & Restore Using Java – Omindu's Blog.pdf
(
465 KB
)
Pobierz
11/01/2019
MySQL Database Backup & Restore Using Java – Omindu's Blog
Omindu's Blog
Omindu in Programming
June 27, 2011May 2, 2016
840 Words
MySQL Database Backup & Restore Using Java
(https://omindu.files.wordpress.com/2011/06/java-mysql.jpg)
Hello Programmers !!
It’s been a while since I’ve written a blog post. Luckily I’m in a mood to write something at the moment. Today I
thought about posting something I’ve learnt recently. It’s about database backups and restores through java.
If anyone wants to backup or restore a database without going through all these troubles, you can simply use
MySQL GUI Tools Bundle (http://dev.mysql.com/downloads/gui-tools/5.0.html) freely distributed by the Oracle
Corporation.
Sometimes it is necessary to integrate a backup & restore system to an application we are developing. In my case it
was a java application. In this post I’m expecting to give you guys a rough idea about backing up and restoring
your MySQL database through a
Java Application.
1. Creating A Backup
When we install MySQL open source database it provides us a client called
mysqldump
for backup purposes.
Here we execute this
mysqldump
command using the java Runtime.
Here’s a sample method to create a complete database backup including add, drop MySQL statements.
https://omindu.wordpress.com/2011/06/27/mysql-database-backup-restore-using-java/
1/35
11/01/2019
MySQL Database Backup & Restore Using Java – Omindu's Blog
d + " --add-drop-database -B " + dbName + " -r " + path;
Basically what we are doing here is similar executing the following command in the windows command line.
1
mysqldump -u Username -pPassword --add-drop-database -B databaseName -r
Here
-u, -p, -B, -r
are options to indicate that we are inserting the database username, password,
database name and location to save the backup respectively. The
--add-drop-database
option is to create
a complete backup. It means that when we are restoring the backup file, if the database does not exist in that
particular database the restoring process itself create the database automatically.
If by any chance you want to create a backup without add, drop database command. You can use the following
command.
1
mysqldump -u Username -pPassword databaseName -r backupPath
For multiple databases use
1
mysqldump -u Username -pPassword --add-drop-database -B db1 db2 db3 -r b
If you want to backup
All
the databases. You can use the command as follows.
1
mysqldump -u Username -pPassword --add-drop-database -A -r backupPath
Likewise you can create your backup according to the requirements. There are many other options that you can add
to your command. You can visit mysqldump Docs (http://dev.mysql.com/doc/refman/5.1/en/mysqldump.html) for
more options or simply type
mysqldump --help
on the command line to view the options.
That’s the basic idea about backing up the database. Let’s see how we can do the restoring process through java.
2. Restoring A Backup
https://omindu.wordpress.com/2011/06/27/mysql-database-backup-restore-using-java/
2/35
11/01/2019
MySQL Database Backup & Restore Using Java – Omindu's Blog
I had hard time finding a working code for the restoring process. After making a few changes on the commands
I’ve found on the internet, I’ve manage to create a workable runtime command for the restore process.
Note: The following method is to restore a complete database backup (backup with add, drop database
command)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public boolean
restoreDB(String dbUserName, String dbPassword, String s
String[] executeCmd =
new
String[]{"mysql", "--u
Process runtimeProcess;
try
{
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int
processComplete = runtimeProcess.waitFor();
if
(processComplete ==
0)
{
System.out.println("Backup restored successfully&q
return true;
}
else
{
System.out.println("Could not restore the backup&q
}
}
catch
(Exception ex) {
ex.printStackTrace();
}
}
return false;
If the the backup doesn’t have the add, drop database, you can use the following method.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public static boolean
restoreDB(String dbName, String dbUserName, Strin
String[] executeCmd =
new
String[]{"mysql", "--u
Process runtimeProcess;
try
{
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int
processComplete = runtimeProcess.waitFor();
if
(processComplete ==
0)
{
System.out.println("Backup restored successfully&q
return true;
}
else
{
System.out.println("Could not restore the backup&q
}
}
catch
(Exception ex) {
ex.printStackTrace();
}
}
return false;
I have tested all the commands and the methods mentioned in this post and they worked fine in Windows
environment. That’s all about creating & restoring backups. Hope you guys learnt something new.
If you have any questions please post them as a comment & I’ll try my best to answer them. I’m backing up for
now then. See you guys with another post. Hopefully!
Update
https://omindu.wordpress.com/2011/06/27/mysql-database-backup-restore-using-java/
3/35
11/01/2019
MySQL Database Backup & Restore Using Java – Omindu's Blog
Make sure to add the path of your ‘MySQL
bin folder’
to the path variable in ‘Windows
Environment Variables’.
References:
http://sureshk37.wordpress.com/2009/09/07/mysql-backup-and-restore-using-java/
(http://sureshk37.wordpress.com/2009/09/07/mysql-backup-and-restore-using-java/)
http://forums.mysql.com (http://forums.mysql.com/)
Tagged:
Database Backup,
Database Restore,
JAVA,
MySQL
126 thoughts on “MySQL Database Backup & Restore
Using Java”
chy
says:
July 14, 2011 at 17:58
how do i backup a single table pls
Reply
Omindu
says:
July 14, 2011 at 20:39
u can try like this
mysqldump -u Username -pPassword databaseName --tables tbl1 tbl2 -r
backupPath
Reply
chy
says:
July 20, 2011 at 20:58
package files;
import java.util.*;
import java.io.*;
public class tableBackup_1 {
public boolean tbBackup(String dbName, String tbName, String dbUserName, String dbPassword, String path)
{
String executeCmd = “mysqldump -u ” + dbUserName + ” -p” + dbPassword +””
+ dbName + “–tables” + tbName + ” -r ” + path;
Process runtimeProcess;
try {
runtimeProcess = Runtime.getRuntime().exec(executeCmd);
int processComplete = runtimeProcess.waitFor();
if (processComplete == 0) {
System.out.println(“Backup created successfully”);
return true;
} else {
https://omindu.wordpress.com/2011/06/27/mysql-database-backup-restore-using-java/
4/35
11/01/2019
MySQL Database Backup & Restore Using Java – Omindu's Blog
System.out.println(“Could not create the backup”);
}
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}
public static void main(String[] args){
tableBackup_1 bb = new tableBackup_1();
bb.tbBackup(“nsetrans”,”price”, “root”, “sa”, “C:/New Folder/table.sql”);
}
}
******** not working pls. what am i doing wrong?
Reply
Omindu
says:
July 20, 2011 at 22:59
Replace your
executeCmd
with this:
String executeCmd = "mysqldump -u " + dbUserName + " -p" + dbPassword +
" "
+ dbName + " --tables " + tbName + " -r " + path;
And change your path like this:
"\"C:/New Folder/table.sql\""
when your folder name has a space, you should include the path within double quotations.
should work fine
Reply
chy
says:
July 21, 2011 at 14:41
thanks. it worked.
am really grateful.
Reply
Vibhakar
says:
February 21, 2012 at 03:57
Thanks Qmindu, Your code worked perfectly……..
Reply
Temur
says:
March 23, 2012 at 16:08
Thank you for answer, I have question,
in my computer problem with mysqldump
java.io.IOException: Cannot run program “mysqldump”: CreateProcess error=2, The system cannot find the
file specified
Reply
Omindu
says:
March 23, 2012 at 22:05
Check whether you have given the path to the dump file correctly. If it doesn’t solve your problem, post
your code here. I can check it for you if you like.
Reply
https://omindu.wordpress.com/2011/06/27/mysql-database-backup-restore-using-java/
5/35
Plik z chomika:
aolo23
Inne pliki z tego folderu:
JAVA 8 - przewodnik doświadczonego programisty.pdf
(114168 KB)
tablice-informatyczne-bootstrap-szymon-pendolski.pdf
(61 KB)
Zadanie ewaluacyjne na młodszego programistę JAVA (1).pdf
(503 KB)
SQL Od Podstaw [Helion][Paul Wilton, John Colby].pdf
(128974 KB)
wzorce projektowe. rusz głową!.pdf
(89977 KB)
Inne foldery tego chomika:
Semestr I
Semestr II
Semestr III
Semestr IV
Zgłoś jeśli
naruszono regulamin