snapshot_command.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. // Copyright 2016 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package command
  15. import (
  16. "context"
  17. "fmt"
  18. "path/filepath"
  19. "strings"
  20. "github.com/coreos/etcd/snapshot"
  21. "github.com/spf13/cobra"
  22. "go.uber.org/zap"
  23. )
  24. const (
  25. defaultName = "default"
  26. defaultInitialAdvertisePeerURLs = "http://localhost:2380"
  27. )
  28. var (
  29. restoreCluster string
  30. restoreClusterToken string
  31. restoreDataDir string
  32. restoreWalDir string
  33. restorePeerURLs string
  34. restoreName string
  35. skipHashCheck bool
  36. )
  37. // NewSnapshotCommand returns the cobra command for "snapshot".
  38. func NewSnapshotCommand() *cobra.Command {
  39. cmd := &cobra.Command{
  40. Use: "snapshot <subcommand>",
  41. Short: "Manages etcd node snapshots",
  42. }
  43. cmd.AddCommand(NewSnapshotSaveCommand())
  44. cmd.AddCommand(NewSnapshotRestoreCommand())
  45. cmd.AddCommand(newSnapshotStatusCommand())
  46. return cmd
  47. }
  48. func NewSnapshotSaveCommand() *cobra.Command {
  49. return &cobra.Command{
  50. Use: "save <filename>",
  51. Short: "Stores an etcd node backend snapshot to a given file",
  52. Run: snapshotSaveCommandFunc,
  53. }
  54. }
  55. func newSnapshotStatusCommand() *cobra.Command {
  56. return &cobra.Command{
  57. Use: "status <filename>",
  58. Short: "Gets backend snapshot status of a given file",
  59. Long: `When --write-out is set to simple, this command prints out comma-separated status lists for each endpoint.
  60. The items in the lists are hash, revision, total keys, total size.
  61. `,
  62. Run: snapshotStatusCommandFunc,
  63. }
  64. }
  65. func NewSnapshotRestoreCommand() *cobra.Command {
  66. cmd := &cobra.Command{
  67. Use: "restore <filename> [options]",
  68. Short: "Restores an etcd member snapshot to an etcd directory",
  69. Run: snapshotRestoreCommandFunc,
  70. }
  71. cmd.Flags().StringVar(&restoreDataDir, "data-dir", "", "Path to the data directory")
  72. cmd.Flags().StringVar(&restoreWalDir, "wal-dir", "", "Path to the WAL directory (use --data-dir if none given)")
  73. cmd.Flags().StringVar(&restoreCluster, "initial-cluster", initialClusterFromName(defaultName), "Initial cluster configuration for restore bootstrap")
  74. cmd.Flags().StringVar(&restoreClusterToken, "initial-cluster-token", "etcd-cluster", "Initial cluster token for the etcd cluster during restore bootstrap")
  75. cmd.Flags().StringVar(&restorePeerURLs, "initial-advertise-peer-urls", defaultInitialAdvertisePeerURLs, "List of this member's peer URLs to advertise to the rest of the cluster")
  76. cmd.Flags().StringVar(&restoreName, "name", defaultName, "Human-readable name for this member")
  77. cmd.Flags().BoolVar(&skipHashCheck, "skip-hash-check", false, "Ignore snapshot integrity hash value (required if copied from data directory)")
  78. return cmd
  79. }
  80. func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
  81. if len(args) != 1 {
  82. err := fmt.Errorf("snapshot save expects one argument")
  83. ExitWithError(ExitBadArgs, err)
  84. }
  85. debug, err := cmd.Flags().GetBool("debug")
  86. if err != nil {
  87. ExitWithError(ExitError, err)
  88. }
  89. lg := zap.NewNop()
  90. if debug {
  91. lg = zap.NewExample()
  92. }
  93. sp := snapshot.NewV3(lg)
  94. cfg := mustClientCfgFromCmd(cmd)
  95. path := args[0]
  96. if err := sp.Save(context.TODO(), *cfg, path); err != nil {
  97. ExitWithError(ExitInterrupted, err)
  98. }
  99. fmt.Printf("Snapshot saved at %s\n", path)
  100. }
  101. func snapshotStatusCommandFunc(cmd *cobra.Command, args []string) {
  102. if len(args) != 1 {
  103. err := fmt.Errorf("snapshot status requires exactly one argument")
  104. ExitWithError(ExitBadArgs, err)
  105. }
  106. initDisplayFromCmd(cmd)
  107. debug, err := cmd.Flags().GetBool("debug")
  108. if err != nil {
  109. ExitWithError(ExitError, err)
  110. }
  111. lg := zap.NewNop()
  112. if debug {
  113. lg = zap.NewExample()
  114. }
  115. sp := snapshot.NewV3(lg)
  116. ds, err := sp.Status(args[0])
  117. if err != nil {
  118. ExitWithError(ExitError, err)
  119. }
  120. display.DBStatus(ds)
  121. }
  122. func snapshotRestoreCommandFunc(cmd *cobra.Command, args []string) {
  123. if len(args) != 1 {
  124. err := fmt.Errorf("snapshot restore requires exactly one argument")
  125. ExitWithError(ExitBadArgs, err)
  126. }
  127. dataDir := restoreDataDir
  128. if dataDir == "" {
  129. dataDir = restoreName + ".etcd"
  130. }
  131. walDir := restoreWalDir
  132. if walDir == "" {
  133. walDir = filepath.Join(dataDir, "member", "wal")
  134. }
  135. debug, err := cmd.Flags().GetBool("debug")
  136. if err != nil {
  137. ExitWithError(ExitError, err)
  138. }
  139. lg := zap.NewNop()
  140. if debug {
  141. lg = zap.NewExample()
  142. }
  143. sp := snapshot.NewV3(lg)
  144. if err := sp.Restore(snapshot.RestoreConfig{
  145. SnapshotPath: args[0],
  146. Name: restoreName,
  147. OutputDataDir: dataDir,
  148. OutputWALDir: walDir,
  149. PeerURLs: strings.Split(restorePeerURLs, ","),
  150. InitialCluster: restoreCluster,
  151. InitialClusterToken: restoreClusterToken,
  152. SkipHashCheck: skipHashCheck,
  153. }); err != nil {
  154. ExitWithError(ExitError, err)
  155. }
  156. }
  157. func initialClusterFromName(name string) string {
  158. n := name
  159. if name == "" {
  160. n = defaultName
  161. }
  162. return fmt.Sprintf("%s=http://localhost:2380", n)
  163. }