restore.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // Copyright 2017 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cmd
  5. import (
  6. "os"
  7. "path"
  8. "github.com/mcuadros/go-version"
  9. "github.com/unknwon/cae/zip"
  10. "github.com/unknwon/com"
  11. "github.com/urfave/cli"
  12. log "gopkg.in/clog.v1"
  13. "gopkg.in/ini.v1"
  14. "gogs.io/gogs/internal/db"
  15. "gogs.io/gogs/internal/setting"
  16. )
  17. var Restore = cli.Command{
  18. Name: "restore",
  19. Usage: "Restore files and database from backup",
  20. Description: `Restore imports all related files and database from a backup archive.
  21. The backup version must lower or equal to current Gogs version. You can also import
  22. backup from other database engines, which is useful for database migrating.
  23. If corresponding files or database tables are not presented in the archive, they will
  24. be skipped and remain unchanged.`,
  25. Action: runRestore,
  26. Flags: []cli.Flag{
  27. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  28. boolFlag("verbose, v", "Show process details"),
  29. stringFlag("tempdir, t", os.TempDir(), "Temporary directory path"),
  30. stringFlag("from", "", "Path to backup archive"),
  31. boolFlag("database-only", "Only import database"),
  32. boolFlag("exclude-repos", "Exclude repositories"),
  33. },
  34. }
  35. // lastSupportedVersionOfFormat returns the last supported version of the backup archive
  36. // format that is able to import.
  37. var lastSupportedVersionOfFormat = map[int]string{}
  38. func runRestore(c *cli.Context) error {
  39. zip.Verbose = c.Bool("verbose")
  40. tmpDir := c.String("tempdir")
  41. if !com.IsExist(tmpDir) {
  42. log.Fatal(0, "'--tempdir' does not exist: %s", tmpDir)
  43. }
  44. log.Info("Restore backup from: %s", c.String("from"))
  45. if err := zip.ExtractTo(c.String("from"), tmpDir); err != nil {
  46. log.Fatal(0, "Failed to extract backup archive: %v", err)
  47. }
  48. archivePath := path.Join(tmpDir, _ARCHIVE_ROOT_DIR)
  49. defer os.RemoveAll(archivePath)
  50. // Check backup version
  51. metaFile := path.Join(archivePath, "metadata.ini")
  52. if !com.IsExist(metaFile) {
  53. log.Fatal(0, "File 'metadata.ini' is missing")
  54. }
  55. metadata, err := ini.Load(metaFile)
  56. if err != nil {
  57. log.Fatal(0, "Failed to load metadata '%s': %v", metaFile, err)
  58. }
  59. backupVersion := metadata.Section("").Key("GOGS_VERSION").MustString("999.0")
  60. if version.Compare(setting.AppVer, backupVersion, "<") {
  61. log.Fatal(0, "Current Gogs version is lower than backup version: %s < %s", setting.AppVer, backupVersion)
  62. }
  63. formatVersion := metadata.Section("").Key("VERSION").MustInt()
  64. if formatVersion == 0 {
  65. log.Fatal(0, "Failed to determine the backup format version from metadata '%s': %s", metaFile, "VERSION is not presented")
  66. }
  67. if formatVersion != _CURRENT_BACKUP_FORMAT_VERSION {
  68. log.Fatal(0, "Backup format version found is %d but this binary only supports %d\nThe last known version that is able to import your backup is %s",
  69. formatVersion, _CURRENT_BACKUP_FORMAT_VERSION, lastSupportedVersionOfFormat[formatVersion])
  70. }
  71. // If config file is not present in backup, user must set this file via flag.
  72. // Otherwise, it's optional to set config file flag.
  73. configFile := path.Join(archivePath, "custom/conf/app.ini")
  74. if c.IsSet("config") {
  75. setting.CustomConf = c.String("config")
  76. } else if !com.IsExist(configFile) {
  77. log.Fatal(0, "'--config' is not specified and custom config file is not found in backup")
  78. } else {
  79. setting.CustomConf = configFile
  80. }
  81. setting.NewContext()
  82. db.LoadConfigs()
  83. db.SetEngine()
  84. // Database
  85. dbDir := path.Join(archivePath, "db")
  86. if err = db.ImportDatabase(dbDir, c.Bool("verbose")); err != nil {
  87. log.Fatal(0, "Failed to import database: %v", err)
  88. }
  89. // Custom files
  90. if !c.Bool("database-only") {
  91. if com.IsExist(setting.CustomPath) {
  92. if err = os.Rename(setting.CustomPath, setting.CustomPath+".bak"); err != nil {
  93. log.Fatal(0, "Failed to backup current 'custom': %v", err)
  94. }
  95. }
  96. if err = os.Rename(path.Join(archivePath, "custom"), setting.CustomPath); err != nil {
  97. log.Fatal(0, "Failed to import 'custom': %v", err)
  98. }
  99. }
  100. // Data files
  101. if !c.Bool("database-only") {
  102. os.MkdirAll(setting.AppDataPath, os.ModePerm)
  103. for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
  104. // Skip if backup archive does not have corresponding data
  105. srcPath := path.Join(archivePath, "data", dir)
  106. if !com.IsDir(srcPath) {
  107. continue
  108. }
  109. dirPath := path.Join(setting.AppDataPath, dir)
  110. if com.IsExist(dirPath) {
  111. if err = os.Rename(dirPath, dirPath+".bak"); err != nil {
  112. log.Fatal(0, "Failed to backup current 'data': %v", err)
  113. }
  114. }
  115. if err = os.Rename(srcPath, dirPath); err != nil {
  116. log.Fatal(0, "Failed to import 'data': %v", err)
  117. }
  118. }
  119. }
  120. // Repositories
  121. reposPath := path.Join(archivePath, "repositories.zip")
  122. if !c.Bool("exclude-repos") && !c.Bool("database-only") && com.IsExist(reposPath) {
  123. if err := zip.ExtractTo(reposPath, path.Dir(setting.RepoRootPath)); err != nil {
  124. log.Fatal(0, "Failed to extract 'repositories.zip': %v", err)
  125. }
  126. }
  127. log.Info("Restore succeed!")
  128. log.Shutdown()
  129. return nil
  130. }