mysqldump

package module
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2018 License: MIT Imports: 7 Imported by: 3

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

Overview

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

Example

This example uses the mymysql driver (example 7 https://github.com/ziutek/mymysql) to connect to a mysql instance.

package main

import (
	"database/sql"
	"fmt"
	"github.com/JamesStewy/go-mysqldump"
	"github.com/ziutek/mymysql/godrv"
	"time"
)

func main() {
    // Register the mymysql driver
    godrv.Register("SET NAMES utf8")

    // Open connection to database
    db, err := sql.Open("mymysql", "tcp:host:port*database/user/password")
	if err != nil {
        fmt.Println("Error opening databse:", err)
        return
    }

    // Register database with mysqldump
    dumper, err := mysqldump.Register(db, "dumps", time.ANSIC)
    if err != nil {
    	fmt.Println("Error registering databse:", err)
    	return
    }

    // Dump database to file
    err = dumper.Dump()
    if err != nil {
    	fmt.Println("Error dumping:", err)
    	return
    }

    // Close dumper and connected database
    dumper.Close()
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dumper

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

Dumper represents a database.

func Register

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

Creates 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'.

func (*Dumper) Close

func (d *Dumper) Close() error

Closes the dumper. Will also close the database the dumper is connected to.

Not required.

func (*Dumper) Dump

func (d *Dumper) Dump() (string, error)

Creates a MYSQL Dump based on the options supplied through the dumper.

Jump to

Keyboard shortcuts

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