diff --git a/cmd/redis.go b/cmd/redis.go index 19b1f1b..ae521a7 100644 --- a/cmd/redis.go +++ b/cmd/redis.go @@ -7,10 +7,10 @@ import ( ) func init() { - redisRootCmd.Flags().StringVarP(&RedisHostname, "host", "H", "localhost", "Redis hostname. Defaults to locahost") - redisRootCmd.Flags().IntVarP(&RedisPort, "port", "P", 6379, "Redis Port. Defaults to 6379") - redisRootCmd.Flags().StringVarP(&RedisPassword, "password", "p", "", "Redis password. Defaults to empty.") - redisRootCmd.Flags().IntVarP(&RedisDB, "database", "d", 0, "Redis database. Defaults to 0.") + redisRootCmd.PersistentFlags().StringVarP(&RedisHostname, "host", "H", "localhost", "Redis hostname. Defaults to locahost") + redisRootCmd.PersistentFlags().IntVarP(&RedisPort, "port", "P", 6379, "Redis Port. Defaults to 6379") + redisRootCmd.PersistentFlags().StringVarP(&RedisPassword, "password", "p", "", "Redis password. Defaults to empty.") + redisRootCmd.PersistentFlags().IntVarP(&RedisDB, "database", "d", 0, "Redis db. Defaults to 0.") rootCmd.AddCommand(redisRootCmd) } diff --git a/cmd/redis_dump.go b/cmd/redis_dump.go index 5557279..26971fc 100644 --- a/cmd/redis_dump.go +++ b/cmd/redis_dump.go @@ -1,8 +1,10 @@ package cmd import ( - "fmt" + b64 "encoding/base64" "encoding/json" + "fmt" + "github.com/go-redis/redis/v8" "github.com/spf13/cobra" ) @@ -10,20 +12,22 @@ import ( func init() { redisDumpCmd.Flags().IntVarP(&MaxDump, "count", "n", -1, "Max number to dump. Defaults to all. May dump a few more depending on how much comes from redis.") redisDumpCmd.Flags().BoolVarP(&DumpJson, "json", "j", false, "Dump as json") + redisDumpCmd.Flags().BoolVarP(&DumpValues, "values", "", false, "Dump values with keys") redisRootCmd.AddCommand(redisDumpCmd) } -var MaxDump int +var MaxDump int var DumpJson bool +var DumpValues bool var redisDumpCmd = &cobra.Command{ Use: "dump", Short: "Dump all keys and values in Redis.. Safely", - Long: `Dump all keys and values in Redis.. Safely. Uses SCAN.`, + Long: `Dump all keys and values in Redis.. Safely. Uses DUMP.`, RunE: func(cmd *cobra.Command, args []string) error { rdb := redis.NewClient(&redis.Options{ Addr: fmt.Sprintf("%s:%d", RedisHostname, RedisPort), Password: RedisPassword, // no password set - DB: 0, // use default DB + DB: RedisDB, // use default DB }) var cursor uint64 var n int @@ -46,33 +50,43 @@ var redisDumpCmd = &cobra.Command{ if VerboseOutput { fmt.Printf("found %d keys\n", n) } - idx := 1 - for key, _ := range allKeys { - val, err := rdb.Get(ctx, key).Result() - if err != nil { - fmt.Printf("Error encountered getting key: %s\n", key) - continue + if DumpValues { + for key, _ := range allKeys { + val, err := rdb.Dump(ctx, key).Result() + if err != nil { + fmt.Printf("Error encountered getting key: %s\n", key) + continue + } + sEnc := b64.StdEncoding.EncodeToString([]byte(val)) + allKeys[key] = sEnc + } + idx := 1 + if DumpJson { + jsonStr, _ := json.Marshal(allKeys) + fmt.Println(string(jsonStr)) + } else { + for k, v := range allKeys { + if VerboseOutput { + fmt.Printf("%d: %s => %s\n", idx, k, v) + } else { + fmt.Printf("%s => %s\n", k, v) + } + idx++ + } } - allKeys[key] = val - idx++ - } - idx = 1 - if DumpJson { - jsonStr, _ := json.Marshal(allKeys) - fmt.Println(string(jsonStr)) } else { - for k, v := range allKeys { + idx := 1 + for k, _ := range allKeys { if VerboseOutput { - fmt.Printf("%d: %s => %s\n", idx, k, v) + fmt.Printf("%d: %s\n", idx, k) } else { - fmt.Printf("%s => %s\n", k, v) + fmt.Printf("%s\n", k) } idx++ } } - + return nil }, } - diff --git a/cmd/redis_upload.go b/cmd/redis_upload.go index b4147be..c165a36 100644 --- a/cmd/redis_upload.go +++ b/cmd/redis_upload.go @@ -1,11 +1,12 @@ package cmd import ( - "fmt" b64 "encoding/base64" + "fmt" + "github.com/go-redis/redis/v8" - "github.com/spf13/cobra" "github.com/rreichel3/hunttools/cmd/utils" + "github.com/spf13/cobra" ) func init() { @@ -15,7 +16,7 @@ func init() { redisRootCmd.AddCommand(redisUploadCmd) } -var UploadJsonInfile string +var UploadJsonInfile string var redisUploadCmd = &cobra.Command{ Use: "restore", Short: "Upload the results of ht redis dump", @@ -24,9 +25,9 @@ var redisUploadCmd = &cobra.Command{ rdb := redis.NewClient(&redis.Options{ Addr: fmt.Sprintf("%s:%d", RedisHostname, RedisPort), Password: RedisPassword, // no password set - DB: RedisDB, // use default DB + DB: RedisDB, // use default DB }) - + // Load JSON Infile dataToUpload, err := utils.LoadJsonMap(UploadJsonInfile) if err != nil { @@ -40,9 +41,8 @@ var redisUploadCmd = &cobra.Command{ dumpValue, _ := b64.StdEncoding.DecodeString(value) rdb.Restore(ctx, key, 0, string(dumpValue)) } - + return nil }, } -