restore.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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/Unknwon/cae/zip"
  9. "github.com/Unknwon/com"
  10. "github.com/mcuadros/go-version"
  11. "github.com/urfave/cli"
  12. log "gopkg.in/clog.v1"
  13. "gopkg.in/ini.v1"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/pkg/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. func runRestore(c *cli.Context) error {
  36. zip.Verbose = c.Bool("verbose")
  37. tmpDir := c.String("tempdir")
  38. if !com.IsExist(tmpDir) {
  39. log.Fatal(0, "'--tempdir' does not exist: %s", tmpDir)
  40. }
  41. log.Info("Restore backup from: %s", c.String("from"))
  42. if err := zip.ExtractTo(c.String("from"), tmpDir); err != nil {
  43. log.Fatal(0, "Fail to extract backup archive: %v", err)
  44. }
  45. archivePath := path.Join(tmpDir, _ARCHIVE_ROOT_DIR)
  46. // Check backup version
  47. metaFile := path.Join(archivePath, "metadata.ini")
  48. if !com.IsExist(metaFile) {
  49. log.Fatal(0, "File 'metadata.ini' is missing")
  50. }
  51. metadata, err := ini.Load(metaFile)
  52. if err != nil {
  53. log.Fatal(0, "Fail to load metadata '%s': %v", metaFile, err)
  54. }
  55. backupVersion := metadata.Section("").Key("GOGS_VERSION").MustString("999.0")
  56. if version.Compare(setting.AppVer, backupVersion, "<") {
  57. log.Fatal(0, "Current Gogs version is lower than backup version: %s < %s", setting.AppVer, backupVersion)
  58. }
  59. // If config file is not present in backup, user must set this file via flag.
  60. // Otherwise, it's optional to set config file flag.
  61. configFile := path.Join(archivePath, "custom/conf/app.ini")
  62. if c.IsSet("config") {
  63. setting.CustomConf = c.String("config")
  64. } else if !com.IsExist(configFile) {
  65. log.Fatal(0, "'--config' is not specified and custom config file is not found in backup")
  66. } else {
  67. setting.CustomConf = configFile
  68. }
  69. setting.NewContext()
  70. models.LoadConfigs()
  71. models.SetEngine()
  72. // Database
  73. dbDir := path.Join(archivePath, "db")
  74. if err = models.ImportDatabase(dbDir); err != nil {
  75. log.Fatal(0, "Fail to import database: %v", err)
  76. }
  77. // Custom files
  78. if !c.Bool("database-only") {
  79. if com.IsExist(setting.CustomPath) {
  80. if err = os.Rename(setting.CustomPath, setting.CustomPath+".bak"); err != nil {
  81. log.Fatal(0, "Fail to backup current 'custom': %v", err)
  82. }
  83. }
  84. if err = os.Rename(path.Join(archivePath, "custom"), setting.CustomPath); err != nil {
  85. log.Fatal(0, "Fail to import 'custom': %v", err)
  86. }
  87. }
  88. // Data files
  89. if !c.Bool("database-only") {
  90. for _, dir := range []string{"attachments", "avatars"} {
  91. dirPath := path.Join(setting.AppDataPath, dir)
  92. if com.IsExist(dirPath) {
  93. if err = os.Rename(dirPath, dirPath+".bak"); err != nil {
  94. log.Fatal(0, "Fail to backup current 'data': %v", err)
  95. }
  96. }
  97. if err = os.Rename(path.Join(archivePath, "data", dir), dirPath); err != nil {
  98. log.Fatal(0, "Fail to import 'data': %v", err)
  99. }
  100. }
  101. }
  102. // Repositories
  103. reposPath := path.Join(archivePath, "repositories.zip")
  104. if !c.Bool("exclude-repos") && !c.Bool("database-only") && com.IsExist(reposPath) {
  105. if err := zip.ExtractTo(reposPath, path.Dir(setting.RepoRootPath)); err != nil {
  106. log.Fatal(0, "Fail to extract 'repositories.zip': %v", err)
  107. }
  108. }
  109. os.RemoveAll(path.Join(tmpDir, _ARCHIVE_ROOT_DIR))
  110. log.Info("Restore succeed!")
  111. log.Shutdown()
  112. return nil
  113. }