main.go 3.2 KB

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