mysqldump

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2021 License: MIT Imports: 9 Imported by: 0

README

Go MYSQL Dump

Create MYSQL dumps in Go without the mysqldump CLI as a dependancy.

Simple Example
package main

import (
	"database/sql"
	"fmt"

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

func main() {
	// Open connection to database
  username := "your-user"
  password := "your-pw"
  hostname := "your-hostname"
  port := "your-port"
	dbname := "your-db"

  dumpDir := "dumps"  // you should create this directory
  dumpFilenameFormat := fmt.Sprintf("%s-20060102T150405", dbname)   // accepts time layout string and add .sql at the end of file

	db, err := sql.Open("mysql", fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", username, password, hostname, port, dbname))
	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 and connected database
	dumper.Close()
}

GoDoc Build Status

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToSQL added in v1.0.0

func ConvertToSQL(in io.Reader, w io.Writer, opts ...ConvertOptions) error

Types

type ConvertOptions added in v1.0.0

type ConvertOptions struct {
	// If nil, all tables will be converted. If a table is specified here but is not present on the dump, no error will be returned
	Tables []string
}

type Dumper

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

Dumper represents a database.

func NewDumper added in v1.0.0

func NewDumper(db *sql.DB, w io.Writer) *Dumper

NewDumper creates a new dumper instance.

func (*Dumper) Dump

func (d *Dumper) Dump(dbName string, tables ...string) error

Dump dumps one or more tables from a database into a writer. If dbName is not empty, a "USE xxx" command will be sent prior to commencing the dump.

func (*Dumper) DumpAllTables added in v1.0.0

func (d *Dumper) DumpAllTables(dbName string) error

DumpAllTables dumps all tables in a database into a writer If dbName is not empty, a "USE xxx" command will be sent prior to commencing the dump.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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