From a4f282c8e83a2447f22cdf230d90f0e91df880b6 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Wed, 19 May 2021 15:04:36 -0600 Subject: [PATCH] Allow backup to stdout --- cmd/backup.go | 4 ++-- ctl/backup.go | 24 ++++++++++++++++-------- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/cmd/backup.go b/cmd/backup.go index 8e272c526..d91975aa5 100644 --- a/cmd/backup.go +++ b/cmd/backup.go @@ -28,7 +28,7 @@ func newBackupCommand(stdin io.Reader, stdout io.Writer, stderr io.Writer) *cobr Use: "backup", Short: "Back up pilosa server", Long: ` -Backs up a pilosa server to a local snapshot file. +Backs up a pilosa server to a local, tar-formatted snapshot file. `, RunE: func(c *cobra.Command, args []string) error { return cmd.Run(context.Background()) @@ -36,7 +36,7 @@ Backs up a pilosa server to a local snapshot file. } flags := ccmd.Flags() - flags.StringVarP(&cmd.OutputPath, "output", "o", "", "output path to write to") + flags.StringVarP(&cmd.OutputPath, "output", "o", "", "output path to write to; specify '-' to send to stdout") flags.StringVar(&cmd.Host, "host", "localhost:10101", "host:port of Pilosa.") ctl.SetTLSConfig(flags, "", &cmd.TLS.CertificatePath, &cmd.TLS.CertificateKeyPath, &cmd.TLS.CACertPath, &cmd.TLS.SkipVerify, &cmd.TLS.EnableClientVerification) return ccmd diff --git a/ctl/backup.go b/ctl/backup.go index de8599020..644b295f6 100644 --- a/ctl/backup.go +++ b/ctl/backup.go @@ -73,6 +73,7 @@ func (cmd *BackupCommand) Run(ctx context.Context) (err error) { if cmd.OutputPath == "" { return fmt.Errorf("-o flag required") } + useStdout := cmd.OutputPath == "-" // Parse TLS configuration for node-specific clients. tls := cmd.TLSConfiguration() @@ -94,12 +95,17 @@ func (cmd *BackupCommand) Run(ctx context.Context) (err error) { } schema := &pilosa.Schema{Indexes: indexes} - // Create output file in temporary location. - w, err := os.Create(cmd.OutputPath + ".tmp") - if err != nil { - return err + // Create output file in temporary location, or send to stdout if a dash is specified. + var w io.Writer + if useStdout { + w = os.Stdout + } else { + f, err := os.Create(cmd.OutputPath + ".tmp") + if err != nil { + return err + } + defer f.Close() } - defer w.Close() // Open a tar writer to the temporary file. tw := tar.NewWriter(w) @@ -125,9 +131,11 @@ func (cmd *BackupCommand) Run(ctx context.Context) (err error) { } // Move data file to final location. - logger.Printf("writing backup: %s", cmd.OutputPath) - if err := os.Rename(cmd.OutputPath+".tmp", cmd.OutputPath); err != nil { - return err + if !useStdout { + logger.Printf("writing backup: %s", cmd.OutputPath) + if err := os.Rename(cmd.OutputPath+".tmp", cmd.OutputPath); err != nil { + return err + } } return nil