2014-05-01 19:21:46 -06:00
// Copyright 2014 The Gogs Authors. All rights reserved.
2016-12-21 05:13:17 -07:00
// Copyright 2016 The Gitea Authors. All rights reserved.
2022-11-27 11:20:29 -07:00
// SPDX-License-Identifier: MIT
2014-05-01 19:21:46 -06:00
package cmd
import (
2014-05-05 11:08:01 -06:00
"fmt"
2014-05-01 19:21:46 -06:00
"os"
"path"
2017-01-11 21:47:20 -07:00
"path/filepath"
2020-06-05 14:47:39 -06:00
"strings"
2014-05-01 19:21:46 -06:00
2021-09-19 05:49:59 -06:00
"code.gitea.io/gitea/models/db"
2024-04-02 20:47:06 -06:00
"code.gitea.io/gitea/modules/dump"
2021-07-24 10:03:58 -06:00
"code.gitea.io/gitea/modules/json"
2019-12-17 09:12:10 -07:00
"code.gitea.io/gitea/modules/log"
2016-11-10 09:24:48 -07:00
"code.gitea.io/gitea/modules/setting"
2020-09-29 03:05:13 -06:00
"code.gitea.io/gitea/modules/storage"
2020-08-11 14:05:34 -06:00
"code.gitea.io/gitea/modules/util"
2017-04-12 01:44:54 -06:00
2021-01-26 08:36:53 -07:00
"gitea.com/go-chi/session"
2022-06-18 08:06:32 -06:00
"github.com/mholt/archiver/v3"
2023-07-21 03:28:19 -06:00
"github.com/urfave/cli/v2"
2014-05-01 19:21:46 -06:00
)
2016-11-04 05:42:18 -06:00
// CmdDump represents the available dump sub-command.
2023-07-21 03:28:19 -06:00
var CmdDump = & cli . Command {
2024-04-02 20:47:06 -06:00
Name : "dump" ,
Usage : "Dump Gitea files and database" ,
Description : ` Dump compresses all related files and database into zip file. It can be used for backup and capture Gitea server image to send to maintainer ` ,
Action : runDump ,
2014-09-07 17:39:26 -06:00
Flags : [ ] cli . Flag {
2023-07-21 03:28:19 -06:00
& cli . StringFlag {
Name : "file" ,
Aliases : [ ] string { "f" } ,
2024-04-02 20:47:06 -06:00
Usage : ` Name of the dump file which will be created, default to "gitea-dump- { time}.zip". Supply '-' for stdout. See type for available types. ` ,
2019-03-31 22:31:37 -06:00
} ,
2023-07-21 03:28:19 -06:00
& cli . BoolFlag {
Name : "verbose" ,
Aliases : [ ] string { "V" } ,
Usage : "Show process details" ,
2016-11-09 15:18:22 -07:00
} ,
2023-07-21 03:28:19 -06:00
& cli . BoolFlag {
Name : "quiet" ,
Aliases : [ ] string { "q" } ,
Usage : "Only display warnings and errors" ,
2023-04-10 07:46:23 -06:00
} ,
2023-07-21 03:28:19 -06:00
& cli . StringFlag {
Name : "tempdir" ,
Aliases : [ ] string { "t" } ,
Value : os . TempDir ( ) ,
Usage : "Temporary dir path" ,
2016-11-09 15:18:22 -07:00
} ,
2023-07-21 03:28:19 -06:00
& cli . StringFlag {
Name : "database" ,
Aliases : [ ] string { "d" } ,
2023-09-03 12:44:01 -06:00
Usage : "Specify the database SQL syntax: sqlite3, mysql, mssql, postgres" ,
2017-01-03 01:20:28 -07:00
} ,
2023-07-21 03:28:19 -06:00
& cli . BoolFlag {
Name : "skip-repository" ,
Aliases : [ ] string { "R" } ,
Usage : "Skip the repository dumping" ,
2019-01-13 14:52:26 -07:00
} ,
2023-07-21 03:28:19 -06:00
& cli . BoolFlag {
Name : "skip-log" ,
Aliases : [ ] string { "L" } ,
Usage : "Skip the log dumping" ,
2020-04-30 19:30:31 -06:00
} ,
2023-07-21 03:28:19 -06:00
& cli . BoolFlag {
2021-02-07 18:00:12 -07:00
Name : "skip-custom-dir" ,
Usage : "Skip custom directory" ,
} ,
2023-07-21 03:28:19 -06:00
& cli . BoolFlag {
2021-04-12 03:33:32 -06:00
Name : "skip-lfs-data" ,
Usage : "Skip LFS data" ,
} ,
2023-07-21 03:28:19 -06:00
& cli . BoolFlag {
2021-04-12 03:33:32 -06:00
Name : "skip-attachment-data" ,
Usage : "Skip attachment data" ,
} ,
2023-07-21 03:28:19 -06:00
& cli . BoolFlag {
2022-04-26 14:30:51 -06:00
Name : "skip-package-data" ,
Usage : "Skip package data" ,
} ,
2023-07-21 03:28:19 -06:00
& cli . BoolFlag {
2022-10-23 21:19:21 -06:00
Name : "skip-index" ,
Usage : "Skip bleve index data" ,
} ,
2024-04-22 00:25:07 -06:00
& cli . BoolFlag {
Name : "skip-db" ,
Usage : "Skip database" ,
} ,
2024-04-02 20:47:06 -06:00
& cli . StringFlag {
2020-06-05 14:47:39 -06:00
Name : "type" ,
2024-04-02 20:47:06 -06:00
Usage : fmt . Sprintf ( ` Dump output format, default to "zip", supported types: %s ` , strings . Join ( dump . SupportedOutputTypes , ", " ) ) ,
2020-06-05 14:47:39 -06:00
} ,
2014-09-07 17:39:26 -06:00
} ,
2014-05-01 19:21:46 -06:00
}
2023-07-04 12:36:08 -06:00
func fatal ( format string , args ... any ) {
2019-12-17 09:12:10 -07:00
log . Fatal ( format , args ... )
}
2016-05-12 12:32:28 -06:00
func runDump ( ctx * cli . Context ) error {
Refactor path & config system (#25330)
# The problem
There were many "path tricks":
* By default, Gitea uses its program directory as its work path
* Gitea tries to use the "work path" to guess its "custom path" and
"custom conf (app.ini)"
* Users might want to use other directories as work path
* The non-default work path should be passed to Gitea by GITEA_WORK_DIR
or "--work-path"
* But some Gitea processes are started without these values
* The "serv" process started by OpenSSH server
* The CLI sub-commands started by site admin
* The paths are guessed by SetCustomPathAndConf again and again
* The default values of "work path / custom path / custom conf" can be
changed when compiling
# The solution
* Use `InitWorkPathAndCommonConfig` to handle these path tricks, and use
test code to cover its behaviors.
* When Gitea's web server runs, write the WORK_PATH to "app.ini", this
value must be the most correct one, because if this value is not right,
users would find that the web UI doesn't work and then they should be
able to fix it.
* Then all other sub-commands can use the WORK_PATH in app.ini to
initialize their paths.
* By the way, when Gitea starts for git protocol, it shouldn't output
any log, otherwise the git protocol gets broken and client blocks
forever.
The "work path" priority is: WORK_PATH in app.ini > cmd arg --work-path
> env var GITEA_WORK_DIR > builtin default
The "app.ini" searching order is: cmd arg --config > cmd arg "work path
/ custom path" > env var "work path / custom path" > builtin default
## ⚠️ BREAKING
If your instance's "work path / custom path / custom conf" doesn't meet
the requirements (eg: work path must be absolute), Gitea will report a
fatal error and exit. You need to set these values according to the
error log.
----
Close #24818
Close #24222
Close #21606
Close #21498
Close #25107
Close #24981
Maybe close #24503
Replace #23301
Replace #22754
And maybe more
2023-06-20 23:50:26 -06:00
setting . MustInstalled ( )
2021-12-01 00:50:01 -07:00
2024-04-02 20:47:06 -06:00
quite := ctx . Bool ( "quiet" )
verbose := ctx . Bool ( "verbose" )
if verbose && quite {
fatal ( "Option --quiet and --verbose cannot both be set" )
2020-06-05 14:47:39 -06:00
}
2024-04-02 20:47:06 -06:00
// outFileName is either "-" or a file name (will be made absolute)
outFileName , outType := dump . PrepareFileNameAndType ( ctx . String ( "file" ) , ctx . String ( "type" ) )
if outType == "" {
fatal ( "Invalid output type" )
2020-06-05 14:47:39 -06:00
}
2023-04-10 07:46:23 -06:00
2024-04-02 20:47:06 -06:00
outFile := os . Stdout
if outFileName != "-" {
var err error
if outFileName , err = filepath . Abs ( outFileName ) ; err != nil {
fatal ( "Unable to get absolute path of dump file: %v" , err )
}
if exist , _ := util . IsExist ( outFileName ) ; exist {
fatal ( "Dump file %q exists" , outFileName )
2023-04-10 07:46:23 -06:00
}
2024-04-02 20:47:06 -06:00
if outFile , err = os . Create ( outFileName ) ; err != nil {
fatal ( "Unable to create dump file %q: %v" , outFileName , err )
}
defer outFile . Close ( )
2023-04-10 07:46:23 -06:00
}
2024-04-02 20:47:06 -06:00
setupConsoleLogger ( util . Iif ( quite , log . WARN , log . INFO ) , log . CanColorStderr , os . Stderr )
2017-01-23 02:11:18 -07:00
2024-04-02 20:47:06 -06:00
setting . DisableLoggerInit ( )
setting . LoadSettings ( ) // cannot access session settings otherwise
2023-04-10 07:46:23 -06:00
2021-11-06 21:11:27 -06:00
stdCtx , cancel := installSignals ( )
defer cancel ( )
err := db . InitEngine ( stdCtx )
2017-01-23 02:11:18 -07:00
if err != nil {
return err
}
2014-05-01 19:21:46 -06:00
2024-04-02 20:47:06 -06:00
if err = storage . Init ( ) ; err != nil {
2020-09-29 03:05:13 -06:00
return err
}
2024-04-02 20:47:06 -06:00
archiverGeneric , err := archiver . ByExtension ( "." + outType )
2019-01-13 14:52:26 -07:00
if err != nil {
2020-06-05 14:47:39 -06:00
fatal ( "Unable to get archiver for extension: %v" , err )
2019-01-13 14:52:26 -07:00
}
2019-12-17 09:12:10 -07:00
2024-04-02 20:47:06 -06:00
archiverWriter := archiverGeneric . ( archiver . Writer )
if err := archiverWriter . Create ( outFile ) ; err != nil {
2020-06-05 14:47:39 -06:00
fatal ( "Creating archiver.Writer failed: %v" , err )
}
2024-04-02 20:47:06 -06:00
defer archiverWriter . Close ( )
dumper := & dump . Dumper {
Writer : archiverWriter ,
Verbose : verbose ,
}
dumper . GlobalExcludeAbsPath ( outFileName )
2019-01-13 14:52:26 -07:00
2020-05-02 21:57:45 -06:00
if ctx . IsSet ( "skip-repository" ) && ctx . Bool ( "skip-repository" ) {
2019-12-17 09:12:10 -07:00
log . Info ( "Skip dumping local repositories" )
2019-01-13 14:52:26 -07:00
} else {
2020-06-05 14:47:39 -06:00
log . Info ( "Dumping local repositories... %s" , setting . RepoRootPath )
2024-04-02 20:47:06 -06:00
if err := dumper . AddRecursiveExclude ( "repos" , setting . RepoRootPath , nil ) ; err != nil {
2020-06-05 14:47:39 -06:00
fatal ( "Failed to include repositories: %v" , err )
2019-01-13 14:52:26 -07:00
}
2020-06-05 14:47:39 -06:00
2021-04-12 03:33:32 -06:00
if ctx . IsSet ( "skip-lfs-data" ) && ctx . Bool ( "skip-lfs-data" ) {
log . Info ( "Skip dumping LFS data" )
2023-03-23 06:30:28 -06:00
} else if ! setting . LFS . StartServer {
log . Info ( "LFS isn't enabled. Skip dumping LFS data" )
2023-03-13 04:23:51 -06:00
} else if err := storage . LFS . IterateObjects ( "" , func ( objPath string , object storage . Object ) error {
2020-09-29 03:05:13 -06:00
info , err := object . Stat ( )
if err != nil {
return err
2020-06-05 14:47:39 -06:00
}
2024-04-02 20:47:06 -06:00
return dumper . AddReader ( object , info , path . Join ( "data" , "lfs" , objPath ) )
2020-09-29 03:05:13 -06:00
} ) ; err != nil {
fatal ( "Failed to dump LFS objects: %v" , err )
2019-01-13 14:52:26 -07:00
}
2014-05-01 19:21:46 -06:00
}
2024-04-22 00:25:07 -06:00
if ctx . Bool ( "skip-db" ) {
// Ensure that we don't dump the database file that may reside in setting.AppDataPath or elsewhere.
dumper . GlobalExcludeAbsPath ( setting . Database . Path )
log . Info ( "Skipping database" )
} else {
tmpDir := ctx . String ( "tempdir" )
if _ , err := os . Stat ( tmpDir ) ; os . IsNotExist ( err ) {
fatal ( "Path does not exist: %s" , tmpDir )
}
2020-06-05 14:47:39 -06:00
2024-04-22 00:25:07 -06:00
dbDump , err := os . CreateTemp ( tmpDir , "gitea-db.sql" )
if err != nil {
fatal ( "Failed to create tmp file: %v" , err )
2020-08-11 14:05:34 -06:00
}
2024-04-22 00:25:07 -06:00
defer func ( ) {
_ = dbDump . Close ( )
if err := util . Remove ( dbDump . Name ( ) ) ; err != nil {
log . Warn ( "Unable to remove temporary file: %s: Error: %v" , dbDump . Name ( ) , err )
}
} ( )
2020-06-05 14:47:39 -06:00
2024-04-22 00:25:07 -06:00
targetDBType := ctx . String ( "database" )
if len ( targetDBType ) > 0 && targetDBType != setting . Database . Type . String ( ) {
log . Info ( "Dumping database %s => %s..." , setting . Database . Type , targetDBType )
} else {
log . Info ( "Dumping database..." )
}
2017-01-03 01:20:28 -07:00
2024-04-22 00:25:07 -06:00
if err := db . DumpDatabase ( dbDump . Name ( ) , targetDBType ) ; err != nil {
fatal ( "Failed to dump database: %v" , err )
}
2014-05-04 22:55:17 -06:00
2024-04-22 00:25:07 -06:00
if err = dumper . AddFile ( "gitea-db.sql" , dbDump . Name ( ) ) ; err != nil {
fatal ( "Failed to include gitea-db.sql: %v" , err )
}
2015-11-28 04:11:38 -07:00
}
2019-04-05 07:24:28 -06:00
2024-04-02 20:47:06 -06:00
log . Info ( "Adding custom configuration file from %s" , setting . CustomConf )
if err = dumper . AddFile ( "app.ini" , setting . CustomConf ) ; err != nil {
fatal ( "Failed to include specified app.ini: %v" , err )
2019-04-05 07:24:28 -06:00
}
2021-02-07 18:00:12 -07:00
if ctx . IsSet ( "skip-custom-dir" ) && ctx . Bool ( "skip-custom-dir" ) {
2021-07-08 05:38:13 -06:00
log . Info ( "Skipping custom directory" )
2021-02-07 18:00:12 -07:00
} else {
customDir , err := os . Stat ( setting . CustomPath )
if err == nil && customDir . IsDir ( ) {
2024-04-02 20:47:06 -06:00
if is , _ := dump . IsSubdir ( setting . AppDataPath , setting . CustomPath ) ; ! is {
if err := dumper . AddRecursiveExclude ( "custom" , setting . CustomPath , nil ) ; err != nil {
2021-02-07 18:00:12 -07:00
fatal ( "Failed to include custom: %v" , err )
}
} else {
log . Info ( "Custom dir %s is inside data dir %s, skipped" , setting . CustomPath , setting . AppDataPath )
2020-06-05 14:47:39 -06:00
}
} else {
2021-02-07 18:00:12 -07:00
log . Info ( "Custom dir %s doesn't exist, skipped" , setting . CustomPath )
2016-05-12 12:32:28 -06:00
}
2015-11-28 04:11:38 -07:00
}
2017-01-11 21:47:20 -07:00
2020-11-27 19:42:08 -07:00
isExist , err := util . IsExist ( setting . AppDataPath )
if err != nil {
log . Error ( "Unable to check if %s exists. Error: %v" , setting . AppDataPath , err )
}
if isExist {
2019-12-17 09:12:10 -07:00
log . Info ( "Packing data directory...%s" , setting . AppDataPath )
2017-01-11 21:47:20 -07:00
2020-06-05 14:47:39 -06:00
var excludes [ ] string
2023-02-19 09:12:01 -07:00
if setting . SessionConfig . OriginalProvider == "file" {
2020-06-05 14:47:39 -06:00
var opts session . Options
if err = json . Unmarshal ( [ ] byte ( setting . SessionConfig . ProviderConfig ) , & opts ) ; err != nil {
return err
}
excludes = append ( excludes , opts . ProviderConfig )
2017-03-02 02:41:33 -07:00
}
2020-06-05 14:47:39 -06:00
2022-10-23 21:19:21 -06:00
if ctx . IsSet ( "skip-index" ) && ctx . Bool ( "skip-index" ) {
excludes = append ( excludes , setting . Indexer . RepoPath )
excludes = append ( excludes , setting . Indexer . IssuePath )
}
2020-06-05 14:47:39 -06:00
excludes = append ( excludes , setting . RepoRootPath )
2023-06-13 21:42:38 -06:00
excludes = append ( excludes , setting . LFS . Storage . Path )
excludes = append ( excludes , setting . Attachment . Storage . Path )
excludes = append ( excludes , setting . Packages . Storage . Path )
2023-02-19 09:12:01 -07:00
excludes = append ( excludes , setting . Log . RootPath )
2024-04-02 20:47:06 -06:00
if err := dumper . AddRecursiveExclude ( "data" , setting . AppDataPath , excludes ) ; err != nil {
2019-12-17 09:12:10 -07:00
fatal ( "Failed to include data directory: %v" , err )
2017-03-02 02:41:33 -07:00
}
2017-01-11 21:47:20 -07:00
}
2021-04-12 03:33:32 -06:00
if ctx . IsSet ( "skip-attachment-data" ) && ctx . Bool ( "skip-attachment-data" ) {
log . Info ( "Skip dumping attachment data" )
2023-03-13 04:23:51 -06:00
} else if err := storage . Attachments . IterateObjects ( "" , func ( objPath string , object storage . Object ) error {
2020-09-29 03:05:13 -06:00
info , err := object . Stat ( )
if err != nil {
return err
}
2024-04-02 20:47:06 -06:00
return dumper . AddReader ( object , info , path . Join ( "data" , "attachments" , objPath ) )
2020-09-29 03:05:13 -06:00
} ) ; err != nil {
fatal ( "Failed to dump attachments: %v" , err )
}
2022-04-26 14:30:51 -06:00
if ctx . IsSet ( "skip-package-data" ) && ctx . Bool ( "skip-package-data" ) {
log . Info ( "Skip dumping package data" )
2023-03-23 06:30:28 -06:00
} else if ! setting . Packages . Enabled {
log . Info ( "Packages isn't enabled. Skip dumping package data" )
2023-03-13 04:23:51 -06:00
} else if err := storage . Packages . IterateObjects ( "" , func ( objPath string , object storage . Object ) error {
2022-04-26 14:30:51 -06:00
info , err := object . Stat ( )
if err != nil {
return err
}
2024-04-02 20:47:06 -06:00
return dumper . AddReader ( object , info , path . Join ( "data" , "packages" , objPath ) )
2022-04-26 14:30:51 -06:00
} ) ; err != nil {
fatal ( "Failed to dump packages: %v" , err )
}
2020-04-30 19:30:31 -06:00
// Doesn't check if LogRootPath exists before processing --skip-log intentionally,
// ensuring that it's clear the dump is skipped whether the directory's initialized
// yet or not.
if ctx . IsSet ( "skip-log" ) && ctx . Bool ( "skip-log" ) {
log . Info ( "Skip dumping log files" )
2020-11-27 19:42:08 -07:00
} else {
2023-02-19 09:12:01 -07:00
isExist , err := util . IsExist ( setting . Log . RootPath )
2020-11-27 19:42:08 -07:00
if err != nil {
2023-02-19 09:12:01 -07:00
log . Error ( "Unable to check if %s exists. Error: %v" , setting . Log . RootPath , err )
2020-11-27 19:42:08 -07:00
}
if isExist {
2024-04-02 20:47:06 -06:00
if err := dumper . AddRecursiveExclude ( "log" , setting . Log . RootPath , nil ) ; err != nil {
2020-11-27 19:42:08 -07:00
fatal ( "Failed to include log: %v" , err )
}
2020-01-16 19:56:51 -07:00
}
2015-11-28 04:11:38 -07:00
}
2017-02-26 01:01:49 -07:00
2024-04-02 20:47:06 -06:00
if outFileName == "-" {
log . Info ( "Finish dumping to stdout" )
} else {
if err = archiverWriter . Close ( ) ; err != nil {
_ = os . Remove ( outFileName )
fatal ( "Failed to save %q: %v" , outFileName , err )
2020-06-05 14:47:39 -06:00
}
2024-04-02 20:47:06 -06:00
if err = os . Chmod ( outFileName , 0 o600 ) ; err != nil {
2020-06-05 14:47:39 -06:00
log . Info ( "Can't change file access permissions mask to 0600: %v" , err )
}
2024-04-02 20:47:06 -06:00
log . Info ( "Finish dumping in file %s" , outFileName )
2017-01-11 21:47:20 -07:00
}
return nil
}