forked from yunionio/cloudmux
/
mongodb_backup.go
104 lines (97 loc) · 3.19 KB
/
mongodb_backup.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
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aliyun
import (
"fmt"
"strings"
"time"
"github.com/nyl1001/pkg/errors"
"github.com/nyl1001/cloudmux/pkg/cloudprovider"
)
type SMongoDBBackup struct {
BackupDBNames string
BackupDownloadURL string
BackupEndTime time.Time
BackupStartTime time.Time
BackupId string
BackupIntranetDownloadURL string
BackupMethod string
BackupMode string
BackupSize int
BackupStatus string
BackupType string
}
func (self *SRegion) GetMongoDBBackups(id string, start time.Time, end time.Time, pageSize, pageNum int) ([]SMongoDBBackup, int, error) {
if pageSize < 1 || pageSize > 100 {
pageSize = 100
}
if pageNum < 1 {
pageNum = 1
}
params := map[string]string{
"StartTime": start.Format("2006-01-02T15:04Z"),
"EndTime": end.Format("2006-01-02T15:04Z"),
"DBInstanceId": id,
"PageSize": fmt.Sprintf("%d", pageSize),
"PageNumber": fmt.Sprintf("%d", pageNum),
}
resp, err := self.mongodbRequest("DescribeBackups", params)
if err != nil {
return nil, 0, errors.Wrapf(err, "DescribeBackups")
}
ret := []SMongoDBBackup{}
err = resp.Unmarshal(&ret, "Backups", "Backup")
if err != nil {
return nil, 0, errors.Wrapf(err, "resp.Unmarshal")
}
totalCount, _ := resp.Int("TotalCount")
return ret, int(totalCount), nil
}
func (self *SMongoDB) GetIBackups() ([]cloudprovider.SMongoDBBackup, error) {
backups := []SMongoDBBackup{}
now := time.Now().Add(time.Minute * -1)
for {
part, total, err := self.region.GetMongoDBBackups(self.DBInstanceId, self.CreationTime, now, 100, len(backups)/100)
if err != nil {
return nil, errors.Wrapf(err, "GetMongoDBBackups")
}
backups = append(backups, part...)
if len(backups) >= total {
break
}
}
ret := []cloudprovider.SMongoDBBackup{}
for _, res := range backups {
backup := cloudprovider.SMongoDBBackup{}
backup.Name = res.BackupId
backup.StartTime = res.BackupStartTime
backup.EndTime = res.BackupEndTime
backup.BackupSizeKb = res.BackupSize / 1024
switch res.BackupStatus {
case "Success":
backup.Status = cloudprovider.MongoDBBackupStatusAvailable
case "Failed":
backup.Status = cloudprovider.MongoDBBackupStatusFailed
default:
backup.Status = cloudprovider.TMongoDBBackupStatus(strings.ToLower(res.BackupStatus))
}
backup.BackupMethod = cloudprovider.TMongoDBBackupMethod(strings.ToLower(res.BackupMethod))
backup.BackupType = cloudprovider.MongoDBBackupTypeAuto
if res.BackupMode == "Manual" {
backup.BackupType = cloudprovider.MongoDBBackupTypeManual
}
ret = append(ret, backup)
}
return ret, nil
}