|
|
@ -39,6 +39,10 @@ be skipped and remain unchanged.`, |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// lastSupportedVersionOfFormat returns the last supported version of the backup archive
|
|
|
|
|
|
|
|
// format that is able to import.
|
|
|
|
|
|
|
|
var lastSupportedVersionOfFormat = map[int]string{} |
|
|
|
|
|
|
|
|
|
|
|
func runRestore(c *cli.Context) error { |
|
|
|
func runRestore(c *cli.Context) error { |
|
|
|
zip.Verbose = c.Bool("verbose") |
|
|
|
zip.Verbose = c.Bool("verbose") |
|
|
|
|
|
|
|
|
|
|
@ -49,9 +53,10 @@ func runRestore(c *cli.Context) error { |
|
|
|
|
|
|
|
|
|
|
|
log.Info("Restore backup from: %s", c.String("from")) |
|
|
|
log.Info("Restore backup from: %s", c.String("from")) |
|
|
|
if err := zip.ExtractTo(c.String("from"), tmpDir); err != nil { |
|
|
|
if err := zip.ExtractTo(c.String("from"), tmpDir); err != nil { |
|
|
|
log.Fatal(0, "Fail to extract backup archive: %v", err) |
|
|
|
log.Fatal(0, "Failed to extract backup archive: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
archivePath := path.Join(tmpDir, _ARCHIVE_ROOT_DIR) |
|
|
|
archivePath := path.Join(tmpDir, _ARCHIVE_ROOT_DIR) |
|
|
|
|
|
|
|
defer os.RemoveAll(archivePath) |
|
|
|
|
|
|
|
|
|
|
|
// Check backup version
|
|
|
|
// Check backup version
|
|
|
|
metaFile := path.Join(archivePath, "metadata.ini") |
|
|
|
metaFile := path.Join(archivePath, "metadata.ini") |
|
|
@ -60,12 +65,20 @@ func runRestore(c *cli.Context) error { |
|
|
|
} |
|
|
|
} |
|
|
|
metadata, err := ini.Load(metaFile) |
|
|
|
metadata, err := ini.Load(metaFile) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Fatal(0, "Fail to load metadata '%s': %v", metaFile, err) |
|
|
|
log.Fatal(0, "Failed to load metadata '%s': %v", metaFile, err) |
|
|
|
} |
|
|
|
} |
|
|
|
backupVersion := metadata.Section("").Key("GOGS_VERSION").MustString("999.0") |
|
|
|
backupVersion := metadata.Section("").Key("GOGS_VERSION").MustString("999.0") |
|
|
|
if version.Compare(setting.AppVer, backupVersion, "<") { |
|
|
|
if version.Compare(setting.AppVer, backupVersion, "<") { |
|
|
|
log.Fatal(0, "Current Gogs version is lower than backup version: %s < %s", setting.AppVer, backupVersion) |
|
|
|
log.Fatal(0, "Current Gogs version is lower than backup version: %s < %s", setting.AppVer, backupVersion) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
formatVersion := metadata.Section("").Key("VERSION").MustInt() |
|
|
|
|
|
|
|
if formatVersion == 0 { |
|
|
|
|
|
|
|
log.Fatal(0, "Failed to determine the backup format version from metadata '%s': %s", metaFile, "VERSION is not presented") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if formatVersion != _CURRENT_BACKUP_FORMAT_VERSION { |
|
|
|
|
|
|
|
log.Fatal(0, "Backup format version found is %d but this binary only supports %d\nThe last known version that is able to import your backup is %s", |
|
|
|
|
|
|
|
formatVersion, _CURRENT_BACKUP_FORMAT_VERSION, lastSupportedVersionOfFormat[formatVersion]) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// If config file is not present in backup, user must set this file via flag.
|
|
|
|
// If config file is not present in backup, user must set this file via flag.
|
|
|
|
// Otherwise, it's optional to set config file flag.
|
|
|
|
// Otherwise, it's optional to set config file flag.
|
|
|
@ -84,18 +97,18 @@ func runRestore(c *cli.Context) error { |
|
|
|
// Database
|
|
|
|
// Database
|
|
|
|
dbDir := path.Join(archivePath, "db") |
|
|
|
dbDir := path.Join(archivePath, "db") |
|
|
|
if err = models.ImportDatabase(dbDir, c.Bool("verbose")); err != nil { |
|
|
|
if err = models.ImportDatabase(dbDir, c.Bool("verbose")); err != nil { |
|
|
|
log.Fatal(0, "Fail to import database: %v", err) |
|
|
|
log.Fatal(0, "Failed to import database: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Custom files
|
|
|
|
// Custom files
|
|
|
|
if !c.Bool("database-only") { |
|
|
|
if !c.Bool("database-only") { |
|
|
|
if com.IsExist(setting.CustomPath) { |
|
|
|
if com.IsExist(setting.CustomPath) { |
|
|
|
if err = os.Rename(setting.CustomPath, setting.CustomPath+".bak"); err != nil { |
|
|
|
if err = os.Rename(setting.CustomPath, setting.CustomPath+".bak"); err != nil { |
|
|
|
log.Fatal(0, "Fail to backup current 'custom': %v", err) |
|
|
|
log.Fatal(0, "Failed to backup current 'custom': %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if err = os.Rename(path.Join(archivePath, "custom"), setting.CustomPath); err != nil { |
|
|
|
if err = os.Rename(path.Join(archivePath, "custom"), setting.CustomPath); err != nil { |
|
|
|
log.Fatal(0, "Fail to import 'custom': %v", err) |
|
|
|
log.Fatal(0, "Failed to import 'custom': %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
@ -112,11 +125,11 @@ func runRestore(c *cli.Context) error { |
|
|
|
dirPath := path.Join(setting.AppDataPath, dir) |
|
|
|
dirPath := path.Join(setting.AppDataPath, dir) |
|
|
|
if com.IsExist(dirPath) { |
|
|
|
if com.IsExist(dirPath) { |
|
|
|
if err = os.Rename(dirPath, dirPath+".bak"); err != nil { |
|
|
|
if err = os.Rename(dirPath, dirPath+".bak"); err != nil { |
|
|
|
log.Fatal(0, "Fail to backup current 'data': %v", err) |
|
|
|
log.Fatal(0, "Failed to backup current 'data': %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
if err = os.Rename(srcPath, dirPath); err != nil { |
|
|
|
if err = os.Rename(srcPath, dirPath); err != nil { |
|
|
|
log.Fatal(0, "Fail to import 'data': %v", err) |
|
|
|
log.Fatal(0, "Failed to import 'data': %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -125,11 +138,10 @@ func runRestore(c *cli.Context) error { |
|
|
|
reposPath := path.Join(archivePath, "repositories.zip") |
|
|
|
reposPath := path.Join(archivePath, "repositories.zip") |
|
|
|
if !c.Bool("exclude-repos") && !c.Bool("database-only") && com.IsExist(reposPath) { |
|
|
|
if !c.Bool("exclude-repos") && !c.Bool("database-only") && com.IsExist(reposPath) { |
|
|
|
if err := zip.ExtractTo(reposPath, path.Dir(setting.RepoRootPath)); err != nil { |
|
|
|
if err := zip.ExtractTo(reposPath, path.Dir(setting.RepoRootPath)); err != nil { |
|
|
|
log.Fatal(0, "Fail to extract 'repositories.zip': %v", err) |
|
|
|
log.Fatal(0, "Failed to extract 'repositories.zip': %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
os.RemoveAll(path.Join(tmpDir, _ARCHIVE_ROOT_DIR)) |
|
|
|
|
|
|
|
log.Info("Restore succeed!") |
|
|
|
log.Info("Restore succeed!") |
|
|
|
log.Shutdown() |
|
|
|
log.Shutdown() |
|
|
|
return nil |
|
|
|
return nil |
|
|
|