You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dump.go 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2016 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cmd
  6. import (
  7. "fmt"
  8. "io"
  9. "os"
  10. "path"
  11. "path/filepath"
  12. "strings"
  13. "time"
  14. "code.gitea.io/gitea/models/db"
  15. "code.gitea.io/gitea/modules/json"
  16. "code.gitea.io/gitea/modules/log"
  17. "code.gitea.io/gitea/modules/setting"
  18. "code.gitea.io/gitea/modules/storage"
  19. "code.gitea.io/gitea/modules/util"
  20. "gitea.com/go-chi/session"
  21. archiver "github.com/mholt/archiver/v3"
  22. "github.com/urfave/cli"
  23. )
  24. func addReader(w archiver.Writer, r io.ReadCloser, info os.FileInfo, customName string, verbose bool) error {
  25. if verbose {
  26. log.Info("Adding file %s", customName)
  27. }
  28. return w.Write(archiver.File{
  29. FileInfo: archiver.FileInfo{
  30. FileInfo: info,
  31. CustomName: customName,
  32. },
  33. ReadCloser: r,
  34. })
  35. }
  36. func addFile(w archiver.Writer, filePath, absPath string, verbose bool) error {
  37. file, err := os.Open(absPath)
  38. if err != nil {
  39. return err
  40. }
  41. defer file.Close()
  42. fileInfo, err := file.Stat()
  43. if err != nil {
  44. return err
  45. }
  46. return addReader(w, file, fileInfo, filePath, verbose)
  47. }
  48. func isSubdir(upper, lower string) (bool, error) {
  49. if relPath, err := filepath.Rel(upper, lower); err != nil {
  50. return false, err
  51. } else if relPath == "." || !strings.HasPrefix(relPath, ".") {
  52. return true, nil
  53. }
  54. return false, nil
  55. }
  56. type outputType struct {
  57. Enum []string
  58. Default string
  59. selected string
  60. }
  61. func (o outputType) Join() string {
  62. return strings.Join(o.Enum, ", ")
  63. }
  64. func (o *outputType) Set(value string) error {
  65. for _, enum := range o.Enum {
  66. if enum == value {
  67. o.selected = value
  68. return nil
  69. }
  70. }
  71. return fmt.Errorf("allowed values are %s", o.Join())
  72. }
  73. func (o outputType) String() string {
  74. if o.selected == "" {
  75. return o.Default
  76. }
  77. return o.selected
  78. }
  79. var outputTypeEnum = &outputType{
  80. Enum: []string{"zip", "tar", "tar.sz", "tar.gz", "tar.xz", "tar.bz2", "tar.br", "tar.lz4"},
  81. Default: "zip",
  82. }
  83. // CmdDump represents the available dump sub-command.
  84. var CmdDump = cli.Command{
  85. Name: "dump",
  86. Usage: "Dump Gitea files and database",
  87. Description: `Dump compresses all related files and database into zip file.
  88. It can be used for backup and capture Gitea server image to send to maintainer`,
  89. Action: runDump,
  90. Flags: []cli.Flag{
  91. cli.StringFlag{
  92. Name: "file, f",
  93. Value: fmt.Sprintf("gitea-dump-%d.zip", time.Now().Unix()),
  94. Usage: "Name of the dump file which will be created. Supply '-' for stdout. See type for available types.",
  95. },
  96. cli.BoolFlag{
  97. Name: "verbose, V",
  98. Usage: "Show process details",
  99. },
  100. cli.StringFlag{
  101. Name: "tempdir, t",
  102. Value: os.TempDir(),
  103. Usage: "Temporary dir path",
  104. },
  105. cli.StringFlag{
  106. Name: "database, d",
  107. Usage: "Specify the database SQL syntax",
  108. },
  109. cli.BoolFlag{
  110. Name: "skip-repository, R",
  111. Usage: "Skip the repository dumping",
  112. },
  113. cli.BoolFlag{
  114. Name: "skip-log, L",
  115. Usage: "Skip the log dumping",
  116. },
  117. cli.BoolFlag{
  118. Name: "skip-custom-dir",
  119. Usage: "Skip custom directory",
  120. },
  121. cli.BoolFlag{
  122. Name: "skip-lfs-data",
  123. Usage: "Skip LFS data",
  124. },
  125. cli.BoolFlag{
  126. Name: "skip-attachment-data",
  127. Usage: "Skip attachment data",
  128. },
  129. cli.BoolFlag{
  130. Name: "skip-package-data",
  131. Usage: "Skip package data",
  132. },
  133. cli.GenericFlag{
  134. Name: "type",
  135. Value: outputTypeEnum,
  136. Usage: fmt.Sprintf("Dump output format: %s", outputTypeEnum.Join()),
  137. },
  138. },
  139. }
  140. func fatal(format string, args ...interface{}) {
  141. fmt.Fprintf(os.Stderr, format+"\n", args...)
  142. log.Fatal(format, args...)
  143. }
  144. func runDump(ctx *cli.Context) error {
  145. var file *os.File
  146. fileName := ctx.String("file")
  147. outType := ctx.String("type")
  148. if fileName == "-" {
  149. file = os.Stdout
  150. err := log.DelLogger("console")
  151. if err != nil {
  152. fatal("Deleting default logger failed. Can not write to stdout: %v", err)
  153. }
  154. } else {
  155. for _, suffix := range outputTypeEnum.Enum {
  156. if strings.HasSuffix(fileName, "."+suffix) {
  157. fileName = strings.TrimSuffix(fileName, "."+suffix)
  158. break
  159. }
  160. }
  161. fileName += "." + outType
  162. }
  163. setting.LoadFromExisting()
  164. // make sure we are logging to the console no matter what the configuration tells us do to
  165. if _, err := setting.Cfg.Section("log").NewKey("MODE", "console"); err != nil {
  166. fatal("Setting logging mode to console failed: %v", err)
  167. }
  168. if _, err := setting.Cfg.Section("log.console").NewKey("STDERR", "true"); err != nil {
  169. fatal("Setting console logger to stderr failed: %v", err)
  170. }
  171. if !setting.InstallLock {
  172. log.Error("Is '%s' really the right config path?\n", setting.CustomConf)
  173. return fmt.Errorf("gitea is not initialized")
  174. }
  175. setting.NewServices() // cannot access session settings otherwise
  176. stdCtx, cancel := installSignals()
  177. defer cancel()
  178. err := db.InitEngine(stdCtx)
  179. if err != nil {
  180. return err
  181. }
  182. if err := storage.Init(); err != nil {
  183. return err
  184. }
  185. if file == nil {
  186. file, err = os.Create(fileName)
  187. if err != nil {
  188. fatal("Unable to open %s: %v", fileName, err)
  189. }
  190. }
  191. defer file.Close()
  192. absFileName, err := filepath.Abs(fileName)
  193. if err != nil {
  194. return err
  195. }
  196. verbose := ctx.Bool("verbose")
  197. var iface interface{}
  198. if fileName == "-" {
  199. iface, err = archiver.ByExtension(fmt.Sprintf(".%s", outType))
  200. } else {
  201. iface, err = archiver.ByExtension(fileName)
  202. }
  203. if err != nil {
  204. fatal("Unable to get archiver for extension: %v", err)
  205. }
  206. w, _ := iface.(archiver.Writer)
  207. if err := w.Create(file); err != nil {
  208. fatal("Creating archiver.Writer failed: %v", err)
  209. }
  210. defer w.Close()
  211. if ctx.IsSet("skip-repository") && ctx.Bool("skip-repository") {
  212. log.Info("Skip dumping local repositories")
  213. } else {
  214. log.Info("Dumping local repositories... %s", setting.RepoRootPath)
  215. if err := addRecursiveExclude(w, "repos", setting.RepoRootPath, []string{absFileName}, verbose); err != nil {
  216. fatal("Failed to include repositories: %v", err)
  217. }
  218. if ctx.IsSet("skip-lfs-data") && ctx.Bool("skip-lfs-data") {
  219. log.Info("Skip dumping LFS data")
  220. } else if err := storage.LFS.IterateObjects(func(objPath string, object storage.Object) error {
  221. info, err := object.Stat()
  222. if err != nil {
  223. return err
  224. }
  225. return addReader(w, object, info, path.Join("data", "lfs", objPath), verbose)
  226. }); err != nil {
  227. fatal("Failed to dump LFS objects: %v", err)
  228. }
  229. }
  230. tmpDir := ctx.String("tempdir")
  231. if _, err := os.Stat(tmpDir); os.IsNotExist(err) {
  232. fatal("Path does not exist: %s", tmpDir)
  233. }
  234. dbDump, err := os.CreateTemp(tmpDir, "gitea-db.sql")
  235. if err != nil {
  236. fatal("Failed to create tmp file: %v", err)
  237. }
  238. defer func() {
  239. if err := util.Remove(dbDump.Name()); err != nil {
  240. log.Warn("Unable to remove temporary file: %s: Error: %v", dbDump.Name(), err)
  241. }
  242. }()
  243. targetDBType := ctx.String("database")
  244. if len(targetDBType) > 0 && targetDBType != setting.Database.Type {
  245. log.Info("Dumping database %s => %s...", setting.Database.Type, targetDBType)
  246. } else {
  247. log.Info("Dumping database...")
  248. }
  249. if err := db.DumpDatabase(dbDump.Name(), targetDBType); err != nil {
  250. fatal("Failed to dump database: %v", err)
  251. }
  252. if err := addFile(w, "gitea-db.sql", dbDump.Name(), verbose); err != nil {
  253. fatal("Failed to include gitea-db.sql: %v", err)
  254. }
  255. if len(setting.CustomConf) > 0 {
  256. log.Info("Adding custom configuration file from %s", setting.CustomConf)
  257. if err := addFile(w, "app.ini", setting.CustomConf, verbose); err != nil {
  258. fatal("Failed to include specified app.ini: %v", err)
  259. }
  260. }
  261. if ctx.IsSet("skip-custom-dir") && ctx.Bool("skip-custom-dir") {
  262. log.Info("Skipping custom directory")
  263. } else {
  264. customDir, err := os.Stat(setting.CustomPath)
  265. if err == nil && customDir.IsDir() {
  266. if is, _ := isSubdir(setting.AppDataPath, setting.CustomPath); !is {
  267. if err := addRecursiveExclude(w, "custom", setting.CustomPath, []string{absFileName}, verbose); err != nil {
  268. fatal("Failed to include custom: %v", err)
  269. }
  270. } else {
  271. log.Info("Custom dir %s is inside data dir %s, skipped", setting.CustomPath, setting.AppDataPath)
  272. }
  273. } else {
  274. log.Info("Custom dir %s doesn't exist, skipped", setting.CustomPath)
  275. }
  276. }
  277. isExist, err := util.IsExist(setting.AppDataPath)
  278. if err != nil {
  279. log.Error("Unable to check if %s exists. Error: %v", setting.AppDataPath, err)
  280. }
  281. if isExist {
  282. log.Info("Packing data directory...%s", setting.AppDataPath)
  283. var excludes []string
  284. if setting.Cfg.Section("session").Key("PROVIDER").Value() == "file" {
  285. var opts session.Options
  286. if err = json.Unmarshal([]byte(setting.SessionConfig.ProviderConfig), &opts); err != nil {
  287. return err
  288. }
  289. excludes = append(excludes, opts.ProviderConfig)
  290. }
  291. excludes = append(excludes, setting.RepoRootPath)
  292. excludes = append(excludes, setting.LFS.Path)
  293. excludes = append(excludes, setting.Attachment.Path)
  294. excludes = append(excludes, setting.Packages.Path)
  295. excludes = append(excludes, setting.LogRootPath)
  296. excludes = append(excludes, absFileName)
  297. if err := addRecursiveExclude(w, "data", setting.AppDataPath, excludes, verbose); err != nil {
  298. fatal("Failed to include data directory: %v", err)
  299. }
  300. }
  301. if ctx.IsSet("skip-attachment-data") && ctx.Bool("skip-attachment-data") {
  302. log.Info("Skip dumping attachment data")
  303. } else if err := storage.Attachments.IterateObjects(func(objPath string, object storage.Object) error {
  304. info, err := object.Stat()
  305. if err != nil {
  306. return err
  307. }
  308. return addReader(w, object, info, path.Join("data", "attachments", objPath), verbose)
  309. }); err != nil {
  310. fatal("Failed to dump attachments: %v", err)
  311. }
  312. if ctx.IsSet("skip-package-data") && ctx.Bool("skip-package-data") {
  313. log.Info("Skip dumping package data")
  314. } else if err := storage.Packages.IterateObjects(func(objPath string, object storage.Object) error {
  315. info, err := object.Stat()
  316. if err != nil {
  317. return err
  318. }
  319. return addReader(w, object, info, path.Join("data", "packages", objPath), verbose)
  320. }); err != nil {
  321. fatal("Failed to dump packages: %v", err)
  322. }
  323. // Doesn't check if LogRootPath exists before processing --skip-log intentionally,
  324. // ensuring that it's clear the dump is skipped whether the directory's initialized
  325. // yet or not.
  326. if ctx.IsSet("skip-log") && ctx.Bool("skip-log") {
  327. log.Info("Skip dumping log files")
  328. } else {
  329. isExist, err := util.IsExist(setting.LogRootPath)
  330. if err != nil {
  331. log.Error("Unable to check if %s exists. Error: %v", setting.LogRootPath, err)
  332. }
  333. if isExist {
  334. if err := addRecursiveExclude(w, "log", setting.LogRootPath, []string{absFileName}, verbose); err != nil {
  335. fatal("Failed to include log: %v", err)
  336. }
  337. }
  338. }
  339. if fileName != "-" {
  340. if err = w.Close(); err != nil {
  341. _ = util.Remove(fileName)
  342. fatal("Failed to save %s: %v", fileName, err)
  343. }
  344. if err := os.Chmod(fileName, 0o600); err != nil {
  345. log.Info("Can't change file access permissions mask to 0600: %v", err)
  346. }
  347. }
  348. if fileName != "-" {
  349. log.Info("Finish dumping in file %s", fileName)
  350. } else {
  351. log.Info("Finish dumping to stdout")
  352. }
  353. return nil
  354. }
  355. func contains(slice []string, s string) bool {
  356. for _, v := range slice {
  357. if v == s {
  358. return true
  359. }
  360. }
  361. return false
  362. }
  363. // addRecursiveExclude zips absPath to specified insidePath inside writer excluding excludeAbsPath
  364. func addRecursiveExclude(w archiver.Writer, insidePath, absPath string, excludeAbsPath []string, verbose bool) error {
  365. absPath, err := filepath.Abs(absPath)
  366. if err != nil {
  367. return err
  368. }
  369. dir, err := os.Open(absPath)
  370. if err != nil {
  371. return err
  372. }
  373. defer dir.Close()
  374. files, err := dir.Readdir(0)
  375. if err != nil {
  376. return err
  377. }
  378. for _, file := range files {
  379. currentAbsPath := path.Join(absPath, file.Name())
  380. currentInsidePath := path.Join(insidePath, file.Name())
  381. if file.IsDir() {
  382. if !contains(excludeAbsPath, currentAbsPath) {
  383. if err := addFile(w, currentInsidePath, currentAbsPath, false); err != nil {
  384. return err
  385. }
  386. if err = addRecursiveExclude(w, currentInsidePath, currentAbsPath, excludeAbsPath, verbose); err != nil {
  387. return err
  388. }
  389. }
  390. } else {
  391. if err = addFile(w, currentInsidePath, currentAbsPath, verbose); err != nil {
  392. return err
  393. }
  394. }
  395. }
  396. return nil
  397. }