1
1
package backup
2
2
3
3
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"
13
11
)
14
12
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
50
14
type BackupManager struct {
51
15
config * EncryptionConfig
52
16
}
@@ -87,10 +51,10 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
87
51
}
88
52
89
53
//create backup data
90
- backupData := & BackupData {
54
+ backupData := & output. BackupData {
91
55
Timestamp : time .Now (),
92
56
SystemInfo : bm .getSystemInfo (),
93
- EncryptedKeys : make (map [string ]EncryptedKey ),
57
+ EncryptedKeys : make (map [string ]output. EncryptedKey ),
94
58
EncryptionKey : key , // Store the key in backup data
95
59
}
96
60
@@ -108,7 +72,7 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
108
72
fmt .Println ("Creating backup tarball..." )
109
73
tarballPath := fmt .Sprintf ("dist/key-backup-%s.tar.gz" ,
110
74
time .Now ().Format ("2006-01-02-15-04-05" ))
111
- err = bm . createTarball (backupData , tarballPath )
75
+ err = output . CreateBackupTarball (backupData , tarballPath )
112
76
if err != nil {
113
77
return fmt .Errorf ("failed to create tarball: %w" , err )
114
78
}
@@ -118,8 +82,9 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
118
82
return nil
119
83
}
120
84
85
+
121
86
// 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 {
123
88
for _ , filePath := range location .Files {
124
89
//get file info for permissions
125
90
fileInfo , err := os .Stat (filePath )
@@ -135,7 +100,7 @@ func (bm *BackupManager) processLocation(location KeyLocation, backupData *Backu
135
100
136
101
// store encrypted key
137
102
keyID := filepath .Base (filePath ) + "_" + strings .ReplaceAll (filePath , "/" , "_" )
138
- backupData .EncryptedKeys [keyID ] = EncryptedKey {
103
+ backupData .EncryptedKeys [keyID ] = output. EncryptedKey {
139
104
OriginalPath : filePath ,
140
105
KeyType : location .Type ,
141
106
EncryptedData : encryptedData ,
@@ -196,60 +161,19 @@ func (bm *BackupManager) processCustomPaths(customPaths []string) []KeyLocation
196
161
}
197
162
198
163
// collect basic system information
199
- func (bm * BackupManager ) getSystemInfo () SystemInfo {
164
+ func (bm * BackupManager ) getSystemInfo () output. SystemInfo {
200
165
hostname , _ := os .Hostname ()
201
166
username := os .Getenv ("USER" )
202
167
if username == "" {
203
168
username = os .Getenv ("USERNAME" )
204
169
}
205
- return SystemInfo {
170
+ return output. SystemInfo {
206
171
Hostname : hostname ,
207
172
Username : username ,
208
173
OS : "linux" ,
209
174
}
210
175
}
211
176
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
-
253
177
// custom key path prompt to the userss
254
178
func GetCustomPaths () []string {
255
179
var paths []string
0 commit comments