add ledis-dump tool

This commit is contained in:
siddontang 2014-07-11 10:43:39 +08:00
parent b2fa4082ff
commit 55056660e3
4 changed files with 69 additions and 7 deletions

62
cmd/ledis-dump/main.go Normal file
View File

@ -0,0 +1,62 @@
package main
import (
"bufio"
"flag"
"fmt"
"github.com/siddontang/ledisdb/server"
"net"
"os"
)
var host = flag.String("host", "127.0.0.1", "ledis server host")
var port = flag.Int("port", 6380, "ledis server port")
var sock = flag.String("sock", "", "ledis unix socket domain")
var dumpFile = flag.String("o", "./ledis.dump", "dump file to save")
var fullSyncCmd = []byte("*1\r\n$8\r\nfullsync\r\n") //fullsync
func main() {
flag.Parse()
var c net.Conn
var err error
var f *os.File
if f, err = os.OpenFile(*dumpFile, os.O_CREATE|os.O_WRONLY, os.ModePerm); err != nil {
println(err.Error())
return
}
defer f.Close()
if len(*sock) != 0 {
c, err = net.Dial("unix", *sock)
} else {
addr := fmt.Sprintf("%s:%d", *host, *port)
c, err = net.Dial("tcp", addr)
}
if err != nil {
println(err.Error())
return
}
defer c.Close()
println("dump begin")
if _, err = c.Write(fullSyncCmd); err != nil {
println(err.Error())
return
}
rb := bufio.NewReaderSize(c, 16*1024)
if err = server.ReadBulkTo(rb, f); err != nil {
println(err.Error())
return
}
println("dump end")
}

View File

@ -81,7 +81,7 @@ func (c *client) run() {
}
func (c *client) readLine() ([]byte, error) {
return readLine(c.rb)
return ReadLine(c.rb)
}
//A client sends to the Redis server a RESP Array consisting of just Bulk Strings.

View File

@ -254,7 +254,7 @@ func (m *master) fullSync() error {
defer os.Remove(dumpPath)
err = readBulkTo(m.rb, f)
err = ReadBulkTo(m.rb, f)
f.Close()
if err != nil {
log.Error("read dump data error %s", err.Error())
@ -291,7 +291,7 @@ func (m *master) sync() error {
m.syncBuf.Reset()
err := readBulkTo(m.rb, &m.syncBuf)
err := ReadBulkTo(m.rb, &m.syncBuf)
if err != nil {
return err
}

View File

@ -14,7 +14,7 @@ var (
errLineFormat = errors.New("bad response line format")
)
func readLine(rb *bufio.Reader) ([]byte, error) {
func ReadLine(rb *bufio.Reader) ([]byte, error) {
p, err := rb.ReadSlice('\n')
if err != nil {
@ -27,8 +27,8 @@ func readLine(rb *bufio.Reader) ([]byte, error) {
return p[:i], nil
}
func readBulkTo(rb *bufio.Reader, w io.Writer) error {
l, err := readLine(rb)
func ReadBulkTo(rb *bufio.Reader, w io.Writer) error {
l, err := ReadLine(rb)
if len(l) == 0 {
return errBulkFormat
} else if l[0] == '$' {
@ -43,7 +43,7 @@ func readBulkTo(rb *bufio.Reader, w io.Writer) error {
return err
}
if l, err = readLine(rb); err != nil {
if l, err = ReadLine(rb); err != nil {
return err
} else if len(l) != 0 {
return errBulkFormat