2014-04-27 22:00:15 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2017-05-01 08:09:24 +00:00
|
|
|
"bufio"
|
|
|
|
"bytes"
|
2015-07-12 20:10:01 +00:00
|
|
|
"fmt"
|
2017-05-01 08:09:24 +00:00
|
|
|
"log"
|
2016-08-03 18:29:08 +00:00
|
|
|
"os"
|
2017-06-11 12:17:44 +00:00
|
|
|
"runtime"
|
2023-06-02 19:51:50 +00:00
|
|
|
godebug "runtime/debug"
|
2016-08-21 15:46:23 +00:00
|
|
|
|
2016-09-17 10:36:05 +00:00
|
|
|
"github.com/spf13/cobra"
|
2023-10-22 14:59:45 +00:00
|
|
|
"go.uber.org/automaxprocs/maxprocs"
|
2016-09-01 20:17:37 +00:00
|
|
|
|
2023-09-28 17:45:58 +00:00
|
|
|
"github.com/restic/restic/internal/debug"
|
2017-07-23 12:21:03 +00:00
|
|
|
"github.com/restic/restic/internal/errors"
|
2023-09-28 17:45:58 +00:00
|
|
|
"github.com/restic/restic/internal/options"
|
|
|
|
"github.com/restic/restic/internal/restic"
|
2014-04-27 22:00:15 +00:00
|
|
|
)
|
|
|
|
|
2023-10-22 14:59:45 +00:00
|
|
|
func init() {
|
|
|
|
// don't import `go.uber.org/automaxprocs` to disable the log output
|
|
|
|
_, _ = maxprocs.Set()
|
|
|
|
}
|
|
|
|
|
2023-07-23 10:09:01 +00:00
|
|
|
// cmdRoot is the base command when no other command has been specified.
|
|
|
|
var cmdRoot = &cobra.Command{
|
|
|
|
Use: "restic",
|
|
|
|
Short: "Backup and restore files",
|
|
|
|
Long: `
|
2016-09-17 10:36:05 +00:00
|
|
|
restic is a backup program which allows saving multiple revisions of files and
|
|
|
|
directories in an encrypted repository stored on different backends.
|
2021-01-11 03:50:02 +00:00
|
|
|
|
2023-07-23 10:09:01 +00:00
|
|
|
The full documentation can be found at https://restic.readthedocs.io/ .
|
|
|
|
`,
|
2017-08-06 19:02:16 +00:00
|
|
|
SilenceErrors: true,
|
|
|
|
SilenceUsage: true,
|
|
|
|
DisableAutoGenTag: true,
|
2017-01-22 18:10:32 +00:00
|
|
|
|
2018-03-08 21:55:03 +00:00
|
|
|
PersistentPreRunE: func(c *cobra.Command, args []string) error {
|
2018-04-22 09:57:20 +00:00
|
|
|
// set verbosity, default is one
|
2018-04-21 20:07:14 +00:00
|
|
|
globalOptions.verbosity = 1
|
2020-12-30 20:24:18 +00:00
|
|
|
if globalOptions.Quiet && globalOptions.Verbose > 0 {
|
2018-04-22 09:57:20 +00:00
|
|
|
return errors.Fatal("--quiet and --verbose cannot be specified at the same time")
|
2018-04-21 20:07:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
switch {
|
2018-04-22 09:57:20 +00:00
|
|
|
case globalOptions.Verbose >= 2:
|
|
|
|
globalOptions.verbosity = 3
|
|
|
|
case globalOptions.Verbose > 0:
|
|
|
|
globalOptions.verbosity = 2
|
2018-04-21 20:07:14 +00:00
|
|
|
case globalOptions.Quiet:
|
|
|
|
globalOptions.verbosity = 0
|
|
|
|
}
|
|
|
|
|
2017-03-25 14:33:52 +00:00
|
|
|
// parse extended options
|
|
|
|
opts, err := options.Parse(globalOptions.Options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
globalOptions.extended = opts
|
2020-10-05 14:03:51 +00:00
|
|
|
if !needsPassword(c.Name()) {
|
2018-03-08 21:55:03 +00:00
|
|
|
return nil
|
|
|
|
}
|
2019-08-13 16:27:20 +00:00
|
|
|
pwd, err := resolvePassword(globalOptions, "RESTIC_PASSWORD")
|
2017-07-24 21:01:16 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Fprintf(os.Stderr, "Resolving password failed: %v\n", err)
|
|
|
|
Exit(1)
|
|
|
|
}
|
|
|
|
globalOptions.password = pwd
|
|
|
|
|
2017-03-25 14:33:52 +00:00
|
|
|
// run the debug functions for all subcommands (if build tag "debug" is
|
|
|
|
// enabled)
|
2023-05-18 16:07:19 +00:00
|
|
|
return runDebug()
|
2017-01-23 16:52:26 +00:00
|
|
|
},
|
2016-09-17 10:36:05 +00:00
|
|
|
}
|
|
|
|
|
2020-10-05 14:03:51 +00:00
|
|
|
// 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 {
|
2023-10-19 20:35:24 +00:00
|
|
|
case "cache", "generate", "help", "options", "self-update", "version", "__complete":
|
2020-10-05 14:03:51 +00:00
|
|
|
return false
|
|
|
|
default:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-01 08:09:24 +00:00
|
|
|
var logBuffer = bytes.NewBuffer(nil)
|
|
|
|
|
2023-06-02 19:51:50 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-10 15:08:27 +00:00
|
|
|
func main() {
|
2023-06-02 19:51:50 +00:00
|
|
|
tweakGoGC()
|
2017-05-01 08:09:24 +00:00
|
|
|
// install custom global logger into a buffer, if an error occurs
|
|
|
|
// we can show the logs
|
|
|
|
log.SetOutput(logBuffer)
|
|
|
|
|
2016-09-27 20:35:08 +00:00
|
|
|
debug.Log("main %#v", os.Args)
|
2018-03-21 20:49:03 +00:00
|
|
|
debug.Log("restic %s compiled with %v on %v/%v",
|
2017-06-11 12:17:44 +00:00
|
|
|
version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
|
2022-10-02 21:24:37 +00:00
|
|
|
err := cmdRoot.ExecuteContext(internalGlobalCtx)
|
2016-08-28 20:19:48 +00:00
|
|
|
|
|
|
|
switch {
|
2022-06-13 18:35:37 +00:00
|
|
|
case restic.IsAlreadyLocked(err):
|
2016-08-28 20:19:48 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%v\nthe `unlock` command can be used to remove stale locks\n", err)
|
2020-07-28 20:57:40 +00:00
|
|
|
case err == ErrInvalidSourceData:
|
|
|
|
fmt.Fprintf(os.Stderr, "Warning: %v\n", err)
|
2022-02-12 22:31:31 +00:00
|
|
|
case errors.IsFatal(err):
|
2016-08-28 20:19:48 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%v\n", err)
|
|
|
|
case err != nil:
|
2016-08-21 16:07:13 +00:00
|
|
|
fmt.Fprintf(os.Stderr, "%+v\n", err)
|
2017-05-01 08:09:24 +00:00
|
|
|
|
|
|
|
if logBuffer.Len() > 0 {
|
|
|
|
fmt.Fprintf(os.Stderr, "also, the following messages were logged by a library:\n")
|
|
|
|
sc := bufio.NewScanner(logBuffer)
|
|
|
|
for sc.Scan() {
|
|
|
|
fmt.Fprintln(os.Stderr, sc.Text())
|
|
|
|
}
|
|
|
|
}
|
2016-01-17 15:59:03 +00:00
|
|
|
}
|
|
|
|
|
2016-12-28 09:53:31 +00:00
|
|
|
var exitCode int
|
2020-01-12 15:21:17 +00:00
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
exitCode = 0
|
2020-07-28 20:57:40 +00:00
|
|
|
case ErrInvalidSourceData:
|
2020-01-12 15:21:17 +00:00
|
|
|
exitCode = 3
|
|
|
|
default:
|
2016-12-28 09:53:31 +00:00
|
|
|
exitCode = 1
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|
2016-12-28 09:53:31 +00:00
|
|
|
Exit(exitCode)
|
2014-04-27 22:00:15 +00:00
|
|
|
}
|