-
Notifications
You must be signed in to change notification settings - Fork 91
/
mysqldump.go
71 lines (62 loc) · 1.26 KB
/
mysqldump.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package mysqldump
import (
"database/sql"
"errors"
"os"
)
// Dumper represents a database.
type Dumper struct {
db *sql.DB
format string
dir string
}
/*
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 Register(db *sql.DB, dir, format string) (*Dumper, error) {
if !isDir(dir) {
return nil, errors.New("Invalid directory")
}
return &Dumper{
db: db,
format: format,
dir: dir,
}, nil
}
// Closes the dumper.
// Will also close the database the dumper is connected to.
//
// Not required.
func (d *Dumper) Close() error {
defer func() {
d.db = nil
}()
return d.db.Close()
}
func exists(p string) (bool, os.FileInfo) {
f, err := os.Open(p)
if err != nil {
return false, nil
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return false, nil
}
return true, fi
}
func isFile(p string) bool {
if e, fi := exists(p); e {
return fi.Mode().IsRegular()
}
return false
}
func isDir(p string) bool {
if e, fi := exists(p); e {
return fi.Mode().IsDir()
}
return false
}