Documentation ¶
Overview ¶
Create MYSQL dumps in Go without the 'mysqldump' CLI as a dependency.
Example ¶
This example uses the mysql driver (https://github.com/go-sql-driver/mysql) to connect to a mysql instance.
package main import ( "database/sql" "fmt" "github.com/JamesStewy/go-mysqldump" "github.com/go-sql-driver/mysql" ) func main() { config := mysql.NewConfig() config.User = "your-user" config.Passwd = "your-pw" config.DBName = "your-db" config.Net = "tcp" config.Addr = "your-hostname:your-port" dumpDir := "dumps" // you should create this directory dumpFilenameFormat := fmt.Sprintf("%s-20060102T150405", dbname) db, err := sql.Open("mysql", config.FormatDNS()) if err != nil { fmt.Println("Error opening database: ", err) return } // Register database with mysqldump. dumper, err := mysqldump.Register(db, dumpDir, dumpFilenameFormat) if err != nil { fmt.Println("Error registering databse:", err) return } // Dump database to file. resultFilename, err := dumper.Dump() if err != nil { fmt.Println("Error dumping:", err) return } fmt.Printf("File is saved to %s", resultFilename) // Close dumper, connected database and file stream. dumper.Close() }
Package mysqldump provides interfaces to control database dumper.
Index ¶
Constants ¶
const (
// Version of this plugin for easy reference.
Version = "0.5.1"
)
Variables ¶
This section is empty.
Functions ¶
func Sanitize ¶ added in v1.0.1
Sanitize input value based on https://dev.mysql.com/doc/refman/8.0/en/string-literals.html table 9.1. Needs to be placed in either a single or a double quoted string.
Types ¶
type Data ¶ added in v1.0.0
type Data struct { Out io.Writer Connection *sql.DB IgnoreTables []string // TODO store in a map MaxAllowedPacket int LockTables bool DBName string // contains filtered or unexported fields }
Data struct to configure dump behavior. Out: Stream to write to Connection: Database connection to dump IgnoreTables: Mark sensitive tables to ignore MaxAllowedPacket: Sets the largest packet size to use in backups LockTables: Lock all tables for the duration of the dump.
func Register ¶
Register a new dumper. db: Database that will be dumped (https://golang.org/pkg/database/sql/#DB). dir: Path to the directory where the dumps will be stored. format: Format to be used to name each dump file.
Uses time.Time.Format (https://golang.org/pkg/time/#Time.Format). format appended with '.sql'.
database: Database name to drop and create within a dump file. Recreate section omitted, when empty.
func (*Data) Begin ¶ added in v1.0.1
Begin starts a read only transaction that will be whatever the database was when it was called.
func (*Data) Close ¶ added in v1.0.0
Close the dumper. Will also close the database the dumper is connected to as well as the out stream if it has a Close method.
func (*Data) CreateTable ¶ added in v1.0.1
CreateTable initializes a Table struct.
func (*Data) GetTemplates ¶ added in v1.0.1
GetTemplates initializes the templates on data from the constants in this file.
func (*Data) WriteTable ¶ added in v1.0.1
WriteTable fills table template with provided *table data.
type MetaData ¶ added in v1.0.1
MetaData struct contains variables for header and footer templates.
func (*MetaData) UpdateServerVersion ¶ added in v1.0.1
UpdateServerVersion fetches server version and stores it in Data.