backup.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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/gogs/gogs/models"
  17. "github.com/gogs/gogs/pkg/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. stringFlag("archive-name", fmt.Sprintf("gogs-backup-%s.zip", time.Now().Format("20060102150405")), "Name of backup archive"),
  32. boolFlag("database-only", "Only dump database"),
  33. boolFlag("exclude-repos", "Exclude repositories"),
  34. },
  35. }
  36. const _CURRENT_BACKUP_FORMAT_VERSION = 1
  37. const _ARCHIVE_ROOT_DIR = "gogs-backup"
  38. func runBackup(c *cli.Context) error {
  39. zip.Verbose = c.Bool("verbose")
  40. if c.IsSet("config") {
  41. setting.CustomConf = c.String("config")
  42. }
  43. setting.NewContext()
  44. models.LoadConfigs()
  45. models.SetEngine()
  46. tmpDir := c.String("tempdir")
  47. if !com.IsExist(tmpDir) {
  48. log.Fatal(0, "'--tempdir' does not exist: %s", tmpDir)
  49. }
  50. rootDir, err := ioutil.TempDir(tmpDir, "gogs-backup-")
  51. if err != nil {
  52. log.Fatal(0, "Fail to create backup root directory '%s': %v", rootDir, err)
  53. }
  54. log.Info("Backup root directory: %s", rootDir)
  55. // Metadata
  56. metaFile := path.Join(rootDir, "metadata.ini")
  57. metadata := ini.Empty()
  58. metadata.Section("").Key("VERSION").SetValue(com.ToStr(_CURRENT_BACKUP_FORMAT_VERSION))
  59. metadata.Section("").Key("DATE_TIME").SetValue(time.Now().String())
  60. metadata.Section("").Key("GOGS_VERSION").SetValue(setting.AppVer)
  61. if err = metadata.SaveTo(metaFile); err != nil {
  62. log.Fatal(0, "Fail to save metadata '%s': %v", metaFile, err)
  63. }
  64. archiveName := path.Join(c.String("target"), c.String("archive-name"))
  65. log.Info("Packing backup files to: %s", archiveName)
  66. z, err := zip.Create(archiveName)
  67. if err != nil {
  68. log.Fatal(0, "Fail to create backup archive '%s': %v", archiveName, err)
  69. }
  70. if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/metadata.ini", metaFile); err != nil {
  71. log.Fatal(0, "Fail to include 'metadata.ini': %v", err)
  72. }
  73. // Database
  74. dbDir := path.Join(rootDir, "db")
  75. if err = models.DumpDatabase(dbDir); err != nil {
  76. log.Fatal(0, "Fail to dump database: %v", err)
  77. }
  78. if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/db", dbDir); err != nil {
  79. log.Fatal(0, "Fail to include 'db': %v", err)
  80. }
  81. // Custom files
  82. if !c.Bool("database-only") {
  83. if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/custom", setting.CustomPath); err != nil {
  84. log.Fatal(0, "Fail to include 'custom': %v", err)
  85. }
  86. }
  87. // Data files
  88. if !c.Bool("database-only") {
  89. for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
  90. dirPath := path.Join(setting.AppDataPath, dir)
  91. if !com.IsDir(dirPath) {
  92. continue
  93. }
  94. if err = z.AddDir(path.Join(_ARCHIVE_ROOT_DIR+"/data", dir), dirPath); err != nil {
  95. log.Fatal(0, "Fail to include 'data': %v", err)
  96. }
  97. }
  98. }
  99. // Repositories
  100. if !c.Bool("exclude-repos") && !c.Bool("database-only") {
  101. reposDump := path.Join(rootDir, "repositories.zip")
  102. log.Info("Dumping repositories in '%s'", setting.RepoRootPath)
  103. if err = zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
  104. log.Fatal(0, "Fail to dump repositories: %v", err)
  105. }
  106. log.Info("Repositories dumped to: %s", reposDump)
  107. if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/repositories.zip", reposDump); err != nil {
  108. log.Fatal(0, "Fail to include 'repositories.zip': %v", err)
  109. }
  110. }
  111. if err = z.Close(); err != nil {
  112. log.Fatal(0, "Fail to save backup archive '%s': %v", archiveName, err)
  113. }
  114. os.RemoveAll(rootDir)
  115. log.Info("Backup succeed! Archive is located at: %s", archiveName)
  116. log.Shutdown()
  117. return nil
  118. }