Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dump redis #4

Merged
merged 2 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions cmd/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
58 changes: 36 additions & 22 deletions cmd/redis_dump.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,33 @@
package cmd

import (
"fmt"
b64 "encoding/base64"
"encoding/json"
"fmt"

"github.com/go-redis/redis/v8"
"github.com/spf13/cobra"
)

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
Expand All @@ -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

},
}

14 changes: 7 additions & 7 deletions cmd/redis_upload.go
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -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",
Expand All @@ -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 {
Expand All @@ -40,9 +41,8 @@ var redisUploadCmd = &cobra.Command{
dumpValue, _ := b64.StdEncoding.DecodeString(value)
rdb.Restore(ctx, key, 0, string(dumpValue))
}

return nil

},
}