Fix stuff

This commit is contained in:
Kim "BKC" Carlbäcker 2017-04-28 21:36:35 +02:00
parent 77ef39112d
commit b0c7a3bb5c
2 changed files with 31 additions and 17 deletions

View File

@ -70,7 +70,10 @@ var Backup = cli.Command{
}, },
} }
const archiveRootDir = "gitea-backup" const (
archiveRootDir = "gitea-backup"
backupVersion = 1
)
func runBackup(c *cli.Context) error { func runBackup(c *cli.Context) error {
zip.Verbose = c.Bool("verbose") zip.Verbose = c.Bool("verbose")
@ -100,7 +103,7 @@ func runBackup(c *cli.Context) error {
// Metadata // Metadata
metaFile := path.Join(rootDir, "metadata.ini") metaFile := path.Join(rootDir, "metadata.ini")
metadata := ini.Empty() metadata := ini.Empty()
metadata.Section("").Key("VERSION").SetValue("1") metadata.Section("").Key("VERSION").SetValue(fmt.Sprintf("%d", backupVersion))
metadata.Section("").Key("DATE_TIME").SetValue(time.Now().String()) metadata.Section("").Key("DATE_TIME").SetValue(time.Now().String())
metadata.Section("").Key("GITEA_VERSION").SetValue(setting.AppVer) metadata.Section("").Key("GITEA_VERSION").SetValue(setting.AppVer)
if err = metadata.SaveTo(metaFile); err != nil { if err = metadata.SaveTo(metaFile); err != nil {

View File

@ -19,6 +19,7 @@ import (
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
) )
// Restore a backup
var Restore = cli.Command{ var Restore = cli.Command{
Name: "restore", Name: "restore",
Usage: "Restore files and database from backup", Usage: "Restore files and database from backup",
@ -49,17 +50,21 @@ be skipped and remian unchanged.`,
Name: "verbose, v", Name: "verbose, v",
Usage: "Show process details", Usage: "Show process details",
}, },
cli.BoolFlag{ cli.BoolTFlag{
Name: "no-repos", Name: "repos",
Usage: "Don't restore repositiries", Usage: "Restore repositories (default: true)",
}, },
cli.BoolFlag{ cli.BoolTFlag{
Name: "no-metadata", Name: "data",
Usage: "Don't restore metadata (such as attachments and avatars)", Usage: "Restore attachments and avatars (default: true)",
}, },
cli.BoolFlag{ cli.BoolTFlag{
Name: "no-custom", Name: "custom",
Usage: "Don't restore custom files", Usage: "Restore custom files (default: true)",
},
cli.BoolTFlag{
Name: "db",
Usage: "Restore database (default: true)",
}, },
}, },
} }
@ -87,6 +92,10 @@ func runRestore(c *cli.Context) error {
if err != nil { if err != nil {
log.Fatal(0, "Fail to load metadata '%s': %v", metaFile, err) log.Fatal(0, "Fail to load metadata '%s': %v", metaFile, err)
} }
ver := metadata.Section("").Key("VERSION").MustInt(10000000)
if ver != backupVersion {
log.Fatal(0, "Current Backup version does not match the version in the backup: %d != %d", ver, backupVersion)
}
backupVersion := metadata.Section("").Key("GITEA_VERSION").MustString("999.0") backupVersion := metadata.Section("").Key("GITEA_VERSION").MustString("999.0")
if version.Compare(setting.AppVer, backupVersion, "<") { if version.Compare(setting.AppVer, backupVersion, "<") {
log.Fatal(0, "Current Gitea version is lower than backup version: %s < %s", setting.AppVer, backupVersion) log.Fatal(0, "Current Gitea version is lower than backup version: %s < %s", setting.AppVer, backupVersion)
@ -107,13 +116,15 @@ func runRestore(c *cli.Context) error {
models.SetEngine() models.SetEngine()
// Database // Database
dbDir := path.Join(archivePath, "db") if c.Bool("db") {
if err = models.ImportDatabase(dbDir); err != nil { dbDir := path.Join(archivePath, "db")
log.Fatal(0, "Fail to import database: %v", err) if err = models.ImportDatabase(dbDir); err != nil {
log.Fatal(0, "Fail to import database: %v", err)
}
} }
// Custom files // Custom files
if !c.Bool("no-custom") { if c.Bool("custom") {
if com.IsExist(setting.CustomPath) { if com.IsExist(setting.CustomPath) {
if err = os.Rename(setting.CustomPath, setting.CustomPath+".bak"); err != nil { if err = os.Rename(setting.CustomPath, setting.CustomPath+".bak"); err != nil {
log.Fatal(0, "Fail to backup current 'custom': %v", err) log.Fatal(0, "Fail to backup current 'custom': %v", err)
@ -125,7 +136,7 @@ func runRestore(c *cli.Context) error {
} }
// Data files // Data files
if !c.Bool("no-metadata") { if c.Bool("data") {
for _, dir := range []string{"attachments", "avatars"} { for _, dir := range []string{"attachments", "avatars"} {
dirPath := path.Join(setting.AppDataPath, dir) dirPath := path.Join(setting.AppDataPath, dir)
if com.IsExist(dirPath) { if com.IsExist(dirPath) {
@ -140,7 +151,7 @@ func runRestore(c *cli.Context) error {
} }
// Repositories // Repositories
if !c.Bool("no-repos") { if c.Bool("repos") {
reposPath := path.Join(archivePath, "repositories.zip") reposPath := path.Join(archivePath, "repositories.zip")
if !c.Bool("exclude-repos") && !c.Bool("database-only") && com.IsExist(reposPath) { if !c.Bool("exclude-repos") && !c.Bool("database-only") && com.IsExist(reposPath) {
if err := zip.ExtractTo(reposPath, path.Dir(setting.RepoRootPath)); err != nil { if err := zip.ExtractTo(reposPath, path.Dir(setting.RepoRootPath)); err != nil {