main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 main
  15. import (
  16. "fmt"
  17. "log"
  18. "os"
  19. "path/filepath"
  20. "strings"
  21. "time"
  22. "github.com/spf13/cobra"
  23. )
  24. var (
  25. rootCommand = &cobra.Command{
  26. Use: "etcd-dump-db",
  27. Short: "etcd-dump-db inspects etcd db files.",
  28. }
  29. listBucketCommand = &cobra.Command{
  30. Use: "list-bucket [data dir or db file path]",
  31. Short: "bucket lists all buckets.",
  32. Run: listBucketCommandFunc,
  33. }
  34. iterateBucketCommand = &cobra.Command{
  35. Use: "iterate-bucket [data dir or db file path] [bucket name]",
  36. Short: "iterate-bucket lists key-value pairs in reverse order.",
  37. Run: iterateBucketCommandFunc,
  38. }
  39. getHashCommand = &cobra.Command{
  40. Use: "hash [data dir or db file path]",
  41. Short: "hash computes the hash of db file.",
  42. Run: getHashCommandFunc,
  43. }
  44. )
  45. var flockTimeout time.Duration
  46. var iterateBucketLimit uint64
  47. var iterateBucketDecode bool
  48. func init() {
  49. rootCommand.PersistentFlags().DurationVar(&flockTimeout, "timeout", 10*time.Second, "time to wait to obtain a file lock on db file, 0 to block indefinitely")
  50. iterateBucketCommand.PersistentFlags().Uint64Var(&iterateBucketLimit, "limit", 0, "max number of key-value pairs to iterate (0< to iterate all)")
  51. iterateBucketCommand.PersistentFlags().BoolVar(&iterateBucketDecode, "decode", false, "true to decode Protocol Buffer encoded data")
  52. rootCommand.AddCommand(listBucketCommand)
  53. rootCommand.AddCommand(iterateBucketCommand)
  54. rootCommand.AddCommand(getHashCommand)
  55. }
  56. func main() {
  57. if err := rootCommand.Execute(); err != nil {
  58. fmt.Fprintln(os.Stdout, err)
  59. os.Exit(1)
  60. }
  61. }
  62. func listBucketCommandFunc(cmd *cobra.Command, args []string) {
  63. if len(args) < 1 {
  64. log.Fatalf("Must provide at least 1 argument (got %v)", args)
  65. }
  66. dp := args[0]
  67. if !strings.HasSuffix(dp, "db") {
  68. dp = filepath.Join(snapDir(dp), "db")
  69. }
  70. if !existFileOrDir(dp) {
  71. log.Fatalf("%q does not exist", dp)
  72. }
  73. bts, err := getBuckets(dp)
  74. if err != nil {
  75. log.Fatal(err)
  76. }
  77. for _, b := range bts {
  78. fmt.Println(b)
  79. }
  80. }
  81. func iterateBucketCommandFunc(cmd *cobra.Command, args []string) {
  82. if len(args) != 2 {
  83. log.Fatalf("Must provide 2 arguments (got %v)", args)
  84. }
  85. dp := args[0]
  86. if !strings.HasSuffix(dp, "db") {
  87. dp = filepath.Join(snapDir(dp), "db")
  88. }
  89. if !existFileOrDir(dp) {
  90. log.Fatalf("%q does not exist", dp)
  91. }
  92. bucket := args[1]
  93. err := iterateBucket(dp, bucket, iterateBucketLimit, iterateBucketDecode)
  94. if err != nil {
  95. log.Fatal(err)
  96. }
  97. }
  98. func getHashCommandFunc(cmd *cobra.Command, args []string) {
  99. if len(args) < 1 {
  100. log.Fatalf("Must provide at least 1 argument (got %v)", args)
  101. }
  102. dp := args[0]
  103. if !strings.HasSuffix(dp, "db") {
  104. dp = filepath.Join(snapDir(dp), "db")
  105. }
  106. if !existFileOrDir(dp) {
  107. log.Fatalf("%q does not exist", dp)
  108. }
  109. hash, err := getHash(dp)
  110. if err != nil {
  111. log.Fatal(err)
  112. }
  113. fmt.Printf("db path: %s\nHash: %d\n", dp, hash)
  114. }