-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsnapshot.go
82 lines (76 loc) · 2.15 KB
/
snapshot.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
package cqrs
import (
"errors"
"io"
"os"
)
//Other snapshot strategies to consider:
//JsonDumpBackup creates a JSON dump of the aggregate data and
// saves to the backup folder specified in the service settings.
//NoSnapshot utility for aggreagates that don't need snapshots.
func NoSnapshot(aggregateName string) (BackupStrategy, RestoreStrategy) {
backup := func(name string) (err error) {
return errors.New("no backup strategy")
}
restore := func(name string) (err error) {
return errors.New("no restore strategy")
}
return backup, restore
}
//FileCopyBackup snapshot strategy that users file copy to backup and restore the data.
func FileCopyBackup(dbBaseFolder, bkpBaseFolder string) SnapshotStrategy {
return func(aggregateName string) (BackupStrategy, RestoreStrategy) {
dbFolder := dbBaseFolder + "/" + aggregateName
backup := func(snapshotID string) (err error) {
bkpFolder := bkpBaseFolder + "/" + snapshotID + "/" + aggregateName
return copyFolder(dbFolder, bkpFolder)
}
restore := func(snapshotID string) (err error) {
bkpFolder := bkpBaseFolder + "/" + snapshotID + "/" + aggregateName
return copyFolder(bkpFolder, dbFolder)
}
return backup, restore
}
}
// copyFolder source to destination :)
func copyFolder(source string, dest string) (err error) {
sourceInfo, err := os.Stat(source)
if err != nil {
return err
}
err = os.MkdirAll(dest, sourceInfo.Mode())
if err != nil {
return err
}
directory, _ := os.Open(source)
objects, err := directory.Readdir(-1)
for _, obj := range objects {
sourcePointer := source + "/" + obj.Name()
destinationPointer := dest + "/" + obj.Name()
if obj.IsDir() {
err = copyFolder(sourcePointer, destinationPointer)
if err != nil {
return err
}
} else {
sourceFile, err := os.Open(sourcePointer)
if err != nil {
return err
}
defer sourceFile.Close()
destfile, err := os.Create(destinationPointer)
if err != nil {
return err
}
defer destfile.Close()
_, err = io.Copy(destfile, sourceFile)
if err == nil {
sourceInfo, err := os.Stat(source)
if err != nil {
err = os.Chmod(dest, sourceInfo.Mode())
}
}
}
}
return nil
}