dump.go 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. "log"
  7. "os"
  8. "path"
  9. "github.com/Unknwon/cae/zip"
  10. "github.com/codegangsta/cli"
  11. "github.com/gogits/gogs/models"
  12. "github.com/gogits/gogs/modules/base"
  13. )
  14. var CmdDump = cli.Command{
  15. Name: "dump",
  16. Usage: "Dump Gogs files and database",
  17. Description: `Dump compresses all related files and database into zip file.
  18. It can be used for backup and capture Gogs server image to send to maintainer`,
  19. Action: runDump,
  20. Flags: []cli.Flag{},
  21. }
  22. func runDump(*cli.Context) {
  23. base.NewConfigContext()
  24. models.LoadModelsConfig()
  25. models.SetEngine()
  26. log.Printf("Dumping local repositories...%s", base.RepoRootPath)
  27. zip.Verbose = false
  28. defer os.Remove("gogs-repo.zip")
  29. if err := zip.PackTo(base.RepoRootPath, "gogs-repo.zip", true); err != nil {
  30. log.Fatalf("Fail to dump local repositories: %v", err)
  31. }
  32. log.Printf("Dumping database...")
  33. defer os.Remove("gogs-db.sql")
  34. if err := models.DumpDatabase("gogs-db.sql"); err != nil {
  35. log.Fatalf("Fail to dump database: %v", err)
  36. }
  37. log.Printf("Packing dump files...")
  38. z, err := zip.Create("gogs-dump.zip")
  39. if err != nil {
  40. os.Remove("gogs-dump.zip")
  41. log.Fatalf("Fail to create gogs-dump.zip: %v", err)
  42. }
  43. execDir, _ := base.ExecDir()
  44. z.AddFile("gogs-repo.zip", path.Join(execDir, "gogs-repo.zip"))
  45. z.AddFile("gogs-db.sql", path.Join(execDir, "gogs-db.sql"))
  46. z.AddFile("custom/conf/app.ini", path.Join(execDir, "custom/conf/app.ini"))
  47. z.AddDir("log", path.Join(execDir, "log"))
  48. if err = z.Close(); err != nil {
  49. os.Remove("gogs-dump.zip")
  50. log.Fatalf("Fail to save gogs-dump.zip: %v", err)
  51. }
  52. log.Println("Finish dumping!")
  53. }