Get the App
SLTechnology News&Howtos  ›  Database  › 

How to use golang to realize mysql Database backup

Shulou Source: shulou.com Published: 2022-05-31 22:21:39 09月29日 Update

In this issue, the editor will bring you about how to use golang to achieve mysql database backup. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

Map & json, when dealing with primary key and foreign key information, need to use json data structure to store intermediate results, because to be flexible, you can only use mapping [string] interface {} in golang.

Interface {} is the equivalent of object in java. It can accept any data type, but it is convenient to use it, otherwise the program will crash if the data type does not match.

The placeholder in xfffd, utf8, and out-of-range utf8mb4 will be stored as xfffd after it is stored in the database. When the data is exported, it needs to be filtered out.

The concurrency support of goroutine and golang is unique. Our tool supports multiple libraries to be backed up at the same time, and it is easy to use goroutine to achieve parallelism.

Code parsing

Describe the core code by functional module

Main.go, concurrency, command line argument

Using command-line parameters, accept a parameter to specify the contents of the backup

Package common

Type OpFlag struct {Tables bool / / Table structure Datum bool / / Table structure and data Views bool / / View Funcs bool / / functions and stored procedures}

Main.go, program entry, processing command line arguments

If len (os.Args) > 1 {flag = common.OpFlag {Tables: false, Datum: false, Views: false, Funcs: false } switch os.Args [1] {/ / accept a parameter case "table": flag.Tables = true / / set the identification quantity case "data": flag.Tables = true flag.Datum = true case "views": flag.Views = true case "funcs": flag.Funcs = true default: / / the parameter is incorrect Exit log.Fatal ("You arg must be in: table, data, views or funcs.")} else {/ / No parameters. By default, export all flag = common.OpFlag {Tables: true, Datum: true, Views: true, Funcs: true,}} err: = backUp.Export (flag) back up the database according to the parameters

Export.go

Backup main process, generate goroutine according to configs.json to back up the database, and wait for completion.

Var configs interface {} fr, err: = os.Open (". / configs.json") if err! = nil {return err} decoder: = json.NewDecoder (fr) / / parse the configuration file err = decoder.Decode (& configs) confs: = configs. (mapping [string] interface {}) workDir: = confs ["workDir"]. (string) ch: = make (chan string) / / Channel variable for key Value: = range confs {if strings.HasPrefix (key, "db_") {dbConf: = value. (map [string] interface {}) dbConn: = common.DbConnFields {/ / Database corresponding configuration DbHost: dbConf ["db_host"]. (string), DbPort: int (dbConf ["db_port"]. (float64)), DbUser: dbConf ["db_user"]. (string) DbPass: dbConf ["db_pass"]. (string), DbName: dbConf ["db_name"]. (string), DbCharset: dbConf ["db_charset"]. (string),} if dbConf ["file_alias"]! = nil {/ / the named dbConn.FileAlias = dbConf ["file_alias"]. (string)} go ExportOne (dbConn, workDir, ch) Flag) / / create a protocol}} for key: = range confs {/ / blocking the main process Upon completion of all collaborative processes, if strings.HasPrefix (key, "db_") {fmt.Print (

Tags: Data backup parameters database file code algorithm structure processing storage content function command source code program view generation configuration information address Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno Microsoft Huawei vpn