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 13KB

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