Skip to content

Commit 7eb6592

Browse files
refactor: separate tarbell generation logic to output
1 parent d4f80c9 commit 7eb6592

File tree

2 files changed

+87
-92
lines changed

2 files changed

+87
-92
lines changed

system/backup/key.go

Lines changed: 16 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,16 @@
11
package backup
22

33
import (
4-
"archive/tar"
5-
"bufio"
6-
"compress/gzip"
7-
"encoding/json"
8-
"fmt"
9-
"os"
10-
"path/filepath"
11-
"strings"
12-
"time"
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"path/filepath"
8+
"strings"
9+
"time"
10+
"github.com/mdgspace/sysreplicate/system/output"
1311
)
1412

15-
// Structure of backed up keys (removed Salt field)
16-
type BackupData struct {
17-
18-
Timestamp time.Time `json:"timestamp"`
19-
20-
SystemInfo SystemInfo `json:"system_info"`
21-
22-
EncryptedKeys map[string]EncryptedKey `json:"encrypted_keys"`
23-
24-
EncryptionKey []byte `json:"encryption_key"` // Store the key directly
25-
}
26-
27-
// Basic system information
28-
type SystemInfo struct {
29-
30-
Hostname string `json:"hostname"`
31-
32-
Username string `json:"username"`
33-
34-
OS string `json:"os"`
35-
}
36-
37-
//encrypted key file
38-
type EncryptedKey struct {
39-
40-
OriginalPath string `json:"original_path"`
41-
42-
KeyType string `json:"key_type"`
43-
44-
EncryptedData string `json:"encrypted_data"`
45-
46-
Permissions uint32 `json:"permissions"`
47-
}
48-
49-
//handles the backup and restore operations
13+
//backup and restore operations
5014
type BackupManager struct {
5115
config *EncryptionConfig
5216
}
@@ -87,10 +51,10 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
8751
}
8852

8953
//create backup data
90-
backupData := &BackupData{
54+
backupData := &output.BackupData{
9155
Timestamp: time.Now(),
9256
SystemInfo: bm.getSystemInfo(),
93-
EncryptedKeys: make(map[string]EncryptedKey),
57+
EncryptedKeys: make(map[string]output.EncryptedKey),
9458
EncryptionKey: key, // Store the key in backup data
9559
}
9660

@@ -108,7 +72,7 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
10872
fmt.Println("Creating backup tarball...")
10973
tarballPath := fmt.Sprintf("dist/key-backup-%s.tar.gz",
11074
time.Now().Format("2006-01-02-15-04-05"))
111-
err = bm.createTarball(backupData, tarballPath)
75+
err = output.CreateBackupTarball(backupData, tarballPath)
11276
if err != nil {
11377
return fmt.Errorf("failed to create tarball: %w", err)
11478
}
@@ -118,8 +82,9 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
11882
return nil
11983
}
12084

85+
12186
// processLocation processes a single key location
122-
func (bm *BackupManager) processLocation(location KeyLocation, backupData *BackupData) error {
87+
func (bm *BackupManager) processLocation(location KeyLocation, backupData *output.BackupData) error {
12388
for _, filePath := range location.Files {
12489
//get file info for permissions
12590
fileInfo, err := os.Stat(filePath)
@@ -135,7 +100,7 @@ func (bm *BackupManager) processLocation(location KeyLocation, backupData *Backu
135100

136101
// store encrypted key
137102
keyID := filepath.Base(filePath) + "_" + strings.ReplaceAll(filePath, "/", "_")
138-
backupData.EncryptedKeys[keyID] = EncryptedKey{
103+
backupData.EncryptedKeys[keyID] = output.EncryptedKey{
139104
OriginalPath: filePath,
140105
KeyType: location.Type,
141106
EncryptedData: encryptedData,
@@ -196,60 +161,19 @@ func (bm *BackupManager) processCustomPaths(customPaths []string) []KeyLocation
196161
}
197162

198163
// collect basic system information
199-
func (bm *BackupManager) getSystemInfo() SystemInfo {
164+
func (bm *BackupManager) getSystemInfo() output.SystemInfo {
200165
hostname, _ := os.Hostname()
201166
username := os.Getenv("USER")
202167
if username == "" {
203168
username = os.Getenv("USERNAME")
204169
}
205-
return SystemInfo{
170+
return output.SystemInfo{
206171
Hostname: hostname,
207172
Username: username,
208173
OS: "linux",
209174
}
210175
}
211176

212-
//create compressed tarball with the backup data
213-
func (bm *BackupManager) createTarball(backupData *BackupData, tarballPath string) error {
214-
// Create tarball file
215-
file, err := os.Create(tarballPath)
216-
if err != nil {
217-
return err
218-
}
219-
defer file.Close()
220-
221-
//gzip writer
222-
gzipWriter := gzip.NewWriter(file)
223-
defer gzipWriter.Close()
224-
225-
//tar writer
226-
tarWriter := tar.NewWriter(gzipWriter)
227-
defer tarWriter.Close()
228-
229-
// Convert backup data to JSON
230-
jsonData, err := json.MarshalIndent(backupData, "", " ")
231-
if err != nil {
232-
return err
233-
}
234-
235-
//add JSON file to tarball
236-
header := &tar.Header{
237-
Name: "backup.json",
238-
Mode: 0644,
239-
Size: int64(len(jsonData)),
240-
}
241-
242-
if err := tarWriter.WriteHeader(header); err != nil {
243-
return err
244-
}
245-
246-
if _, err := tarWriter.Write(jsonData); err != nil {
247-
return err
248-
}
249-
250-
return nil
251-
}
252-
253177
// custom key path prompt to the userss
254178
func GetCustomPaths() []string {
255179
var paths []string

system/output/tarball.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package output
2+
3+
import (
4+
"archive/tar"
5+
"compress/gzip"
6+
"encoding/json"
7+
"os"
8+
"time"
9+
)
10+
11+
//backupData structure for tarball creation
12+
type BackupData struct {
13+
Timestamp time.Time `json:"timestamp"`
14+
SystemInfo SystemInfo `json:"system_info"`
15+
EncryptedKeys map[string]EncryptedKey `json:"encrypted_keys"`
16+
EncryptionKey []byte `json:"encryption_key"`
17+
}
18+
19+
type SystemInfo struct {
20+
Hostname string `json:"hostname"`
21+
Username string `json:"username"`
22+
OS string `json:"os"`
23+
}
24+
25+
type EncryptedKey struct {
26+
OriginalPath string `json:"original_path"`
27+
KeyType string `json:"key_type"`
28+
EncryptedData string `json:"encrypted_data"`
29+
Permissions uint32 `json:"permissions"`
30+
}
31+
32+
//create a compressed tarball with the backup data
33+
func CreateBackupTarball(backupData *BackupData, tarballPath string) error {
34+
//create tarball file
35+
file, err := os.Create(tarballPath)
36+
if err != nil {
37+
return err
38+
}
39+
defer file.Close()
40+
41+
//gzip writer
42+
gzipWriter := gzip.NewWriter(file)
43+
defer gzipWriter.Close()
44+
45+
//tar writer
46+
tarWriter := tar.NewWriter(gzipWriter)
47+
defer tarWriter.Close()
48+
49+
//convertto JSON
50+
jsonData, err := json.MarshalIndent(backupData, "", " ")
51+
if err != nil {
52+
return err
53+
}
54+
55+
//add JSON file to tarball
56+
header := &tar.Header{
57+
Name: "backup.json",
58+
Mode: 0644,
59+
Size: int64(len(jsonData)),
60+
}
61+
62+
if err := tarWriter.WriteHeader(header); err != nil {
63+
return err
64+
}
65+
66+
if _, err := tarWriter.Write(jsonData); err != nil {
67+
return err
68+
}
69+
70+
return nil
71+
}

0 commit comments

Comments
 (0)