mirror of
https://github.com/octoleo/restic.git
synced 2024-11-01 03:12:31 +00:00
Extract parameters for second repository from copy command
This commit is contained in:
parent
1823b8195c
commit
655430550b
@ -3,10 +3,8 @@ package main
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
|
|
||||||
"github.com/restic/restic/internal/debug"
|
"github.com/restic/restic/internal/debug"
|
||||||
"github.com/restic/restic/internal/errors"
|
|
||||||
"github.com/restic/restic/internal/restic"
|
"github.com/restic/restic/internal/restic"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -29,11 +27,7 @@ their deduplication.
|
|||||||
|
|
||||||
// CopyOptions bundles all options for the copy command.
|
// CopyOptions bundles all options for the copy command.
|
||||||
type CopyOptions struct {
|
type CopyOptions struct {
|
||||||
Repo string
|
secondaryRepoOptions
|
||||||
password string
|
|
||||||
PasswordFile string
|
|
||||||
PasswordCommand string
|
|
||||||
KeyHint string
|
|
||||||
Hosts []string
|
Hosts []string
|
||||||
Tags restic.TagLists
|
Tags restic.TagLists
|
||||||
Paths []string
|
Paths []string
|
||||||
@ -45,35 +39,14 @@ func init() {
|
|||||||
cmdRoot.AddCommand(cmdCopy)
|
cmdRoot.AddCommand(cmdCopy)
|
||||||
|
|
||||||
f := cmdCopy.Flags()
|
f := cmdCopy.Flags()
|
||||||
f.StringVarP(©Options.Repo, "repo2", "", os.Getenv("RESTIC_REPOSITORY2"), "destination repository to copy snapshots to (default: $RESTIC_REPOSITORY2)")
|
initSecondaryRepoOptions(f, ©Options.secondaryRepoOptions, "destination", "to copy snapshots to")
|
||||||
f.StringVarP(©Options.PasswordFile, "password-file2", "", os.Getenv("RESTIC_PASSWORD_FILE2"), "`file` to read the destination repository password from (default: $RESTIC_PASSWORD_FILE2)")
|
|
||||||
f.StringVarP(©Options.KeyHint, "key-hint2", "", os.Getenv("RESTIC_KEY_HINT2"), "key ID of key to try decrypting the destination repository first (default: $RESTIC_KEY_HINT2)")
|
|
||||||
f.StringVarP(©Options.PasswordCommand, "password-command2", "", os.Getenv("RESTIC_PASSWORD_COMMAND2"), "shell `command` to obtain the destination repository password from (default: $RESTIC_PASSWORD_COMMAND2)")
|
|
||||||
|
|
||||||
f.StringArrayVarP(©Options.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when no snapshot ID is given (can be specified multiple times)")
|
f.StringArrayVarP(©Options.Hosts, "host", "H", nil, "only consider snapshots for this `host`, when no snapshot ID is given (can be specified multiple times)")
|
||||||
f.Var(©Options.Tags, "tag", "only consider snapshots which include this `taglist`, when no snapshot ID is given")
|
f.Var(©Options.Tags, "tag", "only consider snapshots which include this `taglist`, when no snapshot ID is given")
|
||||||
f.StringArrayVar(©Options.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot ID is given")
|
f.StringArrayVar(©Options.Paths, "path", nil, "only consider snapshots which include this (absolute) `path`, when no snapshot ID is given")
|
||||||
}
|
}
|
||||||
|
|
||||||
func runCopy(opts CopyOptions, gopts GlobalOptions, args []string) error {
|
func runCopy(opts CopyOptions, gopts GlobalOptions, args []string) error {
|
||||||
if opts.Repo == "" {
|
dstGopts, err := fillSecondaryGlobalOpts(opts.secondaryRepoOptions, gopts, "destination")
|
||||||
return errors.Fatal("Please specify a destination repository location (--repo2)")
|
|
||||||
}
|
|
||||||
var err error
|
|
||||||
dstGopts := gopts
|
|
||||||
dstGopts.Repo = opts.Repo
|
|
||||||
dstGopts.PasswordFile = opts.PasswordFile
|
|
||||||
dstGopts.PasswordCommand = opts.PasswordCommand
|
|
||||||
dstGopts.KeyHint = opts.KeyHint
|
|
||||||
if opts.password != "" {
|
|
||||||
dstGopts.password = opts.password
|
|
||||||
} else {
|
|
||||||
dstGopts.password, err = resolvePassword(dstGopts, "RESTIC_PASSWORD2")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
dstGopts.password, err = ReadPassword(dstGopts, "enter password for destination repository: ")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -616,8 +616,10 @@ func TestBackupTags(t *testing.T) {
|
|||||||
|
|
||||||
func testRunCopy(t testing.TB, srcGopts GlobalOptions, dstGopts GlobalOptions) {
|
func testRunCopy(t testing.TB, srcGopts GlobalOptions, dstGopts GlobalOptions) {
|
||||||
copyOpts := CopyOptions{
|
copyOpts := CopyOptions{
|
||||||
|
secondaryRepoOptions: secondaryRepoOptions{
|
||||||
Repo: dstGopts.Repo,
|
Repo: dstGopts.Repo,
|
||||||
password: dstGopts.password,
|
password: dstGopts.password,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
rtest.OK(t, runCopy(copyOpts, srcGopts, nil))
|
rtest.OK(t, runCopy(copyOpts, srcGopts, nil))
|
||||||
|
48
cmd/restic/secondary_repo.go
Normal file
48
cmd/restic/secondary_repo.go
Normal file
@ -0,0 +1,48 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/restic/restic/internal/errors"
|
||||||
|
"github.com/spf13/pflag"
|
||||||
|
)
|
||||||
|
|
||||||
|
type secondaryRepoOptions struct {
|
||||||
|
Repo string
|
||||||
|
password string
|
||||||
|
PasswordFile string
|
||||||
|
PasswordCommand string
|
||||||
|
KeyHint string
|
||||||
|
}
|
||||||
|
|
||||||
|
func initSecondaryRepoOptions(f *pflag.FlagSet, opts *secondaryRepoOptions, repoPrefix string, repoUsage string) {
|
||||||
|
f.StringVarP(&opts.Repo, "repo2", "", os.Getenv("RESTIC_REPOSITORY2"), repoPrefix+" repository "+repoUsage+" (default: $RESTIC_REPOSITORY2)")
|
||||||
|
f.StringVarP(&opts.PasswordFile, "password-file2", "", os.Getenv("RESTIC_PASSWORD_FILE2"), "`file` to read the "+repoPrefix+" repository password from (default: $RESTIC_PASSWORD_FILE2)")
|
||||||
|
f.StringVarP(&opts.KeyHint, "key-hint2", "", os.Getenv("RESTIC_KEY_HINT2"), "key ID of key to try decrypting the "+repoPrefix+" repository first (default: $RESTIC_KEY_HINT2)")
|
||||||
|
f.StringVarP(&opts.PasswordCommand, "password-command2", "", os.Getenv("RESTIC_PASSWORD_COMMAND2"), "shell `command` to obtain the "+repoPrefix+" repository password from (default: $RESTIC_PASSWORD_COMMAND2)")
|
||||||
|
}
|
||||||
|
|
||||||
|
func fillSecondaryGlobalOpts(opts secondaryRepoOptions, gopts GlobalOptions, repoPrefix string) (GlobalOptions, error) {
|
||||||
|
if opts.Repo == "" {
|
||||||
|
return GlobalOptions{}, errors.Fatal("Please specify a " + repoPrefix + " repository location (--repo2)")
|
||||||
|
}
|
||||||
|
var err error
|
||||||
|
dstGopts := gopts
|
||||||
|
dstGopts.Repo = opts.Repo
|
||||||
|
dstGopts.PasswordFile = opts.PasswordFile
|
||||||
|
dstGopts.PasswordCommand = opts.PasswordCommand
|
||||||
|
dstGopts.KeyHint = opts.KeyHint
|
||||||
|
if opts.password != "" {
|
||||||
|
dstGopts.password = opts.password
|
||||||
|
} else {
|
||||||
|
dstGopts.password, err = resolvePassword(dstGopts, "RESTIC_PASSWORD2")
|
||||||
|
if err != nil {
|
||||||
|
return GlobalOptions{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
dstGopts.password, err = ReadPassword(dstGopts, "enter password for "+repoPrefix+" repository: ")
|
||||||
|
if err != nil {
|
||||||
|
return GlobalOptions{}, err
|
||||||
|
}
|
||||||
|
return dstGopts, nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user