Java实现Sqlserver及MySql的备份与还原
注:本人是采用Struts1做的一个简单小例子。 实现步骤: 1.数据库基类
package com.wingo.util;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; /**
* @ClassName: DataBaseUtil * @Description: TODO
* @author 莫希柏
* @date Jul 4, 2012 2:21:41 PM */
public class DataBaseUtil { /**
* @Description: 获取数据库连接 * @author 莫希柏
* @date Jul 4, 2012 2:23:11 PM */
public static Connection getConnection() { Connection conn = null; try {
Class.forName(\); String url = \
DatabaseName=datatest\;
String username = \; String password = \;
conn = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); }
return conn; }
/**
* @Description: 关闭 * @author 莫希柏
* @date Jul 4, 2012 2:22:57 PM
*/
public static void closeConn(Connection conn) { if (conn != null) { try {
conn.close();
} catch (SQLException e) { e.printStackTrace(); } } } }
2.action类
package com.wingo.action;
import java.io.BufferedReader; import java.io.File;
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.OutputStream;
import java.io.OutputStreamWriter; import java.sql.CallableStatement; import java.sql.PreparedStatement;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionForward; import org.apache.struts.action.ActionMapping; import org.apache.struts.actions.DispatchAction;
import com.wingo.util.DataBaseUtil;
public class DataAction extends DispatchAction{ /**
* @Description: SqlServer备份 * @author 莫希柏
* @date Jul 4, 2012 2:45:16 PM */
public ActionForward doSqlServerBackUp(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws Exception { String mssqlBackupName=
request.getParameter(\);//自定义备份数据库名 String mssqlBackupPath=
request.getParameter(\);//自定义备份数据库保存路径
String dbName=\;//被备份的数据库名称 boolean flag=false; try {
File file = new File(mssqlBackupPath); String path = file.getPath() + \
+ mssqlBackupName + \;//备份生成的数据路径及文件名 String bakSql = \
+dbName+\;//备份数据库SQL语句
PreparedStatement bak = DataBaseUtil.getConnection() .prepareStatement(bakSql);
bak.setString(1, path);//path必须是绝对路径 bak.execute(); //备份数据库 bak.close(); flag=true;
} catch (Exception e) { flag=false;
e.printStackTrace(); }
response.setCharacterEncoding(\); try {
if(flag==true){
response.getWriter().print(
\
alert('SQLSERVER备份成功!');document.location.href='DataAction.do?action=toBackIndex';\);
}else{
response.getWriter().print(
\
alert('SQLSERVER备份失败!');document.location.href='DataAction.do?action=toBackIndex';\);
}
} catch (IOException e) {
e.printStackTrace(); }
return null; } /**
* @Description: SqlServer还原 * @author 莫希柏
* @date Jul 4, 2012 4:28:05 PM */
public ActionForward doSqlServerRecovery(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response){
boolean flag = false;
String mssqlRecoveryPath =
request.getParameter(\); //
被还原数据库文件的路径
String dbname=\;//数据库名称 try{
File file = new File(mssqlRecoveryPath);
String path = file.getPath();//数据库文件名 String recoverySql = \
+dbname+\ROLLBACK IMMEDIATE\;// 断开所有连接
PreparedStatement ps
= DataBaseUtil.getConnection()
.prepareStatement(recoverySql); CallableStatement cs
= DataBaseUtil.getConnection().prepareCall(\killrestore(?,?)}\); //调用存储过程
cs.setString(1, dbname); // 数据库名
cs.setString(2, path); // 已备份数据库所在路径 cs.execute(); // 还原数据库 ps.execute(); // 恢复数据库连接 flag=true;
} catch (Exception e) { flag=false;
e.printStackTrace(); }
response.setCharacterEncoding(\); try {
if(flag==true){
response.getWriter().print(
\
alert('SQLSERVER备份成功!');document.location.href='DataAction.do?action=toBackIndex';\);
}else{
response.getWriter().print(
\
alert('SQLSERVER备份失败!');document.location.href='DataAction.do?action=toBackIndex';\);
}
} catch (IOException e) { e.printStackTrace(); }
return null; } /**
* @Description: MySql备份 * @author 莫希柏
* @date Jul 4, 2012 4:39:02 PM */
public ActionForward doMySqlBackUp (ActionMapping mapping,
ActionForm form, HttpServletRequest request, HttpServletResponse response){
boolean flag=false; try {
Runtime rt = Runtime.getRuntime(); String mySqlBackupName
=request.getParameter(\);//mysql自定义数据库备份名称
String mysqlBackupPath
=request.getParameter(\);//mysql自定义数据库备份保存路径
String fPath=mysqlBackupPath+mySqlBackupName+\;
String command=\
5.0/bin/mysqldump -uroot -p123456 datatest\; //调用 mysql的cmd:
Process child = rt.exec(command);// 设置导出编码为utf8。这里必须是utf8
//把进程执行中的控制台输出信息写入.sql文件,即生成了备份文件。注:如果不对控制台信息进行读出,则会导致进程堵塞无法运行
InputStream in = child.getInputStream();// 控制台的输出信息作为输入流