Skip to content

Commit 27c4e52

Browse files
committed
initial creation of h2 backup plugin project
0 parents  commit 27c4e52

File tree

11 files changed

+180
-0
lines changed

11 files changed

+180
-0
lines changed

.gitignore

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
target/
2+
.idea/
3+
project/project/
4+
project/target/

README.MD

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# gitbucket-h2-backup-plugin
2+
3+
This plugin enhances [takezoe/gitbucket](https://github.com/takezoe/gitbucket) by offering a way to backup/dump the h2 database of gitbucket.
4+
5+
It originates from [pull request #845](takezoe/gitbucket#845) and can be used to adress database backup described in [gitbucket backup documentation](https://github.com/takezoe/gitbucket/wiki/Backup)
6+
7+
## Features
8+
9+
### H2 Backup
10+
11+
This plugin allows to backup the underlying H2 database used by default by gitbucket.
12+
13+
## Usage
14+
15+
The plugin provides 2 ways to backup the H2 database:
16+
17+
- via an administration page and a backup button
18+
- via an HTTP call to http://YOUR_GITBUCKET/database/backup
19+
20+
In current version database backup file is written to `GITBUCKET_HOME/.gitbucket/gitbucket-database-backup.zip`.
21+
22+
## Release Notes
23+
24+
### 1.0
25+
26+
- introduce gitbucket-h2-backup-plugin
27+
- allows to backup h2 database via a live dump

project/build.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 0.13.5

project/build.scala

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import sbt._
2+
import Keys._
3+
import play.twirl.sbt.SbtTwirl
4+
import play.twirl.sbt.Import.TwirlKeys._
5+
6+
object MyBuild extends Build {
7+
8+
val Organization = "fr.brouillard.gitbucket"
9+
val Name = "gitbucket-h2-backup-plugin"
10+
val Version = "1.0"
11+
val ScalaVersion = "2.11.6"
12+
13+
lazy val project = Project (
14+
"gitbucket-h2-backup-plugin",
15+
file(".")
16+
)
17+
.settings(
18+
sourcesInBase := false,
19+
organization := Organization,
20+
name := Name,
21+
version := Version,
22+
scalaVersion := ScalaVersion,
23+
scalacOptions := Seq("-deprecation", "-language:postfixOps"),
24+
resolvers ++= Seq(
25+
"amateras-repo" at "http://amateras.sourceforge.jp/mvn/"
26+
),
27+
libraryDependencies ++= Seq(
28+
"gitbucket" % "gitbucket-assembly" % "3.5.0" % "provided",
29+
"com.typesafe.play" %% "twirl-compiler" % "1.0.4" % "provided",
30+
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided"
31+
),
32+
javacOptions in compile ++= Seq("-target", "7", "-source", "7")
33+
).enablePlugins(SbtTwirl)
34+
}

project/plugins.sbt

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
logLevel := Level.Warn
2+
3+
addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.0.2")

sbt-launch-0.13.5.jar

1.13 MB
Binary file not shown.

sbt.bat

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
set SCRIPT_DIR=%~dp0
2+
java -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -Xss2M -jar "%SCRIPT_DIR%\sbt-launch-0.13.5.jar" %*

sbt.sh

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
java -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -Xss2M -jar `dirname $0`/sbt-launch-0.13.5.jar "$@"

src/main/scala/Plugin.scala

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import javax.servlet.ServletContext
2+
3+
import fr.brouillard.gitbucket.h2.controller.H2BackupController
4+
import gitbucket.core.plugin.PluginRegistry
5+
import gitbucket.core.service.SystemSettingsService.SystemSettings
6+
import gitbucket.core.util.Version
7+
8+
class Plugin extends gitbucket.core.plugin.Plugin {
9+
override val pluginId: String = "h2-backup"
10+
11+
override val pluginName: String = "H2 Database Backup Plugin"
12+
13+
override val description: String = "Allows to export h2 database of gitbucket"
14+
15+
override val versions: List[Version] = List(
16+
Version(1, 0)
17+
)
18+
19+
override def javaScripts(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(String, String)] = {
20+
// Add Snippet link to the header
21+
val path = settings.baseUrl.getOrElse(context.getContextPath)
22+
Seq(
23+
".*/admin/h2backup" -> s"""
24+
|$$('#system-admin-menu-container>li:last').after(
25+
| $$('<li class="active"><a href="${path}/admin/h2backup">H2 Backup</a></li>')
26+
|);
27+
""".stripMargin,
28+
".*/admin/(?!h2backup).*" -> s"""
29+
|$$('#system-admin-menu-container>li:last').after(
30+
| $$('<li><a href="${path}/admin/h2backup">H2 Backup</a></li>')
31+
|);
32+
""".stripMargin
33+
)
34+
}
35+
36+
override val controllers = Seq(
37+
"/admin/h2backup" -> new H2BackupController()
38+
, "/database/backup" -> new H2BackupController()
39+
)
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package fr.brouillard.gitbucket.h2.controller
2+
3+
import java.io.File
4+
5+
import gitbucket.core.controller.ControllerBase
6+
import gitbucket.core.servlet.Database
7+
import gitbucket.core.util.Directory._
8+
import fr.brouillard.gitbucket.h2._
9+
import org.scalatra.Ok
10+
import org.slf4j.LoggerFactory
11+
12+
class H2BackupController extends ControllerBase {
13+
private val logger = LoggerFactory.getLogger(classOf[H2BackupController])
14+
15+
def exportDatabase: Unit = {
16+
val session = Database.getSession(request)
17+
val conn = session.conn
18+
val exportFile = new File(GitBucketHome, "gitbucket-database-backup.zip")
19+
20+
logger.info("exporting database to {}", exportFile)
21+
22+
conn.prepareStatement("BACKUP TO '" + exportFile + "'").execute();
23+
}
24+
25+
get("/admin/h2backup") {
26+
html.export(flash.get("info"));
27+
}
28+
29+
get("/database/backup") {
30+
exportDatabase
31+
Ok("done")
32+
}
33+
34+
post("/database/backup") {
35+
exportDatabase
36+
flash += "info" -> "H2 Database has been exported."
37+
redirect("/admin/h2backup")
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
@(info: Option[Any])(implicit context: gitbucket.core.controller.Context)
2+
@import context._
3+
@import gitbucket.core.html.main
4+
@import gitbucket.core.admin.html.menu
5+
@import gitbucket.core.helper.html.information
6+
@import gitbucket.core.view.helpers._
7+
@import gitbucket.core.util.Directory._
8+
@main("H2 Backup"){
9+
@menu("h2backup"){
10+
@information(info)
11+
<form action="@path/database/backup" method="POST">
12+
<div class="box">
13+
<div class="box-header">Database export</div>
14+
<div class="box-content">
15+
<label><span class="strong">Database export file:</span>@GitBucketHome/gitbucket-database-backup.zip</label>
16+
<p class="muted">
17+
Allows to export the database content. The same action can be achieved via an HTTP GET call to @path/database/backup
18+
</p>
19+
20+
<fieldset>
21+
<input type="submit" class="btn btn-primary" value="Export database"/>
22+
</fieldset>
23+
</div>
24+
</div>
25+
</form>
26+
}
27+
}
28+

0 commit comments

Comments
 (0)