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() {
username := "your-user"
password := "your-pw"
hostname := "your-hostname"
port := "your-port"
dbname := "your-db"
dumpDir := "dumps"
dumpFilenameFormat := fmt.Sprintf("%s-20060102T150405", dbname)
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
}
dumper, err := mysqldump.Register(db, dumpDir, dumpFilenameFormat)
if err != nil {
fmt.Println("Error registering databse:", err)
return
}
resultFilename, err := dumper.Dump()
if err != nil {
fmt.Println("Error dumping:", err)
return
}
fmt.Printf("File is saved to %s", resultFilename)
dumper.Close()
}