backup.go 4.1 KB

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