mirror of
https://github.com/gitbucket/gitbucket.git
synced 2026-01-07 08:02:14 +01:00
Database export tool is available
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
package gitbucket.core.controller
|
||||
|
||||
import java.io.FileInputStream
|
||||
|
||||
import gitbucket.core.admin.html
|
||||
import gitbucket.core.service.{AccountService, SystemSettingsService, RepositoryService}
|
||||
import gitbucket.core.util.AdminAuthenticator
|
||||
@@ -11,7 +13,7 @@ import gitbucket.core.util.ControlUtil._
|
||||
import gitbucket.core.util.Directory._
|
||||
import gitbucket.core.util.StringUtil._
|
||||
import io.github.gitbucket.scalatra.forms._
|
||||
import org.apache.commons.io.FileUtils
|
||||
import org.apache.commons.io.{IOUtils, FileUtils}
|
||||
import org.scalatra.i18n.Messages
|
||||
|
||||
class SystemSettingsController extends SystemSettingsControllerBase
|
||||
@@ -74,6 +76,7 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
|
||||
|
||||
case class PluginForm(pluginIds: List[String])
|
||||
|
||||
case class DataExportForm(tableNames: List[String])
|
||||
|
||||
case class NewUserForm(userName: String, password: String, fullName: String,
|
||||
mailAddress: String, isAdmin: Boolean,
|
||||
@@ -268,6 +271,29 @@ trait SystemSettingsControllerBase extends AccountManagementControllerBase {
|
||||
}
|
||||
})
|
||||
|
||||
get("/admin/data")(adminOnly {
|
||||
import gitbucket.core.util.JDBCUtil._
|
||||
val session = request2Session(request)
|
||||
html.data(session.conn.allTableNames())
|
||||
})
|
||||
|
||||
post("/admin/data")(adminOnly {
|
||||
import gitbucket.core.util.JDBCUtil._
|
||||
val session = request2Session(request)
|
||||
val file = session.conn.export(request.getParameterValues("tableNames").toSeq)
|
||||
|
||||
contentType = "application/octet-stream"
|
||||
response.setHeader("Content-Disposition", "attachment; filename=" + file.getName)
|
||||
response.setContentLength(file.length.toInt)
|
||||
|
||||
using(new FileInputStream(file)){ in =>
|
||||
IOUtils.copy(in, response.outputStream)
|
||||
}
|
||||
|
||||
()
|
||||
})
|
||||
|
||||
|
||||
private def members: Constraint = new Constraint(){
|
||||
override def validate(name: String, value: String, messages: Messages): Option[String] = {
|
||||
if(value.split(",").exists {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
package gitbucket.core.util
|
||||
|
||||
import java.io.FileOutputStream
|
||||
import java.io._
|
||||
import java.sql._
|
||||
import java.text.SimpleDateFormat
|
||||
import ControlUtil._
|
||||
@@ -60,18 +60,17 @@ object JDBCUtil {
|
||||
}
|
||||
}
|
||||
|
||||
def export(targetTables: Seq[String]): Unit = {
|
||||
def export(targetTables: Seq[String]): File = {
|
||||
val dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss")
|
||||
val file = File.createTempFile("gitbucket-export-", ".sql")
|
||||
|
||||
using(new FileOutputStream("export.sql")) { out =>
|
||||
using(new FileOutputStream(file)) { out =>
|
||||
val dbMeta = conn.getMetaData
|
||||
val allTablesInDatabase = allTablesOrderByDependencies(dbMeta)
|
||||
|
||||
allTablesInDatabase.foreach { tableName =>
|
||||
if (targetTables.contains(tableName)) {
|
||||
val sb = new StringBuilder()
|
||||
sb.append("DELETE FROM ACCOUNT WHERE USER_NAME = 'root';\n")
|
||||
|
||||
select(s"SELECT * FROM ${tableName}") { rs =>
|
||||
sb.append(s"INSERT INTO ${tableName} (")
|
||||
|
||||
@@ -107,6 +106,8 @@ object JDBCUtil {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
file
|
||||
}
|
||||
|
||||
def allTableNames(): Seq[String] = {
|
||||
|
||||
28
src/main/twirl/gitbucket/core/admin/data.scala.html
Normal file
28
src/main/twirl/gitbucket/core/admin/data.scala.html
Normal file
@@ -0,0 +1,28 @@
|
||||
@(tableNames: Seq[String])(implicit context: gitbucket.core.controller.Context)
|
||||
@import context._
|
||||
@import gitbucket.core.view.helpers._
|
||||
@html.main("Data export / import"){
|
||||
@admin.html.menu("data") {
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading strong">Export</div>
|
||||
<div class="panel-body">
|
||||
<form class="form form-horizontal" action="@path/admin/data" method="POST">
|
||||
@tableNames.map { tableName =>
|
||||
<div class="checkbox">
|
||||
<label>
|
||||
<input type="checkbox" name="tableNames" id="@tableName" value="@tableName" checked/> @tableName
|
||||
</label>
|
||||
</div>
|
||||
}
|
||||
<input type="submit" class="btn btn-success pull-right" value="Export">
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="panel panel-default">
|
||||
<div class="panel-heading strong">Import</div>
|
||||
<div class="panel-body">
|
||||
</div>
|
||||
</div>
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,9 @@
|
||||
<li@if(active=="plugins"){ class="active"}>
|
||||
<a href="@path/admin/plugins">Plugins</a>
|
||||
</li>
|
||||
<li@if(active=="data"){ class="active"}>
|
||||
<a href="@path/admin/data">Data export / import</a>
|
||||
</li>
|
||||
<li>
|
||||
<a href="@path/console/login.jsp">H2 Console</a>
|
||||
</li>
|
||||
|
||||
Reference in New Issue
Block a user