Skip to content

Instantly share code, notes, and snippets.

@oremj
Last active August 29, 2015 14:00
Show Gist options
  • Save oremj/11161063 to your computer and use it in GitHub Desktop.
Save oremj/11161063 to your computer and use it in GitHub Desktop.
mysqldump filter
package main
import (
"bufio"
"fmt"
"io"
"log"
"os"
"strings"
)
func main() {
excludeTables := make(map[string]bool)
for _, arg := range os.Args {
excludeTables[arg] = true
}
tableName := ""
excluded := false
inData := false
reader := bufio.NewReader(os.Stdin)
for {
line, err := reader.ReadString('\n')
if err != nil {
if err == io.EOF {
break
}
log.Fatal(err)
}
if len(line) > 30 && line[:30] == "-- Table structure for table `" {
lastTick := strings.IndexRune(line[30:], '`') + 30
tableName = line[30:lastTick]
excluded = excludeTables[tableName]
inData = false
} else if len(line) > 27 && line[:27] == "-- Dumping data for table `" {
inData = true
} else if excluded && inData {
continue
}
fmt.Print(line)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment