Skip to content

Commit

Permalink
backport of commit e8f4bfe
Browse files Browse the repository at this point in the history
  • Loading branch information
absolutelightning committed Sep 5, 2023
1 parent dce0ab6 commit 86e6906
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 33 deletions.
29 changes: 4 additions & 25 deletions command/snapshot/save/snapshot_save.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type cmd struct {
func (c *cmd) getAppendFileNameFlag() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ContinueOnError)
fs.Var(&c.appendFileNameFlag, "append-filename", "Append filename flag supports the following "+
"comma-separated arguments. 1. version, 2. dc. 3. node 4. status. It appends these values to the filename provided in the command")
"comma-separated arguments. 1. version, 2. dc. It appends these values to the filename provided in the command")
return fs
}

Expand Down Expand Up @@ -75,7 +75,9 @@ func (c *cmd) Run(args []string) int {

if len(appendFileNameFlags) != 0 && len(c.appendFileNameFlag.String()) > 0 {
agentSelfResponse, err := client.Agent().Self()
if err != nil {
operatorHealthResponse, error := client.Operator().AutopilotServerHealth(nil)

if err != nil && error != nil {
c.UI.Error(fmt.Sprintf("Error connecting to Consul agent and fetching datacenter/version: %s", err))
return 1
}
Expand All @@ -99,29 +101,6 @@ func (c *cmd) Run(args []string) int {
}
}

if slices.Contains(appendFileNameFlags, "node") {
if config, ok := agentSelfResponse["Config"]; ok {
if nodeName, ok := config["NodeName"]; ok {
fileNameWithoutExt = fileNameWithoutExt + "-" + nodeName.(string)
}
}
}

if slices.Contains(appendFileNameFlags, "status") {
if status, ok := agentSelfResponse["Stats"]; ok {
if config, ok := status["consul"]; ok {
configMap := config.(map[string]interface{})
if leader, ok := configMap["leader"]; ok {
if leader == "true" {
fileNameWithoutExt = fileNameWithoutExt + "-" + "leader"
} else {
fileNameWithoutExt = fileNameWithoutExt + "-" + "follower"
}
}
}
}
}

//adding extension back
file = fileNameWithoutExt + fileExt
}
Expand Down
15 changes: 7 additions & 8 deletions website/content/commands/snapshot/save.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -77,20 +77,19 @@ $ consul snapshot save -stale backup.snap
# ...
```

To create snapshot file with consul version, datacenter, node name and leader/follower info,
run
This is useful for situations where a cluster is in a degraded state and no
leader is available. To target a specific server for a snapshot, you can run
the `consul snapshot save` command on that specific server.

To create snapshot file with consul version and datacenter run

```shell-session
$ consul snapshot save -append-filename node,status,version,dc backup.snap
$ consul snapshot save -append-filename version,dc backup.snap
#...
```

File name created will be like backup-%CONSUL_VERSION%-%DC_NAME%-%NODE_NAME%-%STATUS.snap
File name created will be like backup-%CONSUL_VERSION%-%DC_NAME%.snap
example - backup-1.17.0-dc1-local-machine-leader.tgz

This is useful for situations where a cluster is in a degraded state and no
leader is available. To target a specific server for a snapshot, you can run
the `consul snapshot save` command on that specific server.

Please see the [HTTP API](/consul/api-docs/snapshot) documentation for
more details about snapshot internals.

0 comments on commit 86e6906

Please sign in to comment.