java 备份mysql数据库

java 备份mysql数据库,第1张

备份MySQL数据库的方法:

import javaioFile;

import javaioIOException;

/

MySQL数据库备份

@author GaoHuanjie

/

public class MySQLDatabaseBackup {

/

Java代码实现MySQL数据库导出

@author GaoHuanjie

@param hostIP MySQL数据库所在服务器地址IP

@param userName 进入数据库所需要的用户名

@param password 进入数据库所需要的密码

@param savePath 数据库导出文件保存路径

@param fileName 数据库导出文件文件名

@param databaseName 要导出的数据库名

@return 返回true表示导出成功,否则返回false。

/

public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath, String fileName, String databaseName) {

File saveFile = new File(savePath);

if (!saveFileexists()) {// 如果目录不存在

saveFilemkdirs();// 创建文件夹

}

if (!savePathendsWith(Fileseparator)) {

savePath = savePath + Fileseparator;

}

StringBuilder stringBuilder = new StringBuilder();

stringBuilderappend("mysqldump")append(" --opt")append(" -h")append(hostIP);

stringBuilderappend(" --user=")append(userName) append(" --password=")append(password)append(" --lock-all-tables=true");

stringBuilderappend(" --result-file=")append(savePath + fileName)append(" --default-character-set=utf8 ")append(databaseName);

try {

Process process = RuntimegetRuntime()exec(stringBuildertoString());

if (processwaitFor() == 0) {// 0 表示线程正常终止。

return true;

}

} catch (IOException e) {

eprintStackTrace();

} catch (InterruptedException e) {

eprintStackTrace();

}

return false;

}

public static void main(String[] args) throws InterruptedException {

if (exportDatabaseTool("172160127", "root", "123456", "D:/backupDatabase", "2014-10-14sql", "test")) {

Systemoutprintln("数据库备份成功!!!");

} else {

Systemoutprintln("数据库备份失败!!!");

}

}

}

还原数据库string strSql = @"RESTORE DATABASE " + 数据库名+ " FROM DISK = '" + 路径 + @"xxxxbak' ";备份数据库string strSql = @"Backup Database " + 数据库名 + " To disk = ' + 路径 + "xxxbak' "; 执行语句SqlConnecton sqlCon = new SqlConnection(连接字符串);sqlConOpen();SqlCommand sqlCmd = new SqlCommand(strSql,sqlCon);sqlCmdExecuteNonQuery();sqlConClose();

停掉数据库服务器。

将刚才生成的数据库的日志文件_logldf删除,用要恢复的数据库mdf(mdf)文件覆盖刚才生成的数据库数据文件mdf。

启动数据库服务器。

restore

database

DemoDB

from

disk='c:\abak'

--c:\abak是备份文件名

with

move

'demodb_data01'

to

'\demodb_data01mdf'

--将要恢复到文件

,move

,move

'demodb_log'

to

'\demodb_logldf'

--------------------------------------------------------------------------------

<form name="form1" method="post" action="action=bf">

<table width="90%" border="0" align=center cellpadding="5" cellspacing="1" bgcolor="#336699" class="tableBorder">

<tr>

<th width="593" height=25 bgcolor="#FFFFFF" >   <B>备份数据</B>( 需要FSO支持,FSO相关帮助请看微软网站 ) </th>

</tr>

<tr>

<td height=100 bgcolor="#FFFFFF" class="forumrow">   

<span class="STYLE1">

<%

if requestQueryString("action")="bf" then

Dbpath=requestform("Dbpath")

backpath=requestform("backpath")&"mdb"

if dbpath="" then

responsewrite "请输入您要您要备份的数据库"

else

Dbpath=servermappath(Dbpath)

end if

backpath=servermappath(backpath)

Set Fso=servercreateobject("scriptingfilesystemobject")

if fsofileexists(dbpath) then

fsocopyfile Dbpath,Backpath

responsewrite "数据备份成功!"

else

responsewrite "数据库地址不存在,请检查CONNASP中的DB=这一项是否是绝对地址!"

end if

end if%>

</span> <div align="center">    

<p>备份数据库路径(相对):

<input name=backpath type=text id="backpath" value="bak\jester_bak" size=30>

<br>

目标数据库路径(相对):

<input name=DBpath type=text id="DBpath" value="<%=db%>" size=30>

<BR>

  <BR>

  

<input name="submit" type=submit value="备份数据">

</p>

<p>必须是绝对的数据库地址<br>

</p>

</div></td>

</tr>

</table>

<p>  </p>

</form>

<form name="form1" method="post" action="action=hy">

<font color=red class="STYLE1">

<%

if requestQueryString("action")="hy" then

Dbpath=requestform("Dbpath")

backpath=requestform("backpath")

if dbpath="" then

responsewrite "请输入您要恢复成的数据库全名"

else

Dbpath=servermappath(Dbpath)

end if

backpath=servermappath(backpath)

Set Fso=servercreateobject("scriptingfilesystemobject")

if fsofileexists(dbpath) then

fsocopyfile Dbpath,Backpath

responsewrite "成功恢复数据!"

else

responsewrite "备份目录下并无您的备份文件!"

end if

end if%>

</font> <table width="80%" height="1" border="0" align=center cellpadding="5" cellspacing="1" bgcolor="#336699" class="tableBorder">

<tr>

<th width="593" height=25 bgcolor="#FFFFFF" >   <B>恢复论坛数据</B>( 需要FSO支持,FSO相关帮助请看微软网站 ) </th>

</tr>

<tr>

<td height=100 bgcolor="#FFFFFF" class="forumrow">   备份数据库路径(相对):

<input type=text size=30 name=DBpath value="bak\jester_bakMdb">

  <BR>

  目标数据库路径(相对):

<input name=backpath type=text id="backpath" value="<%=db%>" size=30>

<BR>

  填写您当前使用的数据库路径,如不想覆盖当前文件,可自行命名(注意路径是否正确),然后修改connasp文件, 如果目标文件名和当前使用数据库名一致的话,不需修改connasp文件<BR>

  

<input name="submit" type=submit value="恢复数据">

<br>

-----------------------------------------------------------------------------------------<br>

  在上面填写本程序的数据库路径全名,本程序的默认备份数据库文件为,请按照您的备份文件自行修改。<br>

  您可以用这个功能来备份您的法规数据,以保证您的数据安全!<br>

  注意:所有路径都是相对与程序空间根目录的相对路径 </td>

</tr>

</table>

<p> </p>

</form>

<span class="STYLE1">

<%

if requestQueryString("action")="ys" then

dim dbpath,boolIs97

if request("Dbpath")<>"" then Dbpath=request("Dbpath") end if

if request("Dbpath")="" then

if request("bkfolder")<>"" then bkfolder=request("bkfolder") else bkfolder="spubbsbak" end if

if request("bkdbname")<>"" then bkdbname=request("bkdbname") else bkdbname="spubbs" end if

bkdbname=bkdbname&"#asp"

Dbpath=bkfolder&"\"&bkdbname

end if

'dbpath = request("dbpath")

if request("boolIs97")<>"" then boolIs97=request("boolIs97") else boolIs97=true end if

'boolIs97 = request("boolIs97")

If dbpath <> "" Then

dbpath = servermappath(dbpath)

responsewrite(CompactDB(dbpath,boolIs97))

End If

'=====================压缩参数=========================

Function CompactDB(dbPath, boolIs97)

Dim fso, Engine, strDBPath,JET_3X

strDBPath = left(dbPath,instrrev(DBPath,"\"))

Set fso = CreateObject("ScriptingFileSystemObject")

If fsoFileExists(dbPath) Then

fsoCopyFile dbpath,strDBPath & "tempmdb"

Set Engine = CreateObject("JROJetEngine")

If boolIs97 = "True" Then

EngineCompactDatabase "Provider=MicrosoftJetOLEDB40;Data Source=" & strDBPath & "tempmdb", _

"Provider=MicrosoftJetOLEDB40;Data Source=" & strDBPath & "temp1mdb;" _

& "Jet OLEDB:Engine Type=" & JET_3X

Else

EngineCompactDatabase "Provider=MicrosoftJetOLEDB40;Data Source=" & strDBPath & "tempmdb", _

"Provider=MicrosoftJetOLEDB40;Data Source=" & strDBPath & "temp1mdb"

End If

fsoCopyFile strDBPath & "temp1mdb",dbpath

fsoDeleteFile(strDBPath & "tempmdb")

fsoDeleteFile(strDBPath & "temp1mdb")

Set fso = nothing

Set Engine = nothing

CompactDB = "你的数据库, " & dbpath & ", 已经压缩成功!" & vbCrLf

Else

CompactDB = "数据库名称或路径不正确 请重试!" & vbCrLf

End If

End Function

end if%>

</span>

<form action="action=ys" method="post">

<table width="80%" height="1" border="0" align=center cellpadding="5" cellspacing="1" bgcolor="#336699" class="tableBorder">

<tr>

<td width="581" height=25 bgcolor="#FFFFFF" class="forumrow"><b>注意:</b><br>

输入数据库所在相对路径,并且输入数据库名称(正在使用中数据库不能压缩,请选择备份数据库进行压缩 *** 作) </td>

</tr>

<tr>

<td bgcolor="#FFFFFF" class="forumrow">压缩数据库:

<input type="text" name="dbpath" value=<%=db%>>

 

<input name="submit3" type="submit" value="开始压缩"></td>

</tr>

<tr>

<td bgcolor="#FFFFFF" class="forumrow"><input type="checkbox" name="boolIs97" value="True">

如果使用 Access 97 数据库请选择 (默认为 Access 2000 数据库)<br>

<br></td>

</tr>

</table> </form>

以上就是关于java 备份mysql数据库全部的内容,包括:java 备份mysql数据库、在C#中怎样用代码将MSSQLServer数据库备份,还原以及怎样创建发布和订阅数据库,实现数据库异地同步、SQL Server 2005 与VS2005编程语言C# winform实现数据库备份与恢复。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/sjk/10131125.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-05
下一篇2023-05-05

发表评论

登录后才能评论

评论列表(0条)

    保存