snapshot_command.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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/pkg/logger"
  21. "github.com/coreos/etcd/pkg/types"
  22. "github.com/coreos/etcd/snapshot"
  23. "github.com/spf13/cobra"
  24. )
  25. const (
  26. defaultName = "default"
  27. defaultInitialAdvertisePeerURLs = "http://localhost:2380"
  28. )
  29. var (
  30. restoreCluster string
  31. restoreClusterToken string
  32. restoreDataDir string
  33. restoreWalDir string
  34. restorePeerURLs string
  35. restoreName string
  36. skipHashCheck bool
  37. )
  38. // NewSnapshotCommand returns the cobra command for "snapshot".
  39. func NewSnapshotCommand() *cobra.Command {
  40. cmd := &cobra.Command{
  41. Use: "snapshot <subcommand>",
  42. Short: "Manages etcd node snapshots",
  43. }
  44. cmd.AddCommand(NewSnapshotSaveCommand())
  45. cmd.AddCommand(NewSnapshotRestoreCommand())
  46. cmd.AddCommand(newSnapshotStatusCommand())
  47. return cmd
  48. }
  49. func NewSnapshotSaveCommand() *cobra.Command {
  50. return &cobra.Command{
  51. Use: "save <filename>",
  52. Short: "Stores an etcd node backend snapshot to a given file",
  53. Run: snapshotSaveCommandFunc,
  54. }
  55. }
  56. func newSnapshotStatusCommand() *cobra.Command {
  57. return &cobra.Command{
  58. Use: "status <filename>",
  59. Short: "Gets backend snapshot status of a given file",
  60. Long: `When --write-out is set to simple, this command prints out comma-separated status lists for each endpoint.
  61. The items in the lists are hash, revision, total keys, total size.
  62. `,
  63. Run: snapshotStatusCommandFunc,
  64. }
  65. }
  66. func NewSnapshotRestoreCommand() *cobra.Command {
  67. cmd := &cobra.Command{
  68. Use: "restore <filename> [options]",
  69. Short: "Restores an etcd member snapshot to an etcd directory",
  70. Run: snapshotRestoreCommandFunc,
  71. }
  72. cmd.Flags().StringVar(&restoreDataDir, "data-dir", "", "Path to the data directory")
  73. cmd.Flags().StringVar(&restoreWalDir, "wal-dir", "", "Path to the WAL directory (use --data-dir if none given)")
  74. cmd.Flags().StringVar(&restoreCluster, "initial-cluster", initialClusterFromName(defaultName), "Initial cluster configuration for restore bootstrap")
  75. cmd.Flags().StringVar(&restoreClusterToken, "initial-cluster-token", "etcd-cluster", "Initial cluster token for the etcd cluster during restore bootstrap")
  76. cmd.Flags().StringVar(&restorePeerURLs, "initial-advertise-peer-urls", defaultInitialAdvertisePeerURLs, "List of this member's peer URLs to advertise to the rest of the cluster")
  77. cmd.Flags().StringVar(&restoreName, "name", defaultName, "Human-readable name for this member")
  78. cmd.Flags().BoolVar(&skipHashCheck, "skip-hash-check", false, "Ignore snapshot integrity hash value (required if copied from data directory)")
  79. return cmd
  80. }
  81. func snapshotSaveCommandFunc(cmd *cobra.Command, args []string) {
  82. if len(args) != 1 {
  83. err := fmt.Errorf("snapshot save expects one argument")
  84. ExitWithError(ExitBadArgs, err)
  85. }
  86. lg := logger.NewDiscardLogger()
  87. debug, err := cmd.Flags().GetBool("debug")
  88. if err != nil {
  89. ExitWithError(ExitError, err)
  90. }
  91. if debug {
  92. lg = logger.NewPackageLogger("github.com/coreos/etcd", "snapshot")
  93. }
  94. sp := snapshot.NewV3(mustClientFromCmd(cmd), lg)
  95. path := args[0]
  96. if err := sp.Save(context.TODO(), 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. lg := logger.NewDiscardLogger()
  108. debug, err := cmd.Flags().GetBool("debug")
  109. if err != nil {
  110. ExitWithError(ExitError, err)
  111. }
  112. if debug {
  113. lg = logger.NewPackageLogger("github.com/coreos/etcd", "snapshot")
  114. }
  115. sp := snapshot.NewV3(nil, 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. urlmap, uerr := types.NewURLsMap(restoreCluster)
  128. if uerr != nil {
  129. ExitWithError(ExitBadArgs, uerr)
  130. }
  131. dataDir := restoreDataDir
  132. if dataDir == "" {
  133. dataDir = restoreName + ".etcd"
  134. }
  135. walDir := restoreWalDir
  136. if walDir == "" {
  137. walDir = filepath.Join(dataDir, "member", "wal")
  138. }
  139. lg := logger.NewDiscardLogger()
  140. debug, err := cmd.Flags().GetBool("debug")
  141. if err != nil {
  142. ExitWithError(ExitError, err)
  143. }
  144. if debug {
  145. lg = logger.NewPackageLogger("github.com/coreos/etcd", "snapshot")
  146. }
  147. sp := snapshot.NewV3(nil, lg)
  148. if err := sp.Restore(args[0], snapshot.RestoreConfig{
  149. Name: restoreName,
  150. OutputDataDir: dataDir,
  151. OutputWALDir: walDir,
  152. InitialCluster: urlmap,
  153. InitialClusterToken: restoreClusterToken,
  154. PeerURLs: types.MustNewURLs(strings.Split(restorePeerURLs, ",")),
  155. SkipHashCheck: skipHashCheck,
  156. }); err != nil {
  157. ExitWithError(ExitError, err)
  158. }
  159. }
  160. func initialClusterFromName(name string) string {
  161. n := name
  162. if name == "" {
  163. n = defaultName
  164. }
  165. return fmt.Sprintf("%s=http://localhost:2380", n)
  166. }