aboutsummaryrefslogtreecommitdiff
path: root/internal/cmd/backup.go
blob: 28411b0e787960c5adb1c76ed40c404c2bbd79a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2017 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package cmd

import (
	"fmt"
	"io/ioutil"
	"os"
	"path"
	"path/filepath"
	"time"

	"github.com/pkg/errors"
	"github.com/unknwon/cae/zip"
	"github.com/unknwon/com"
	"github.com/urfave/cli"
	"gopkg.in/ini.v1"
	log "unknwon.dev/clog/v2"

	"gogs.io/gogs/internal/conf"
	"gogs.io/gogs/internal/db"
)

var Backup = cli.Command{
	Name:  "backup",
	Usage: "Backup files and database",
	Description: `Backup dumps and compresses all related files and database into zip file,
which can be used for migrating Gogs to another server. The output format is meant to be
portable among all supported database engines.`,
	Action: runBackup,
	Flags: []cli.Flag{
		stringFlag("config, c", "custom/conf/app.ini", "Custom configuration file path"),
		boolFlag("verbose, v", "Show process details"),
		stringFlag("tempdir, t", os.TempDir(), "Temporary directory path"),
		stringFlag("target", "./", "Target directory path to save backup archive"),
		stringFlag("archive-name", fmt.Sprintf("gogs-backup-%s.zip", time.Now().Format("20060102150405")), "Name of backup archive"),
		boolFlag("database-only", "Only dump database"),
		boolFlag("exclude-repos", "Exclude repositories"),
	},
}

const _CURRENT_BACKUP_FORMAT_VERSION = 1
const _ARCHIVE_ROOT_DIR = "gogs-backup"

func runBackup(c *cli.Context) error {
	zip.Verbose = c.Bool("verbose")

	err := conf.Init(c.String("config"))
	if err != nil {
		return errors.Wrap(err, "init configuration")
	}

	db.LoadConfigs()
	db.SetEngine()

	tmpDir := c.String("tempdir")
	if !com.IsExist(tmpDir) {
		log.Fatal("'--tempdir' does not exist: %s", tmpDir)
	}
	rootDir, err := ioutil.TempDir(tmpDir, "gogs-backup-")
	if err != nil {
		log.Fatal("Failed to create backup root directory '%s': %v", rootDir, err)
	}
	log.Info("Backup root directory: %s", rootDir)

	// Metadata
	metaFile := path.Join(rootDir, "metadata.ini")
	metadata := ini.Empty()
	metadata.Section("").Key("VERSION").SetValue(com.ToStr(_CURRENT_BACKUP_FORMAT_VERSION))
	metadata.Section("").Key("DATE_TIME").SetValue(time.Now().String())
	metadata.Section("").Key("GOGS_VERSION").SetValue(conf.App.Version)
	if err = metadata.SaveTo(metaFile); err != nil {
		log.Fatal("Failed to save metadata '%s': %v", metaFile, err)
	}

	archiveName := filepath.Join(c.String("target"), c.String("archive-name"))
	log.Info("Packing backup files to: %s", archiveName)

	z, err := zip.Create(archiveName)
	if err != nil {
		log.Fatal("Failed to create backup archive '%s': %v", archiveName, err)
	}
	if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/metadata.ini", metaFile); err != nil {
		log.Fatal("Failed to include 'metadata.ini': %v", err)
	}

	// Database
	dbDir := filepath.Join(rootDir, "db")
	if err = db.DumpDatabase(dbDir); err != nil {
		log.Fatal("Failed to dump database: %v", err)
	}
	if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/db", dbDir); err != nil {
		log.Fatal("Failed to include 'db': %v", err)
	}

	// Custom files
	if !c.Bool("database-only") {
		if err = z.AddDir(_ARCHIVE_ROOT_DIR+"/custom", conf.CustomDir()); err != nil {
			log.Fatal("Failed to include 'custom': %v", err)
		}
	}

	// Data files
	if !c.Bool("database-only") {
		for _, dir := range []string{"attachments", "avatars", "repo-avatars"} {
			dirPath := filepath.Join(conf.Server.AppDataPath, dir)
			if !com.IsDir(dirPath) {
				continue
			}

			if err = z.AddDir(path.Join(_ARCHIVE_ROOT_DIR+"/data", dir), dirPath); err != nil {
				log.Fatal("Failed to include 'data': %v", err)
			}
		}
	}

	// Repositories
	if !c.Bool("exclude-repos") && !c.Bool("database-only") {
		reposDump := filepath.Join(rootDir, "repositories.zip")
		log.Info("Dumping repositories in '%s'", conf.RepoRootPath)
		if err = zip.PackTo(conf.RepoRootPath, reposDump, true); err != nil {
			log.Fatal("Failed to dump repositories: %v", err)
		}
		log.Info("Repositories dumped to: %s", reposDump)

		if err = z.AddFile(_ARCHIVE_ROOT_DIR+"/repositories.zip", reposDump); err != nil {
			log.Fatal("Failed to include 'repositories.zip': %v", err)
		}
	}

	if err = z.Close(); err != nil {
		log.Fatal("Failed to save backup archive '%s': %v", archiveName, err)
	}

	os.RemoveAll(rootDir)
	log.Info("Backup succeed! Archive is located at: %s", archiveName)
	log.Stop()
	return nil
}