mysqldump

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2022 License: MIT Imports: 12 Imported by: 4

README

Go MySQL dump

License Godoc ReportCard

Create MySQL dumps without mysqldump dependency.

Fork from JamesStewy/go-mysqldump. Includes pull request JamesStewy/go-mysqldump#14.

Features
Example
package main

import (
	"database/sql"
	"fmt"

	"github.com/aliakseiz/go-mysqldump"
	_ "github.com/go-sql-driver/mysql"
)

func main() {
	// Open connection to database
	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", config.DBName) // accepts time layout string and add .sql at the end of file
	
	db, err := sql.Open("mysql", config.FormatDSN())
	if err != nil {
		fmt.Println("Error opening database: ", err)
		
		return
	}
	// Register database with mysqldump
	dumper, err := mysqldump.Register(db, dumpDir, dumpFilenameFormat, config.DBName)
	if err != nil {
		fmt.Println("Error registering database:", err)
		
		return
	}
	// Dump database to file
	if err = dumper.Dump(); err != nil {
		fmt.Println("Error dumping:", err)
		
		return
	}
	
	fmt.Printf("File is saved to %s", dumpFilenameFormat)
	// Close dumper, connected database and file stream.
	dumper.Close()
}

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

View Source
const (
	// Version of this plugin for easy reference.
	Version = "0.5.1"
)

Variables

This section is empty.

Functions

func Dump added in v1.0.0

func Dump(db *sql.DB, out io.Writer) error

Dump Creates a MYSQL dump from the connection to the stream.

func Sanitize added in v1.0.1

func Sanitize(input string) string

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

func Register(db *sql.DB, dir, format, database string) (*Data, error)

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

func (data *Data) Begin() (err error)

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

func (data *Data) Close() error

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

func (data *Data) CreateTable(name string) *Table

CreateTable initializes a Table struct.

func (*Data) Dump added in v1.0.0

func (data *Data) Dump() error

Dump data using struct.

func (*Data) GetTables added in v1.0.1

func (data *Data) GetTables() ([]string, error)

GetTables returns database tables.

func (*Data) GetTemplates added in v1.0.1

func (data *Data) GetTemplates() (err error)

GetTemplates initializes the templates on data from the constants in this file.

func (*Data) WriteTable added in v1.0.1

func (data *Data) WriteTable(table *Table) error

WriteTable fills table template with provided *table data.

type MetaData added in v1.0.1

type MetaData struct {
	DumpVersion   string
	ServerVersion string
	CompleteTime  string
}

MetaData struct contains variables for header and footer templates.

func (*MetaData) UpdateServerVersion added in v1.0.1

func (meta *MetaData) UpdateServerVersion(data *Data) (err error)

UpdateServerVersion fetches server version and stores it in Data.

type Table added in v1.0.1

type Table struct {
	Name string
	Err  error
	// contains filtered or unexported fields
}

Table struct contains variables for table template.

func (*Table) CreateSQL added in v1.0.1

func (table *Table) CreateSQL() (string, error)

func (*Table) Init added in v1.0.1

func (table *Table) Init() (err error)

func (*Table) NameEsc added in v1.0.1

func (table *Table) NameEsc() string

func (*Table) Next added in v1.0.1

func (table *Table) Next() bool

func (*Table) RowBuffer added in v1.0.1

func (table *Table) RowBuffer() *bytes.Buffer

func (*Table) RowValues added in v1.0.1

func (table *Table) RowValues() string

func (*Table) Stream added in v1.0.1

func (table *Table) Stream() <-chan string

Jump to

Keyboard shortcuts

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