Exemple #1
0
func main() {
	flag.Parse()

	d, err := dump.NewDumper(*execution, *addr, *user, *password)
	if err != nil {
		fmt.Printf("Create Dumper error %v\n", errors.ErrorStack(err))
		return
	}

	if len(*ignoreTables) == 0 {
		subs := strings.Split(*ignoreTables, ",")
		for _, sub := range subs {
			if seps := strings.Split(sub, "."); len(seps) == 2 {
				d.AddIgnoreTables(seps[0], seps[1])
			}
		}
	}

	if len(*tables) > 0 && len(*tableDB) > 0 {
		subs := strings.Split(*tables, ",")
		d.AddTables(*tableDB, subs...)
	} else if len(*dbs) > 0 {
		subs := strings.Split(*dbs, ",")
		d.AddDatabases(subs...)
	}

	var f = os.Stdout

	if len(*output) > 0 {
		f, err = os.OpenFile(*output, os.O_CREATE|os.O_WRONLY, 0644)
		if err != nil {
			fmt.Printf("Open file error %v\n", errors.ErrorStack(err))
			return
		}
	}

	defer f.Close()

	if err = d.Dump(f); err != nil {
		fmt.Printf("Dump MySQL error %v\n", errors.ErrorStack(err))
		return
	}
}
Exemple #2
0
func (c *Canal) prepareDumper() error {
	var err error
	if c.dumper, err = dump.NewDumper(c.cfg.Dump.ExecutionPath,
		c.cfg.Addr, c.cfg.User, c.cfg.Password); err != nil {
		if err != exec.ErrNotFound {
			return errors.Trace(err)
		}
		//no mysqldump, use binlog only
		c.dumper = nil
		return nil
	}

	dbs := c.cfg.Dump.Databases
	tables := c.cfg.Dump.Tables
	tableDB := c.cfg.Dump.TableDB

	if len(tables) == 0 {
		c.dumper.AddDatabases(dbs...)
	} else {
		c.dumper.AddTables(tableDB, tables...)
	}

	for _, ignoreTable := range c.cfg.Dump.IgnoreTables {
		if seps := strings.Split(ignoreTable, ","); len(seps) == 2 {
			c.dumper.AddIgnoreTables(seps[0], seps[1])
		}
	}

	if c.cfg.Dump.DiscardErr {
		c.dumper.SetErrOut(ioutil.Discard)
	} else {
		c.dumper.SetErrOut(os.Stderr)
	}

	return nil
}