dump.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2014 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. "log"
  8. "os"
  9. "path"
  10. "time"
  11. "io/ioutil"
  12. "github.com/Unknwon/cae/zip"
  13. "github.com/codegangsta/cli"
  14. "github.com/gogits/gogs/models"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. var CmdDump = cli.Command{
  18. Name: "dump",
  19. Usage: "Dump Gogs files and database",
  20. Description: `Dump compresses all related files and database into zip file.
  21. It can be used for backup and capture Gogs server image to send to maintainer`,
  22. Action: runDump,
  23. Flags: []cli.Flag{
  24. stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
  25. boolFlag("verbose, v", "show process details"),
  26. },
  27. }
  28. func runDump(ctx *cli.Context) {
  29. if ctx.IsSet("config") {
  30. setting.CustomConf = ctx.String("config")
  31. }
  32. setting.NewContext()
  33. models.LoadConfigs()
  34. models.SetEngine()
  35. TmpWorkDir, err := ioutil.TempDir(os.TempDir(), "gogs-dump-")
  36. if err != nil {
  37. log.Fatalf("Fail to create tmp work directory: %v", err)
  38. }
  39. log.Printf("Creating tmp work dir: %s", TmpWorkDir)
  40. reposDump := path.Join(TmpWorkDir, "gogs-repo.zip")
  41. dbDump := path.Join(TmpWorkDir, "gogs-db.sql")
  42. log.Printf("Dumping local repositories...%s", setting.RepoRootPath)
  43. zip.Verbose = ctx.Bool("verbose")
  44. if err := zip.PackTo(setting.RepoRootPath, reposDump, true); err != nil {
  45. log.Fatalf("Fail to dump local repositories: %v", err)
  46. }
  47. log.Printf("Dumping database...")
  48. if err := models.DumpDatabase(dbDump); err != nil {
  49. log.Fatalf("Fail to dump database: %v", err)
  50. }
  51. fileName := fmt.Sprintf("gogs-dump-%d.zip", time.Now().Unix())
  52. log.Printf("Packing dump files...")
  53. z, err := zip.Create(fileName)
  54. if err != nil {
  55. os.Remove(fileName)
  56. log.Fatalf("Fail to create %s: %v", fileName, err)
  57. }
  58. if err := z.AddFile("gogs-repo.zip", reposDump); err !=nil {
  59. log.Fatalf("Fail to include gogs-repo.zip: %v", err)
  60. }
  61. if err := z.AddFile("gogs-db.sql", dbDump); err !=nil {
  62. log.Fatalf("Fail to include gogs-db.sql: %v", err)
  63. }
  64. customDir, err := os.Stat(setting.CustomPath)
  65. if err == nil && customDir.IsDir() {
  66. if err := z.AddDir("custom", setting.CustomPath); err !=nil {
  67. log.Fatalf("Fail to include custom: %v", err)
  68. }
  69. } else {
  70. log.Printf("Custom dir %s doesn't exist, skipped", setting.CustomPath)
  71. }
  72. if err := z.AddDir("log", setting.LogRootPath); err !=nil {
  73. log.Fatalf("Fail to include log: %v", err)
  74. }
  75. // FIXME: SSH key file.
  76. if err = z.Close(); err != nil {
  77. os.Remove(fileName)
  78. log.Fatalf("Fail to save %s: %v", fileName, err)
  79. }
  80. log.Printf("Removing tmp work dir: %s", TmpWorkDir)
  81. os.RemoveAll(TmpWorkDir)
  82. log.Printf("Finish dumping in file %s", fileName)
  83. }