-
Notifications
You must be signed in to change notification settings - Fork 29
/
backupstore.go
170 lines (143 loc) · 4.27 KB
/
backupstore.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package backupstore
import (
"fmt"
"net/url"
"sync"
"github.com/pkg/errors"
"github.com/longhorn/backupstore/util"
)
type Volume struct {
Name string
Size int64 `json:",string"`
Labels map[string]string
CreatedTime string
LastBackupName string
LastBackupAt string
BlockCount int64 `json:",string"`
BackingImageName string `json:",string"`
BackingImageChecksum string `json:",string"`
CompressionMethod string `json:",string"`
StorageClassName string `json:",string"`
DataEngine string `json:",string"`
}
type Snapshot struct {
Name string
CreatedTime string
}
type ProcessingBlocks struct {
sync.Mutex
blocks map[string][]*BlockMapping
}
type Backup struct {
sync.Mutex
Name string
VolumeName string
SnapshotName string
SnapshotCreatedAt string
CreatedTime string
Size int64 `json:",string"`
Labels map[string]string
Parameters map[string]string
IsIncremental bool
CompressionMethod string
NewlyUploadedDataSize int64 `json:",string"`
ReUploadedDataSize int64 `json:",string"`
ProcessingBlocks *ProcessingBlocks
Blocks []BlockMapping `json:",omitempty"`
SingleFile BackupFile `json:",omitempty"`
}
var (
backupstoreBase = "backupstore"
)
func SetBackupstoreBase(base string) {
backupstoreBase = base
}
func GetBackupstoreBase() string {
return backupstoreBase
}
func addVolume(driver BackupStoreDriver, volume *Volume) error {
if volumeExists(driver, volume.Name) {
return nil
}
if !util.ValidateName(volume.Name) {
return fmt.Errorf("invalid volume name %v", volume.Name)
}
if err := saveVolume(driver, volume); err != nil {
log.WithError(err).Errorf("Failed to add volume %v", volume.Name)
return err
}
log.Infof("Added backupstore volume %v", volume.Name)
return nil
}
func removeVolume(volumeName string, driver BackupStoreDriver) error {
if !util.ValidateName(volumeName) {
return fmt.Errorf("invalid volume name %v", volumeName)
}
volumeDir := getVolumePath(volumeName)
volumeBlocksDirectory := getBlockPath(volumeName)
volumeBackupsDirectory := getBackupPath(volumeName)
volumeLocksDirectory := getLockPath(volumeName)
if err := driver.Remove(volumeBackupsDirectory); err != nil {
return errors.Wrapf(err, "failed to remove all the backups for volume %v", volumeName)
}
if err := driver.Remove(volumeBlocksDirectory); err != nil {
return errors.Wrapf(err, "failed to remove all the blocks for volume %v", volumeName)
}
if err := driver.Remove(volumeLocksDirectory); err != nil {
return errors.Wrapf(err, "failed to remove all the locks for volume %v", volumeName)
}
if err := driver.Remove(volumeDir); err != nil {
return errors.Wrapf(err, "failed to remove backup volume %v directory in backupstore", volumeName)
}
log.Infof("Removed volume directory in backupstore %v", volumeDir)
log.Infof("Removed backupstore volume %v", volumeName)
return nil
}
func EncodeBackupURL(backupName, volumeName, destURL string) string {
u, err := url.Parse(destURL)
if err != nil {
return ""
}
v, err := url.ParseQuery(u.RawQuery)
if err != nil {
// Just start with empty values list then
v = url.Values{}
}
v.Add("volume", volumeName)
if backupName != "" {
v.Add("backup", backupName)
}
u.RawQuery = v.Encode()
return u.String()
}
func DecodeBackupURL(backupURL string) (string, string, string, error) {
u, err := url.Parse(backupURL)
if err != nil {
return "", "", "", err
}
v := u.Query()
volumeName := v.Get("volume")
backupName := v.Get("backup")
if !util.ValidateName(volumeName) {
return "", "", "", fmt.Errorf("invalid volume name parsed, got %v", volumeName)
}
if backupName != "" && !util.ValidateName(backupName) {
return "", "", "", fmt.Errorf("invalid backup name parsed, got %v", backupName)
}
v.Del("volume")
v.Del("backup")
u.RawQuery = v.Encode()
destURL := u.String()
return backupName, volumeName, destURL, nil
}
func LoadVolume(backupURL string) (*Volume, error) {
_, volumeName, _, err := DecodeBackupURL(backupURL)
if err != nil {
return nil, err
}
driver, err := GetBackupStoreDriver(backupURL)
if err != nil {
return nil, err
}
return loadVolume(driver, volumeName)
}