snapshot_command.go 5.7 KB

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