Skip to content

Commit

Permalink
add backup subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanielc committed Apr 10, 2017
1 parent a314c5d commit 9eaf7e3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
6 changes: 2 additions & 4 deletions client/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
"path"
Expand Down Expand Up @@ -2185,13 +2184,12 @@ func (c *Client) DoStorageAction(l Link, opt StorageActionOptions) error {
return nil
}

// StorageBackup requests a backup of all storage from Kapacitor.
// Backup requests a backup of all storage from Kapacitor.
// A short read is possible, to verify that the backup was successful
// check that the number of bytes read matches the returned size.
func (c *Client) StorageBackup() (int64, io.ReadCloser, error) {
func (c *Client) Backup() (int64, io.ReadCloser, error) {
u := *c.url
u.Path = backupPath
log.Println("D! PATH", backupPath)

req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
Expand Down
40 changes: 40 additions & 0 deletions cmd/kapacitor/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Commands:
show-template Display detailed information about a template.
show-topic-handler Display detailed information about an alert handler for a topic.
show-topic Display detailed information about an alert topic.
backup Backup the Kapacitor database.
level Sets the logging level on the kapacitord server.
stats Display various stats about Kapacitor.
version Displays the Kapacitor version info.
Expand Down Expand Up @@ -180,6 +181,9 @@ func main() {
case "show-topic":
commandArgs = args
commandF = doShowTopic
case "backup":
commandArgs = args
commandF = doBackup
case "level":
commandArgs = args
commandF = doLevel
Expand Down Expand Up @@ -278,6 +282,8 @@ func doHelp(args []string) error {
showTopicHandlerUsage()
case "show-topic":
showTopicUsage()
case "backup":
backupUsage()
case "level":
levelUsage()
case "help":
Expand Down Expand Up @@ -2206,3 +2212,37 @@ func doServiceTest(args []string) error {
}
return nil
}

// Backup
func backupUsage() {
var u = `Usage: kapacitor backup <output file>
Perform a backup of the Kapacitor database.
To restore a database first stop Kapacitor, then replace the existing kapacitor.db file with the backup file.
`
fmt.Fprintln(os.Stderr, u)
}

func doBackup(args []string) error {
if len(args) != 1 {
return errors.New("must provide file path for backup.")
}
f, err := os.Create(args[0])
if err != nil {
return errors.Wrap(err, "failed to create backup file")
}
defer f.Close()
size, backup, err := cli.Backup()
if err != nil {
return errors.Wrap(err, "failed to perform backup")
}
n, err := io.Copy(f, backup)
if err != nil {
return errors.Wrap(err, "failed to save backup")
}
if n != size {
return fmt.Errorf("failed to download entire backup, only wrote %d bytes out of a total %d bytes.", n, size)
}
return nil
}
2 changes: 1 addition & 1 deletion server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9021,7 +9021,7 @@ func TestStorage_Backup(t *testing.T) {
}

// Perform backup
size, r, err := cli.StorageBackup()
size, r, err := cli.Backup()
if err != nil {
t.Fatal(err)
}
Expand Down
4 changes: 3 additions & 1 deletion usr/share/bash-completion/completions/kapacitor
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ _kapacitor()
show-topic)
words=$(_kapacitor_list topics "$cur")
;;
backup)
;;
level)
words='debug info warn error'
;;
Expand All @@ -200,7 +202,7 @@ _kapacitor()
;;
*)
words='record define define-template define-topic-handler replay replay-live enable disable \
reload delete list show show-template show-topic-handler show-topic level stats version vars service-tests help'
reload delete list show show-template show-topic-handler show-topic backup level stats version vars service-tests help'
;;
esac
if [ -z "$COMPREPLY" ]
Expand Down

0 comments on commit 9eaf7e3

Please sign in to comment.