Skip to content

Commit

Permalink
initial creation of h2 backup plugin project
Browse files Browse the repository at this point in the history
  • Loading branch information
McFoggy committed Sep 1, 2015
0 parents commit 27c4e52
Show file tree
Hide file tree
Showing 11 changed files with 180 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
target/
.idea/
project/project/
project/target/
27 changes: 27 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# gitbucket-h2-backup-plugin

This plugin enhances [takezoe/gitbucket](https://github.com/takezoe/gitbucket) by offering a way to backup/dump the h2 database of gitbucket.

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)

## Features

### H2 Backup

This plugin allows to backup the underlying H2 database used by default by gitbucket.

## Usage

The plugin provides 2 ways to backup the H2 database:

- via an administration page and a backup button
- via an HTTP call to http://YOUR_GITBUCKET/database/backup

In current version database backup file is written to `GITBUCKET_HOME/.gitbucket/gitbucket-database-backup.zip`.

## Release Notes

### 1.0

- introduce gitbucket-h2-backup-plugin
- allows to backup h2 database via a live dump
1 change: 1 addition & 0 deletions project/build.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sbt.version = 0.13.5
34 changes: 34 additions & 0 deletions project/build.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import sbt._
import Keys._
import play.twirl.sbt.SbtTwirl
import play.twirl.sbt.Import.TwirlKeys._

object MyBuild extends Build {

val Organization = "fr.brouillard.gitbucket"
val Name = "gitbucket-h2-backup-plugin"
val Version = "1.0"
val ScalaVersion = "2.11.6"

lazy val project = Project (
"gitbucket-h2-backup-plugin",
file(".")
)
.settings(
sourcesInBase := false,
organization := Organization,
name := Name,
version := Version,
scalaVersion := ScalaVersion,
scalacOptions := Seq("-deprecation", "-language:postfixOps"),
resolvers ++= Seq(
"amateras-repo" at "http://amateras.sourceforge.jp/mvn/"
),
libraryDependencies ++= Seq(
"gitbucket" % "gitbucket-assembly" % "3.5.0" % "provided",
"com.typesafe.play" %% "twirl-compiler" % "1.0.4" % "provided",
"javax.servlet" % "javax.servlet-api" % "3.1.0" % "provided"
),
javacOptions in compile ++= Seq("-target", "7", "-source", "7")
).enablePlugins(SbtTwirl)
}
3 changes: 3 additions & 0 deletions project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
logLevel := Level.Warn

addSbtPlugin("com.typesafe.sbt" % "sbt-twirl" % "1.0.2")
Binary file added sbt-launch-0.13.5.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions sbt.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
set SCRIPT_DIR=%~dp0
java -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -Xss2M -jar "%SCRIPT_DIR%\sbt-launch-0.13.5.jar" %*
2 changes: 2 additions & 0 deletions sbt.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/sh
java -Dsbt.log.noformat=true -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256m -Xmx512M -Xss2M -jar `dirname $0`/sbt-launch-0.13.5.jar "$@"
40 changes: 40 additions & 0 deletions src/main/scala/Plugin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import javax.servlet.ServletContext

import fr.brouillard.gitbucket.h2.controller.H2BackupController
import gitbucket.core.plugin.PluginRegistry
import gitbucket.core.service.SystemSettingsService.SystemSettings
import gitbucket.core.util.Version

class Plugin extends gitbucket.core.plugin.Plugin {
override val pluginId: String = "h2-backup"

override val pluginName: String = "H2 Database Backup Plugin"

override val description: String = "Allows to export h2 database of gitbucket"

override val versions: List[Version] = List(
Version(1, 0)
)

override def javaScripts(registry: PluginRegistry, context: ServletContext, settings: SystemSettings): Seq[(String, String)] = {
// Add Snippet link to the header
val path = settings.baseUrl.getOrElse(context.getContextPath)
Seq(
".*/admin/h2backup" -> s"""
|$$('#system-admin-menu-container>li:last').after(
| $$('<li class="active"><a href="${path}/admin/h2backup">H2 Backup</a></li>')
|);
""".stripMargin,
".*/admin/(?!h2backup).*" -> s"""
|$$('#system-admin-menu-container>li:last').after(
| $$('<li><a href="${path}/admin/h2backup">H2 Backup</a></li>')
|);
""".stripMargin
)
}

override val controllers = Seq(
"/admin/h2backup" -> new H2BackupController()
, "/database/backup" -> new H2BackupController()
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fr.brouillard.gitbucket.h2.controller

import java.io.File

import gitbucket.core.controller.ControllerBase
import gitbucket.core.servlet.Database
import gitbucket.core.util.Directory._
import fr.brouillard.gitbucket.h2._
import org.scalatra.Ok
import org.slf4j.LoggerFactory

class H2BackupController extends ControllerBase {
private val logger = LoggerFactory.getLogger(classOf[H2BackupController])

def exportDatabase: Unit = {
val session = Database.getSession(request)
val conn = session.conn
val exportFile = new File(GitBucketHome, "gitbucket-database-backup.zip")

logger.info("exporting database to {}", exportFile)

conn.prepareStatement("BACKUP TO '" + exportFile + "'").execute();
}

get("/admin/h2backup") {
html.export(flash.get("info"));
}

get("/database/backup") {
exportDatabase
Ok("done")
}

post("/database/backup") {
exportDatabase
flash += "info" -> "H2 Database has been exported."
redirect("/admin/h2backup")
}
}
28 changes: 28 additions & 0 deletions src/main/twirl/fr/brouillard/gitbucket/h2/export.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
@(info: Option[Any])(implicit context: gitbucket.core.controller.Context)
@import context._
@import gitbucket.core.html.main
@import gitbucket.core.admin.html.menu
@import gitbucket.core.helper.html.information
@import gitbucket.core.view.helpers._
@import gitbucket.core.util.Directory._
@main("H2 Backup"){
@menu("h2backup"){
@information(info)
<form action="@path/database/backup" method="POST">
<div class="box">
<div class="box-header">Database export</div>
<div class="box-content">
<label><span class="strong">Database export file:</span>@GitBucketHome/gitbucket-database-backup.zip</label>
<p class="muted">
Allows to export the database content. The same action can be achieved via an HTTP GET call to @path/database/backup
</p>

<fieldset>
<input type="submit" class="btn btn-primary" value="Export database"/>
</fieldset>
</div>
</div>
</form>
}
}

0 comments on commit 27c4e52

Please sign in to comment.