forked from longhorn/backupstore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
singlefile.go
137 lines (109 loc) · 3.15 KB
/
singlefile.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
package backupstore
import (
"fmt"
"path/filepath"
"github.com/longhorn/backupstore/util"
"github.com/sirupsen/logrus"
. "github.com/longhorn/backupstore/logging"
)
const (
BACKUP_FILES_DIRECTORY = "BackupFiles"
)
type BackupFile struct {
FilePath string
}
func getSingleFileBackupFilePath(sfBackup *Backup) string {
backupFileName := sfBackup.Name + ".bak"
return filepath.Join(getVolumePath(sfBackup.VolumeName), BACKUP_FILES_DIRECTORY, backupFileName)
}
func CreateSingleFileBackup(volume *Volume, snapshot *Snapshot, filePath, destURL string) (string, error) {
driver, err := GetBackupStoreDriver(destURL)
if err != nil {
return "", err
}
if err := addVolume(volume, driver); err != nil {
return "", err
}
volume, err = loadVolume(volume.Name, driver)
if err != nil {
return "", err
}
log.WithFields(logrus.Fields{
LogFieldReason: LogReasonStart,
LogFieldEvent: LogEventBackup,
LogFieldObject: LogObjectSnapshot,
LogFieldSnapshot: snapshot.Name,
LogFieldFilepath: filePath,
}).Debug("Creating backup")
backup := &Backup{
Name: util.GenerateName("backup"),
VolumeName: volume.Name,
SnapshotName: snapshot.Name,
SnapshotCreatedAt: snapshot.CreatedTime,
}
backup.SingleFile.FilePath = getSingleFileBackupFilePath(backup)
if err := driver.Upload(filePath, backup.SingleFile.FilePath); err != nil {
return "", err
}
backup.CreatedTime = util.Now()
if err := saveBackup(backup, driver); err != nil {
return "", err
}
log.WithFields(logrus.Fields{
LogFieldReason: LogReasonComplete,
LogFieldEvent: LogEventBackup,
LogFieldObject: LogObjectSnapshot,
LogFieldSnapshot: snapshot.Name,
}).Debug("Created backup")
return encodeBackupURL(backup.Name, volume.Name, destURL), nil
}
func RestoreSingleFileBackup(backupURL, path string) (string, error) {
driver, err := GetBackupStoreDriver(backupURL)
if err != nil {
return "", err
}
srcBackupName, srcVolumeName, err := decodeBackupURL(backupURL)
if err != nil {
return "", err
}
if _, err := loadVolume(srcVolumeName, driver); err != nil {
return "", generateError(logrus.Fields{
LogFieldVolume: srcVolumeName,
LogEventBackupURL: backupURL,
}, "Volume doesn't exist in backupstore: %v", err)
}
backup, err := loadBackup(srcBackupName, srcVolumeName, driver)
if err != nil {
return "", err
}
dstFile := filepath.Join(path, filepath.Base(backup.SingleFile.FilePath))
if err := driver.Download(backup.SingleFile.FilePath, dstFile); err != nil {
return "", err
}
return dstFile, nil
}
func DeleteSingleFileBackup(backupURL string) error {
driver, err := GetBackupStoreDriver(backupURL)
if err != nil {
return err
}
backupName, volumeName, err := decodeBackupURL(backupURL)
if err != nil {
return err
}
_, err = loadVolume(volumeName, driver)
if err != nil {
return fmt.Errorf("Cannot find volume %v in backupstore due to: %v", volumeName, err)
}
backup, err := loadBackup(backupName, volumeName, driver)
if err != nil {
return err
}
if err := driver.Remove(backup.SingleFile.FilePath); err != nil {
return err
}
if err := removeBackup(backup, driver); err != nil {
return err
}
return nil
}