backup.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. "fmt"
  7. "io/ioutil"
  8. "os"
  9. "path"
  10. "time"
  11. "github.com/Unknwon/cae/zip"
  12. "github.com/Unknwon/com"
  13. "github.com/urfave/cli"
  14. log "gopkg.in/clog.v1"
  15. "gopkg.in/ini.v1"
  16. "github.com/gogits/gogs/models"
  17. "github.com/gogits/gogs/modules/setting"
  18. )
  19. var Backup = cli.Command{
  20. Name: "backup",
  21. Usage: "Backup files and database",
  22. Description: `Backup dumps and compresses all related files and database into zip file,
  23. which can be used for migrating Gogs to another server. The output format is meant to be
  24. portable among all supported database engines.`,
  25. Action: runBackup,
  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("target", "./", "Target directory path to save backup archive"),
  31. boolFlag("database-only", "Only dump database"),
  32. boolFlag("exclude-repos", "Exclude repositories"),
  33. },
  34. }
  35. const _ARCHIVE_ROOT_DIR = "gogs-backup"
  36. func runBackup(c *cli.Context) error {
  37. zip.Verbose = c.Bool("verbose")
  38. if c.IsSet("config") {
  39. setting.CustomConf = c.String("config")
  40. }
  41. setting.NewContext()
  42. models.LoadConfigs()
  43. models.SetEngine()
  44. tmpDir := c.String("tempdir")
  45. if !com.IsExist(tmpDir) {
  46. log.Fatal(0, "'--tempdir' does not exist: %s", tmpDir)
  47. }
  48. rootDir, err := ioutil.TempDir(tmpDir, "gogs-backup-")
  49. if err != nil {
  50. log.Fatal(0, "Fail to create backup root directory '%s': %v", rootDir, err)
  51. }
  52. log.Info("Backup root directory: %s", rootDir)
  53. // Metadata
  54. metaFile := path.Join(rootDir, "metadata.ini")
  55. metadata := ini.Empty()
  56. metadata.Section("").Key("VERSION").SetValue("1")
  57. metadata.Section("").Key("DATE_TIME").SetValue(time.Now().String())
  58. metadata.Section("").Key("GOGS_VERSION").SetValue(setting.AppVer)
  59. if err = metadata.SaveTo(metaFile); err != nil {
  60. log.Fatal(0, "Fail to save metadata '%s': %v", metaFile, err)
  61. }
  62. archiveName := path.Join(c.String("target"), fmt.Sprintf("gogs-backup-%d.zip", time.Now().Unix()))
  63. log.Info("Packing backup files to: %s", archiveName)
  64. z, err := zip.Create(archiveName)
  65. if err != nil {
  66. log.Fatal(0, "Fail to create backup archive '%s': %v", archiveName, err)
  67. }
  68. if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/metadata.ini", metaFile); err != nil {
  69. log.Fatal(0, "Fail to include 'metadata.ini': %v", err)
  70. }
  71. // Database
  72. dbDir := path.Join(rootDir, "db")
  73. if err = models.DumpDatabase(dbDir); err != nil {
  74. log.Fatal(0, "Fail to dump database: %v", err)
  75. }
  76. if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/db", dbDir); err != nil {
  77. log.Fatal(0, "Fail to include 'db': %v", err)
  78. }
  79. // Custom files
  80. if !c.Bool("database-only") {
  81. if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/custom", setting.CustomPath); err != nil {
  82. log.Fatal(0, "Fail to include 'custom': %v", err)
  83. }
  84. }
  85. // Data files
  86. if !c.Bool("database-only") {
  87. for _, dir := range []string{"attachments", "avatars"} {
  88. dirPath := path.Join(setting.AppDataPath, dir)
  89. if !com.IsDir(dirPath) {
  90. continue
  91. }
  92. if err = z.AddDir(path.Join(_ARCHIVE_ROOT_DIR+"/data", dir), dirPath); err != nil {
  93. log.Fatal(0, "Fail to include 'data': %v", err)
  94. }
  95. }
  96. }
  97. // Repositories
  98. if !c.Bool("exclude-repos") && !c.Bool("database-only") {
  99. reposDump := path.Join(rootDir, "repositories.zip")
  100. log.Info("Dumping repositories in '%s'", setting.RepoRootPath)
  101. if err = zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
  102. log.Fatal(0, "Fail to dump repositories: %v", err)
  103. }
  104. log.Info("Repositories dumped to: %s", reposDump)
  105. if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/repositories.zip", reposDump); err != nil {
  106. log.Fatal(0, "Fail to include 'repositories.zip': %v", err)
  107. }
  108. }
  109. if err = z.Close(); err != nil {
  110. log.Fatal(0, "Fail to save backup archive '%s': %v", archiveName, err)
  111. }
  112. os.RemoveAll(rootDir)
  113. log.Info("Backup succeed! Archive is located at: %s", archiveName)
  114. log.Shutdown()
  115. return nil
  116. }