-
Notifications
You must be signed in to change notification settings - Fork 0
/
es.go
117 lines (97 loc) · 2.63 KB
/
es.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"strconv"
"time"
sh "github.com/codeskyblue/go-sh"
"github.com/mholt/archiver"
"github.com/pkg/errors"
)
type es struct {
name string
addr string
port int
backupLocalDir string
retention int
}
func NewEs(name, addr, backupLocalDir string, port, retention int) *es {
e := &es{
name: name,
addr: addr,
port: port,
backupLocalDir: backupLocalDir,
retention: retention,
}
return e
}
func (e es) check() (string, error) {
fullAddr := fmt.Sprintf("http://%v:%v", e.addr, strconv.Itoa(e.port))
resp, err := http.Get(fullAddr)
if err != nil {
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
return string(body), nil
}
func (e *es) dump() (string, string, error) {
t := time.Now()
date := fmt.Sprintf("%v-%v-%v", t.Year(), int(t.Month()), t.Day())
archive := fmt.Sprintf("%v/%v-%v.gz", e.backupLocalDir, e.name, date)
fullAddr := fmt.Sprintf("http://%v:%v", e.addr, strconv.Itoa(e.port))
// Register a snapshot repository with the name my_backup
data := `{"type":"fs","settings":{"location":"/snapshot/backups/my_backup","compress":true}}`
jsonStr := []byte(data)
err := request(fullAddr+"/_snapshot/my_backup", bytes.NewBuffer(jsonStr), "PUT")
if err != nil {
return "", "", err
}
// Create a snapshot with the name snapshot_1 in the repository my_backup
data = ""
jsonStr = []byte(data)
err = request(fullAddr+"/_snapshot/my_backup/snapshot_1?wait_for_completion=true", bytes.NewBuffer(jsonStr), "PUT")
if err != nil {
return "", "", err
}
// Compressed with gzip format from the es snapshot
err = archiver.TarGz.Make(archive, []string{"/snapshot/backups/my_backup"})
if err != nil {
return "", "", err
}
// Delete current snapshot
err = request(fullAddr+"/_snapshot/my_backup/snapshot_1", nil, "DELETE")
if err != nil {
return "", "", err
}
return archive, "", err
}
func (e es) cleanup() error {
gz := fmt.Sprintf("cd %v && rm -f $(ls -1t %v*.gz | tail -n +%v)", e.backupLocalDir, e.name, e.retention+1)
err := sh.Command("sh", "-c", gz).Run()
if err != nil {
return errors.Wrapf(err, "removing old gz files from %v failed", e.backupLocalDir)
}
return nil
}
func request(url string, data io.Reader, method string) error {
client := &http.Client{}
req, err := http.NewRequest(method, url, data)
if err != nil {
return err
}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
out, _ := ioutil.ReadAll(resp.Body)
Info.Println(string(out))
return nil
}