-
Notifications
You must be signed in to change notification settings - Fork 30
/
singlefile.go
134 lines (107 loc) · 3.16 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
package backupstore
import (
"path/filepath"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
. "github.com/longhorn/backupstore/logging"
"github.com/longhorn/backupstore/util"
)
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(driver, volume); err != nil {
return "", err
}
volume, err = loadVolume(driver, volume.Name)
if err != nil {
return "", err
}
log.WithFields(logrus.Fields{
LogFieldReason: LogReasonStart,
LogFieldEvent: LogEventBackup,
LogFieldObject: LogObjectSnapshot,
LogFieldSnapshot: snapshot.Name,
LogFieldFilepath: filePath,
}).Info("Creating backup")
backup := &Backup{
Name: util.GenerateName("backup"),
VolumeName: volume.Name,
SnapshotName: snapshot.Name,
SnapshotCreatedAt: snapshot.CreatedTime,
CompressionMethod: volume.CompressionMethod,
}
backup.SingleFile.FilePath = getSingleFileBackupFilePath(backup)
if err := driver.Upload(filePath, backup.SingleFile.FilePath); err != nil {
return "", err
}
backup.CreatedTime = util.Now()
if err := saveBackup(driver, backup); err != nil {
return "", err
}
log.WithFields(logrus.Fields{
LogFieldReason: LogReasonComplete,
LogFieldEvent: LogEventBackup,
LogFieldObject: LogObjectSnapshot,
LogFieldSnapshot: snapshot.Name,
}).Info("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(driver, srcVolumeName); err != nil {
return "", generateError(logrus.Fields{
LogFieldVolume: srcVolumeName,
LogEventBackupURL: backupURL,
}, "Volume doesn't exist in backupstore: %v", err)
}
backup, err := loadBackup(driver, srcBackupName, srcVolumeName)
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(driver, volumeName)
if err != nil {
return errors.Wrapf(err, "cannot find volume %v in backupstore", volumeName)
}
backup, err := loadBackup(driver, backupName, volumeName)
if err != nil {
return err
}
if err := driver.Remove(backup.SingleFile.FilePath); err != nil {
return err
}
return removeBackup(backup, driver)
}