main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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]",
  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 (
  45. iterateBucketName string
  46. iterateBucketLimit uint64
  47. )
  48. func init() {
  49. iterateBucketCommand.PersistentFlags().StringVar(&iterateBucketName, "bucket", "", "bucket name to iterate")
  50. iterateBucketCommand.PersistentFlags().Uint64Var(&iterateBucketLimit, "limit", 0, "max number of key-value pairs to iterate (0< to iterate all)")
  51. rootCommand.AddCommand(listBucketCommand)
  52. rootCommand.AddCommand(iterateBucketCommand)
  53. rootCommand.AddCommand(getHashCommand)
  54. }
  55. func main() {
  56. if err := rootCommand.Execute(); err != nil {
  57. fmt.Fprintln(os.Stdout, err)
  58. os.Exit(1)
  59. }
  60. }
  61. func listBucketCommandFunc(cmd *cobra.Command, args []string) {
  62. if len(args) < 1 {
  63. log.Fatalf("Must provide at least 1 argument (got %v)", args)
  64. }
  65. dp := args[0]
  66. if !strings.HasSuffix(dp, "db") {
  67. dp = filepath.Join(snapDir(dp), "db")
  68. }
  69. if !existFileOrDir(dp) {
  70. log.Fatalf("%q does not exist", dp)
  71. }
  72. bts, err := getBuckets(dp)
  73. if err != nil {
  74. log.Fatal(err)
  75. }
  76. for _, b := range bts {
  77. fmt.Println(b)
  78. }
  79. }
  80. func iterateBucketCommandFunc(cmd *cobra.Command, args []string) {
  81. if len(args) < 1 {
  82. log.Fatalf("Must provide at least 1 argument (got %v)", args)
  83. }
  84. dp := args[0]
  85. if !strings.HasSuffix(dp, "db") {
  86. dp = filepath.Join(snapDir(dp), "db")
  87. }
  88. if !existFileOrDir(dp) {
  89. log.Fatalf("%q does not exist", dp)
  90. }
  91. if iterateBucketName == "" {
  92. log.Fatal("got empty bucket name")
  93. }
  94. err := iterateBucket(dp, iterateBucketName, iterateBucketLimit)
  95. if err != nil {
  96. log.Fatal(err)
  97. }
  98. }
  99. func getHashCommandFunc(cmd *cobra.Command, args []string) {
  100. if len(args) < 1 {
  101. log.Fatalf("Must provide at least 1 argument (got %v)", args)
  102. }
  103. dp := args[0]
  104. if !strings.HasSuffix(dp, "db") {
  105. dp = filepath.Join(snapDir(dp), "db")
  106. }
  107. if !existFileOrDir(dp) {
  108. log.Fatalf("%q does not exist", dp)
  109. }
  110. hash, err := getHash(dp)
  111. if err != nil {
  112. log.Fatal(err)
  113. }
  114. fmt.Printf("db path: %s\nHash: %d\n", dp, hash)
  115. }