-
Notifications
You must be signed in to change notification settings - Fork 0
/
restore.go
146 lines (117 loc) · 3.43 KB
/
restore.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
package main
import (
"archive/tar"
"compress/gzip"
"context"
"io"
"log"
"os"
"path/filepath"
"runtime"
"strconv"
"sync"
"time"
"filippo.io/age"
"github.com/minio/minio-go/v7"
)
func getBasenameWithoutExtension(p string) (string, error) {
filenameWithExt := filepath.Base(p)
extension := filepath.Ext(filenameWithExt)
if extension == "" {
// File has no extension
return filenameWithExt, nil
}
filenameWithoutExt := filenameWithExt[:len(filenameWithExt)-len(extension)]
return filenameWithoutExt, nil
}
const parallelDownload = 10000
func Restore(config *BackupConfig) {
now := time.Now()
downloadedChunk := make(chan IndexedBuffer)
var downloadWg sync.WaitGroup
downloadWg.Add(parallelDownload)
archives := getListOfArchives(config)
archiveName := archives[len(archives)-1]
objectChan := getObjectsFromArchives(config, archiveName)
log.Println("Restoring archive", archiveName)
pool := NewBufferPool()
identity := check(age.ParseX25519Identity(config.Config.Age.Private))
go func() {
for i := 0; i < config.Config.S3.Parallel; i++ {
go func() {
defer downloadWg.Done()
for object := range objectChan {
if object.Err != nil {
log.Println("Unable to list object, skipping", object.Err, object)
continue
}
for {
log.Println("Downloading archive", object.Key)
file, err := config.MinioClient.GetObject(context.Background(), config.Config.S3.Bucket, object.Key, minio.GetObjectOptions{})
if err == nil {
fileBuff := pool.Get()
check(io.Copy(fileBuff, file))
check0(file.Close())
index := check(strconv.Atoi(check(getBasenameWithoutExtension(object.Key))))
downloadedChunk <- IndexedBuffer{
buffer: fileBuff,
i: index,
}
break
} else {
time.Sleep(10 * time.Second)
}
}
}
}()
}
downloadWg.Wait()
close(downloadedChunk)
}()
decompressedBufferChan := chanWorker[IndexedBuffer, IndexedBuffer](downloadedChunk, runtime.NumCPU(), func(in IndexedBuffer) IndexedBuffer {
unencryptedMessage := check(age.Decrypt(in.buffer, identity))
gzipReader := check(gzip.NewReader(unencryptedMessage))
unencryptedBuffer := pool.Get()
check(io.Copy(unencryptedBuffer, gzipReader))
log.Println("Decompressed and decrypted buffer", in.i)
pool.Put(in.buffer)
return IndexedBuffer{
buffer: unencryptedBuffer,
i: in.i,
}
})
rawTarReader := syncronizeBuffers(decompressedBufferChan, &pool)
tarReader := tar.NewReader(rawTarReader)
for {
header, err := tarReader.Next()
if err == io.EOF {
break // End of archive
}
if err != nil {
log.Fatal("Error reading archive:", err)
}
// Construct the path to restore the file
targetPath := header.Name
// Ensure the directory structure exists for the target path
parentDir := filepath.Dir(targetPath)
if err := os.MkdirAll(parentDir, os.ModePerm); err != nil {
log.Fatal("Error creating parent directory:", err)
}
// Handle regular file
file, err := os.Create(targetPath)
if err != nil {
log.Fatal("Error creating file:", err)
}
defer file.Close()
// Copy the content from the archive to the file
_, err = io.Copy(file, tarReader)
if err != nil {
log.Fatal("Error copying file content:", err)
}
// Set permissions
if err := os.Chmod(targetPath, os.FileMode(header.Mode)); err != nil {
log.Fatal("Error setting file permissions:", err)
}
}
log.Println("DONE", time.Since(now))
}