sqldump

package module
v0.0.0-...-cc43a7e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 11, 2014 License: MIT Imports: 6 Imported by: 0

README

sqldump

A package for dumping database contents. For the momment only MySQL driver had been developed. Perhaps more in the future. I get the idea from https://github.com/hgfischer/mysqlsuperdump start hacking the code as learning it, and finally, I release as a package.

Feel free to contact me, or send pull requests if this package is util for someone. I use, sqldumper.go as a tool for backing up some databases...

Install

go get github.com/jordic/sqldump

Example

Simple example dumping one table from a MySQL database

    // dumping all tables to stdout
    dumper := sqldump.NewMySQLDump(db, os.Stdout)
    DumpAllTables(dumper)

    dumper.DumpInit()
    // dumping just table test structure
    dumper.DumpCreateTable("test")
    // dumping table test content
    dumper.DumpTableData("test")
    dumper.DumpEnd()

    // w.. is a io.Writer, and it can works with 


Examples in apps

sqldumper.go

Using a json config file, describing all your databases it can download all of them, or just one.

Params:

  • config="jsonfile" defaults to sqldumper.json
  • output="directory where to output in format /tmp/"
  • gzip default false, put the flag if want to dump to gzip file

JsonFile:

{
    "test1":"root:@tcp(127.0.0.1:3306)/test1?charset=utf8",
    "test2":"root:@tcp(127.0.0.1:3306)/test2?charset=utf8"
}

Where each key represents a database, and each value, a dsn string for connecting to it

Install:

go get github.com/jordic/sqldump
go run apps/sqldumper/sqldumper.go
or go build apps/sqldumper/sqldumper.go

Note: I'm actually using this tool to backup my mysql databases. Crosscompiled with gox, and tested on linux_amd_64 and osx64

webdumper.go

Demostrates the use of the dumper as a web dumper..

Install:

go get github.com/jordic/sqldump go run apps/webdumper.go

Fires an http server on port :3000, default, shows the form for input params... if param action="dowload" as a hidden or get, param... dumps the requested database

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpAllTables

func DumpAllTables(t SQLDumper) error

DumpAllTables will dump a database, content and structure to io.Writer

Types

type MySQLDump

type MySQLDump struct {
	// contains filtered or unexported fields
}

MySQLDump is actually, the only dumper released.. perhaps will build new ones on future. to init it, just, create a new type:

    dumper := sqldump.NewMySQLDump(db *db.DB, w io.Writer)
and use it.

on apps directory i will build some examples... a commandline tool for downloading databases from server... a web tool, for the same prupose..

func (MySQLDump) DumpCreateTable

func (t MySQLDump) DumpCreateTable(table string) error

DumpCreateTable dumps sql table structure to w struct

func (MySQLDump) DumpEnd

func (t MySQLDump) DumpEnd()

DumpInit adds variables at the end of a dump

func (MySQLDump) DumpInit

func (t MySQLDump) DumpInit()

DumpInit adds variables at begining of a dump

func (MySQLDump) DumpTableData

func (t MySQLDump) DumpTableData(table string) error

DumpTableData will dump data for a given table

func (MySQLDump) GetColumnsFromTable

func (t MySQLDump) GetColumnsFromTable(table string) ([]string, error)

GetColumnsFromTable returns the column names of a give table

func (MySQLDump) GetTables

func (t MySQLDump) GetTables() ([]string, error)

GetTables returns the list of tables on the database @todo: return views also

type SQLDumper

type SQLDumper interface {
	GetTables() ([]string, error)
	GetColumnsFromTable(table string) ([]string, error)
	DumpCreateTable(table string) error
	DumpTableData(table string) error
	DumpInit()
	DumpEnd()
}

SQLDumper is an interface that can be implemented by and used by apps, that will dump database contents to io.Writer

func NewMySQLDump

func NewMySQLDump(db *sql.DB, w io.Writer) SQLDumper

NewMySQLDump returns an instance of the Dumper to use,

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL