mirror of
https://github.com/restic/restic.git
synced 2026-12-04 15:44:46 +00:00
285 lines
7.6 KiB
Go
285 lines
7.6 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
"regexp"
|
|
"runtime"
|
|
godebug "runtime/debug"
|
|
"strings"
|
|
|
|
"github.com/spf13/cobra"
|
|
"go.uber.org/automaxprocs/maxprocs"
|
|
|
|
"github.com/restic/restic/internal/backend/all"
|
|
"github.com/restic/restic/internal/backend/location"
|
|
"github.com/restic/restic/internal/debug"
|
|
"github.com/restic/restic/internal/errors"
|
|
"github.com/restic/restic/internal/feature"
|
|
"github.com/restic/restic/internal/global"
|
|
"github.com/restic/restic/internal/repository"
|
|
"github.com/restic/restic/internal/ui"
|
|
"github.com/restic/restic/internal/ui/termstatus"
|
|
)
|
|
|
|
func init() {
|
|
// don't import `go.uber.org/automaxprocs` to disable the log output
|
|
_, _ = maxprocs.Set()
|
|
}
|
|
|
|
var ErrOK = errors.New("ok")
|
|
|
|
var cmdGroupDefault = "default"
|
|
var cmdGroupAdvanced = "advanced"
|
|
|
|
func newRootCommand(globalOptions *global.Options) *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "restic",
|
|
Short: "Backup and restore files",
|
|
Long: fmt.Sprintf(`
|
|
restic is a backup program which allows saving multiple revisions of files and
|
|
directories in an encrypted repository stored on different backends.
|
|
|
|
The full documentation can be found at %s.
|
|
`, docsURL(global.Version)),
|
|
SilenceErrors: true,
|
|
SilenceUsage: true,
|
|
DisableAutoGenTag: true,
|
|
|
|
PersistentPreRunE: func(c *cobra.Command, _ []string) error {
|
|
switch c.Name() {
|
|
case cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd:
|
|
return nil
|
|
}
|
|
return globalOptions.PreRun(needsPassword(c.Name()))
|
|
},
|
|
}
|
|
|
|
cmd.AddGroup(
|
|
&cobra.Group{
|
|
ID: cmdGroupDefault,
|
|
Title: "Available Commands:",
|
|
},
|
|
&cobra.Group{
|
|
ID: cmdGroupAdvanced,
|
|
Title: "Advanced Options:",
|
|
},
|
|
)
|
|
|
|
globalOptions.AddFlags(cmd.PersistentFlags())
|
|
|
|
// Use our "generate" command instead of the cobra provided "completion" command
|
|
cmd.CompletionOptions.DisableDefaultCmd = true
|
|
|
|
// globalOptions is passed to commands by reference to allow PersistentPreRunE to modify it
|
|
cmd.AddCommand(
|
|
newBackupCommand(globalOptions),
|
|
newCacheCommand(globalOptions),
|
|
newCatCommand(globalOptions),
|
|
newCheckCommand(globalOptions),
|
|
newCopyCommand(globalOptions),
|
|
newDiffCommand(globalOptions),
|
|
newDumpCommand(globalOptions),
|
|
newFeaturesCommand(globalOptions),
|
|
newFindCommand(globalOptions),
|
|
newForgetCommand(globalOptions),
|
|
newGenerateCommand(globalOptions),
|
|
newInitCommand(globalOptions),
|
|
newKeyCommand(globalOptions),
|
|
newListCommand(globalOptions),
|
|
newLsCommand(globalOptions),
|
|
newMigrateCommand(globalOptions),
|
|
newOptionsCommand(globalOptions),
|
|
newPruneCommand(globalOptions),
|
|
newRebuildIndexCommand(globalOptions),
|
|
newRecoverCommand(globalOptions),
|
|
newRepairCommand(globalOptions),
|
|
newRestoreCommand(globalOptions),
|
|
newRewriteCommand(globalOptions),
|
|
newSnapshotsCommand(globalOptions),
|
|
newStatsCommand(globalOptions),
|
|
newTagCommand(globalOptions),
|
|
newUnlockCommand(globalOptions),
|
|
newVersionCommand(globalOptions),
|
|
)
|
|
|
|
registerDebugCommand(cmd, globalOptions)
|
|
registerMountCommand(cmd, globalOptions)
|
|
registerSelfUpdateCommand(cmd, globalOptions)
|
|
global.RegisterProfiling(cmd, os.Stderr)
|
|
|
|
return cmd
|
|
}
|
|
|
|
// Distinguish commands that need the password from those that work without,
|
|
// so we don't run $RESTIC_PASSWORD_COMMAND for no reason (it might prompt the
|
|
// user for authentication).
|
|
func needsPassword(cmd string) bool {
|
|
switch cmd {
|
|
case "cache", "generate", "help", "options", "self-update", "version", cobra.ShellCompRequestCmd, cobra.ShellCompNoDescRequestCmd:
|
|
return false
|
|
default:
|
|
return true
|
|
}
|
|
}
|
|
|
|
func tweakGoGC() {
|
|
// lower GOGC from 100 to 50, unless it was manually overwritten by the user
|
|
oldValue := godebug.SetGCPercent(50)
|
|
if oldValue != 100 {
|
|
godebug.SetGCPercent(oldValue)
|
|
}
|
|
}
|
|
|
|
func docsURL(version string) string {
|
|
baseURL := "https://restic.readthedocs.io/en/"
|
|
|
|
if version == "" || version == "unknown" {
|
|
return baseURL + "latest"
|
|
}
|
|
|
|
if strings.Contains(version, "dev") || strings.Contains(version, "compiled") {
|
|
return baseURL + "latest"
|
|
}
|
|
|
|
if matches := regexp.MustCompile(`^(\d+\.\d+\.\d+)`).FindStringSubmatch(version); len(matches) == 2 {
|
|
return fmt.Sprintf(baseURL+"v%s", matches[1])
|
|
}
|
|
|
|
return baseURL + "latest"
|
|
}
|
|
|
|
func printExitError(globalOptions global.Options, code int, message string) {
|
|
if globalOptions.JSON {
|
|
type jsonExitError struct {
|
|
MessageType string `json:"message_type"` // exit_error
|
|
Code int `json:"code"`
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
jsonS := jsonExitError{
|
|
MessageType: "exit_error",
|
|
Code: code,
|
|
Message: message,
|
|
}
|
|
|
|
b, err := json.Marshal(jsonS)
|
|
if err != nil {
|
|
// ignore error as there's no good way to handle it
|
|
_, _ = fmt.Fprintf(os.Stderr, "JSON encode failed: %v\n", err)
|
|
debug.Log("JSON encode failed: %v\n", err)
|
|
return
|
|
}
|
|
_, _ = fmt.Fprintf(os.Stderr, "%s\n", ui.EscapeControls(string(b)))
|
|
} else {
|
|
_, _ = fmt.Fprintf(os.Stderr, "%s\n", ui.EscapeControls(message))
|
|
}
|
|
}
|
|
|
|
// redactLoggedArgs heuristically hides repository passwords from the debug log.
|
|
func redactLoggedArgs(args []string, registry *location.Registry) []string {
|
|
redacted := make([]string, 0, len(args))
|
|
for _, arg := range args {
|
|
var cleaned string
|
|
if flag, value, ok := strings.Cut(arg, "="); ok && strings.HasPrefix(flag, "-") {
|
|
cleaned = flag + "=" + location.StripPassword(registry, value)
|
|
} else {
|
|
cleaned = location.StripPassword(registry, arg)
|
|
}
|
|
|
|
redacted = append(redacted, cleaned)
|
|
}
|
|
return redacted
|
|
}
|
|
|
|
func main() {
|
|
tweakGoGC()
|
|
// install custom global logger into a buffer, if an error occurs
|
|
// we can show the logs
|
|
logBuffer := bytes.NewBuffer(nil)
|
|
log.SetOutput(logBuffer)
|
|
|
|
err := feature.Flag.Apply(os.Getenv("RESTIC_FEATURES"), func(s string) {
|
|
_, _ = fmt.Fprintln(os.Stderr, s)
|
|
})
|
|
if err != nil {
|
|
_, _ = fmt.Fprintln(os.Stderr, err)
|
|
Exit(1)
|
|
}
|
|
|
|
backends := all.Backends()
|
|
debug.Log("main %#v", redactLoggedArgs(os.Args, backends))
|
|
debug.Log("restic %s compiled with %v on %v/%v",
|
|
global.Version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
|
|
|
globalOptions := global.Options{
|
|
Backends: backends,
|
|
}
|
|
func() {
|
|
term, cancel := termstatus.Setup(os.Stdin, os.Stdout, os.Stderr, globalOptions.Quiet)
|
|
defer cancel()
|
|
globalOptions.Term = term
|
|
ctx := createGlobalContext(os.Stderr)
|
|
err = newRootCommand(&globalOptions).ExecuteContext(ctx)
|
|
switch err {
|
|
case nil:
|
|
err = ctx.Err()
|
|
case ErrOK:
|
|
// ErrOK overwrites context cancellation errors
|
|
err = nil
|
|
}
|
|
}()
|
|
|
|
var exitMessage string
|
|
switch {
|
|
case repository.IsAlreadyLocked(err):
|
|
exitMessage = fmt.Sprintf("%v\nthe `unlock` command can be used to remove stale locks", err)
|
|
case err == ErrInvalidSourceData:
|
|
exitMessage = fmt.Sprintf("Warning: %v", err)
|
|
case errors.IsFatal(err):
|
|
exitMessage = err.Error()
|
|
case errors.Is(err, repository.ErrNoKeyFound):
|
|
exitMessage = fmt.Sprintf("Fatal: %v", err)
|
|
case err != nil:
|
|
exitMessage = fmt.Sprintf("%+v", err)
|
|
|
|
if logBuffer.Len() > 0 {
|
|
exitMessage += " also, the following messages were logged by a library:\n"
|
|
sc := bufio.NewScanner(logBuffer)
|
|
for sc.Scan() {
|
|
exitMessage += fmt.Sprintln(sc.Text())
|
|
}
|
|
}
|
|
}
|
|
|
|
var exitCode int
|
|
switch {
|
|
case err == nil:
|
|
exitCode = 0
|
|
case err == ErrInvalidSourceData:
|
|
exitCode = 3
|
|
case errors.Is(err, ErrFailedToRemoveOneOrMoreSnapshots):
|
|
exitCode = 3
|
|
case errors.Is(err, global.ErrNoRepository):
|
|
exitCode = 10
|
|
case repository.IsAlreadyLocked(err):
|
|
exitCode = 11
|
|
case errors.Is(err, repository.ErrNoKeyFound):
|
|
exitCode = 12
|
|
case errors.Is(err, context.Canceled):
|
|
exitCode = 130
|
|
default:
|
|
exitCode = 1
|
|
}
|
|
|
|
if exitCode != 0 {
|
|
printExitError(globalOptions, exitCode, exitMessage)
|
|
}
|
|
Exit(exitCode)
|
|
}
|