-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
92 lines (80 loc) · 2.07 KB
/
config.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"encoding/json"
"io"
"io/ioutil"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
// Config for database backups.
type Config struct {
// Username for login.
Username string `json:"username"`
// Password of user, if any.
Password string `json:"password"`
// Host address to connect to.
Host string `json:"host"`
// Port to connect to.
Port string `json:"port"`
// Name of database to back up from.
Name string `json:"name"`
// Type of database is either "mysql" or "postgres".
Type string `json:"type"`
// Tables to back up.
Tables []string `json:"tables"`
// Region for S3 bucket.
Region string `json:"region"`
// Bucket to send dumps to.
Bucket string `json:"bucket"`
}
func loadConfig(fn string) (*Config, error) {
data, err := ioutil.ReadFile(fn)
if err != nil {
return nil, err
}
cfg := &Config{}
err = json.Unmarshal(data, cfg)
return cfg, err
}
// TbleDates holds the most recent update times for all tables.
type TableDates struct {
Dates map[string]string `json:"dates"`
}
const tabledates = "tables.json"
// LoadTableDates returns a map of table names with last modified dates.
func (b *Bucket) LoadTableDates() *TableDates {
td := &TableDates{Dates: make(map[string]string)}
f := NewMemFile(tabledates, false)
dl := s3manager.NewDownloader(b.sess)
_, _ = dl.Download(f,
&s3.GetObjectInput{
Bucket: aws.String(b.Name),
Key: aws.String(tabledates),
})
// Simply return an empty structure no matter what happened during download.
json.Unmarshal(f.buf, &td)
return td
}
func (b *Bucket) UpdateTableDates(td *TableDates) error {
data, err := json.MarshalIndent(td, "", "\t")
if err != nil {
return err
}
f := NewMemFile(tabledates, false)
f.WriteAt(data, 0)
f.Seek(0, io.SeekStart)
r, w := io.Pipe()
go func() {
io.Copy(w, f)
w.Close()
}()
uploader := s3manager.NewUploader(b.sess)
_, err = uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(b.Name),
Key: aws.String(tabledates),
Body: r,
})
r.Close()
return err
}