@@ -7,192 +7,192 @@ import (
7
7
"path/filepath"
8
8
"strings"
9
9
"time"
10
+
10
11
"github.com/mdgspace/sysreplicate/system/output"
11
12
)
12
13
13
- //backup and restore operations
14
+ // backup and restore operations
14
15
type BackupManager struct {
15
- config * EncryptionConfig
16
+ config * EncryptionConfig
16
17
}
17
18
18
19
func NewBackupManager () * BackupManager {
19
- return & BackupManager {}
20
+ return & BackupManager {}
20
21
}
21
22
22
- //create a complete backup of keys (no password required)
23
+ // create a complete backup of keys (no password required)
23
24
func (bm * BackupManager ) CreateBackup (customPaths []string ) error {
24
- fmt .Println ("Starting key backup process..." )
25
-
26
- //generate random encryption key (no password needed)
27
- key , err := GenerateKey ()
28
- if err != nil {
29
- return fmt .Errorf ("failed to generate encryption key: %w" , err )
30
- }
31
-
32
- bm .config = & EncryptionConfig {
33
- Key : key ,
34
- }
35
-
36
- // search standard locations
37
- fmt .Println ("searching standard key locations..." )
38
- standardLocations , err := searchStandardLocations ()
39
- if err != nil {
40
- return fmt .Errorf ("failed to search standard locations: %w" , err )
41
- }
42
-
43
- //add custom paths
44
- customLocations := bm .processCustomPaths (customPaths )
45
-
46
- //combine all locations
47
- allLocations := append (standardLocations , customLocations ... )
48
- if len (allLocations ) == 0 {
49
- fmt .Println ("No key locations found to backup." )
50
- return nil
51
- }
52
-
53
- //create backup data
54
- backupData := & output.BackupData {
55
- Timestamp : time .Now (),
56
- SystemInfo : bm .getSystemInfo (),
57
- EncryptedKeys : make (map [string ]output.EncryptedKey ),
58
- EncryptionKey : key , // Store the key in backup data
59
- }
60
-
61
- //encrypt and store keys
62
- fmt .Println ("Encrypting keys..." )
63
- for _ , location := range allLocations {
64
- err := bm .processLocation (location , backupData )
65
- if err != nil {
66
- fmt .Printf ("Warning: Failed to process location %s: %v\n " , location .Path , err )
67
- continue
68
- }
69
- }
70
-
71
- //creating tarball for the backup storing
72
- fmt .Println ("Creating backup tarball..." )
73
- tarballPath := fmt .Sprintf ("dist/key-backup-%s.tar.gz" ,
74
- time .Now ().Format ("2006-01-02-15-04-05" ))
75
- err = output .CreateBackupTarball (backupData , tarballPath )
76
- if err != nil {
77
- return fmt .Errorf ("failed to create tarball: %w" , err )
78
- }
79
-
80
- fmt .Printf ("Backup completed successfully: %s\n " , tarballPath )
81
- fmt .Printf ("Backed up %d key files\n " , len (backupData .EncryptedKeys ))
82
- return nil
25
+ fmt .Println ("Starting key backup process..." )
26
+
27
+ //generate random encryption key (no password needed)
28
+ key , err := GenerateKey ()
29
+ if err != nil {
30
+ return fmt .Errorf ("failed to generate encryption key: %w" , err )
31
+ }
32
+
33
+ bm .config = & EncryptionConfig {
34
+ Key : key ,
35
+ }
36
+
37
+ // search standard locations
38
+ fmt .Println ("searching standard key locations..." )
39
+ standardLocations , err := searchStandardLocations ()
40
+ if err != nil {
41
+ return fmt .Errorf ("failed to search standard locations: %w" , err )
42
+ }
43
+
44
+ //add custom paths
45
+ customLocations := bm .processCustomPaths (customPaths )
46
+
47
+ //combine all locations
48
+ allLocations := append (standardLocations , customLocations ... )
49
+ if len (allLocations ) == 0 {
50
+ fmt .Println ("No key locations found to backup." )
51
+ return nil
52
+ }
53
+
54
+ //create backup data
55
+ backupData := & output.BackupData {
56
+ Timestamp : time .Now (),
57
+ SystemInfo : bm .getSystemInfo (),
58
+ EncryptedKeys : make (map [string ]output.EncryptedKey ),
59
+ EncryptionKey : key , // Store the key in backup data
60
+ }
61
+
62
+ //encrypt and store keys
63
+ fmt .Println ("Encrypting keys..." )
64
+ for _ , location := range allLocations {
65
+ err := bm .processLocation (location , backupData )
66
+ if err != nil {
67
+ fmt .Printf ("Warning: Failed to process location %s: %v\n " , location .Path , err )
68
+ continue
69
+ }
70
+ }
71
+
72
+ //creating tarball for the backup storing
73
+ fmt .Println ("Creating backup tarball..." )
74
+ tarballPath := fmt .Sprintf ("dist/key-backup-%s.tar.gz" ,
75
+ time .Now ().Format ("2006-01-02-15-04-05" ))
76
+ err = output .CreateBackupTarball (backupData , tarballPath )
77
+ if err != nil {
78
+ return fmt .Errorf ("failed to create tarball: %w" , err )
79
+ }
80
+
81
+ fmt .Printf ("Backup completed successfully: %s\n " , tarballPath )
82
+ fmt .Printf ("Backed up %d key files\n " , len (backupData .EncryptedKeys ))
83
+ return nil
83
84
}
84
85
85
-
86
86
// processLocation processes a single key location
87
87
func (bm * BackupManager ) processLocation (location KeyLocation , backupData * output.BackupData ) error {
88
- for _ , filePath := range location .Files {
89
- //get file info for permissions
90
- fileInfo , err := os .Stat (filePath )
91
- if err != nil {
92
- continue
93
- }
94
-
95
- // call encryption of the file
96
- encryptedData , err := EncryptFile (filePath , bm .config )
97
- if err != nil {
98
- return fmt .Errorf ("failed to encrypt %s: %w" , filePath , err )
99
- }
100
-
101
- // store encrypted key
102
- keyID := filepath .Base (filePath ) + "_" + strings .ReplaceAll (filePath , "/" , "_" )
103
- backupData .EncryptedKeys [keyID ] = output.EncryptedKey {
104
- OriginalPath : filePath ,
105
- KeyType : location .Type ,
106
- EncryptedData : encryptedData ,
107
- Permissions : uint32 (fileInfo .Mode ()),
108
- }
109
- }
110
- return nil
88
+ for _ , filePath := range location .Files {
89
+ //get file info for permissions
90
+ fileInfo , err := os .Stat (filePath )
91
+ if err != nil {
92
+ continue
93
+ }
94
+
95
+ // call encryption of the file
96
+ encryptedData , err := EncryptFile (filePath , bm .config )
97
+ if err != nil {
98
+ return fmt .Errorf ("failed to encrypt %s: %w" , filePath , err )
99
+ }
100
+
101
+ // store encrypted key
102
+ keyID := filepath .Base (filePath ) + "_" + strings .ReplaceAll (filePath , "/" , "_" )
103
+ backupData .EncryptedKeys [keyID ] = output.EncryptedKey {
104
+ OriginalPath : filePath ,
105
+ KeyType : location .Type ,
106
+ EncryptedData : encryptedData ,
107
+ Permissions : uint32 (fileInfo .Mode ()),
108
+ }
109
+ }
110
+ return nil
111
111
}
112
112
113
113
// processCustomPaths converts custom paths to KeyLocation objects
114
114
func (bm * BackupManager ) processCustomPaths (customPaths []string ) []KeyLocation {
115
- var locations []KeyLocation
116
- for _ , path := range customPaths {
117
- if path == "" {
118
- continue
119
- }
120
-
121
- // Expand home directory
122
- if strings .HasPrefix (path , "~/" ) {
123
-
115
+ var locations []KeyLocation
116
+ for _ , path := range customPaths {
117
+ if path == "" {
118
+ continue
119
+ }
120
+
121
+ // Expand home directory
122
+ if strings .HasPrefix (path , "~/" ) {
123
+
124
124
homeDir , _ := os .UserHomeDir ()
125
125
path = filepath .Join (homeDir , path [2 :])
126
126
}
127
127
128
- fileInfo , err := os .Stat (path )
129
- if err != nil {
130
- fmt .Printf ("Warning: Custom path %s does not exist\n " , path )
131
- continue
132
- }
133
-
134
- if fileInfo .IsDir () {
135
- // Either Process directory
136
- files , err := discoverKeyFiles (path )
137
- if err != nil {
138
- fmt .Printf ("Warning: Failed to scan directory %s: %v\n " , path , err )
139
- continue
140
- }
141
-
142
- if len (files ) > 0 {
143
- locations = append (locations , KeyLocation {
144
- Path : path ,
145
- Type : "custom" ,
146
- Files : files ,
147
- IsDirectory : true ,
148
- })
149
- }
150
- } else {
151
- // Or Process single file
152
- locations = append (locations , KeyLocation {
153
- Path : path ,
154
- Type : "custom" ,
155
- Files : []string {path },
156
- IsDirectory : false ,
157
- })
158
- }
159
- }
160
- return locations
128
+ fileInfo , err := os .Stat (path )
129
+ if err != nil {
130
+ fmt .Printf ("Warning: Custom path %s does not exist\n " , path )
131
+ continue
132
+ }
133
+
134
+ if fileInfo .IsDir () {
135
+ // Either Process directory
136
+ files , err := discoverKeyFiles (path )
137
+ if err != nil {
138
+ fmt .Printf ("Warning: Failed to scan directory %s: %v\n " , path , err )
139
+ continue
140
+ }
141
+
142
+ if len (files ) > 0 {
143
+ locations = append (locations , KeyLocation {
144
+ Path : path ,
145
+ Type : "custom" ,
146
+ Files : files ,
147
+ IsDirectory : true ,
148
+ })
149
+ }
150
+ } else {
151
+ // Or Process single file
152
+ locations = append (locations , KeyLocation {
153
+ Path : path ,
154
+ Type : "custom" ,
155
+ Files : []string {path },
156
+ IsDirectory : false ,
157
+ })
158
+ }
159
+ }
160
+ return locations
161
161
}
162
162
163
163
// collect basic system information
164
164
func (bm * BackupManager ) getSystemInfo () output.SystemInfo {
165
- hostname , _ := os .Hostname ()
166
- username := os .Getenv ("USER" )
167
- if username == "" {
168
- username = os .Getenv ("USERNAME" )
169
- }
170
- return output.SystemInfo {
171
- Hostname : hostname ,
172
- Username : username ,
173
- OS : "linux" ,
174
- }
165
+ hostname , _ := os .Hostname ()
166
+ username := os .Getenv ("USER" )
167
+ if username == "" {
168
+ username = os .Getenv ("USERNAME" )
169
+ }
170
+ return output.SystemInfo {
171
+ Hostname : hostname ,
172
+ Username : username ,
173
+ OS : "linux" ,
174
+ }
175
175
}
176
176
177
177
// custom key path prompt to the userss
178
178
func GetCustomPaths () []string {
179
- var paths []string
180
- scanner := bufio .NewScanner (os .Stdin )
181
- fmt .Println ("\n Enter additional key locations (one per line, empty line to finish):" )
182
- fmt .Println ("Examples: ~/mykeys/, /opt/certificates/, ~/.config/app/keys" )
183
- fmt .Println ("Note: .ssh and .gnupg are default scouting locations" )
184
-
185
- for {
186
- fmt .Print ("Path: " )
187
- if ! scanner .Scan () {
188
- break
189
- }
190
-
191
- path := strings .TrimSpace (scanner .Text ())
192
- if path == "" {
193
- break
194
- }
195
- paths = append (paths , path )
196
- }
197
- return paths
179
+ var paths []string
180
+ scanner := bufio .NewScanner (os .Stdin )
181
+ fmt .Println ("\n Enter additional key locations (one per line, empty line to finish):" )
182
+ fmt .Println ("Examples: ~/mykeys/, /opt/certificates/, ~/.config/app/keys" )
183
+ fmt .Println ("Note: .ssh and .gnupg are default scouting locations" )
184
+
185
+ for {
186
+ fmt .Print ("Path: " )
187
+ if ! scanner .Scan () {
188
+ break
189
+ }
190
+
191
+ path := strings .TrimSpace (scanner .Text ())
192
+ if path == "" {
193
+ break
194
+ }
195
+ paths = append (paths , path )
196
+ }
197
+ return paths
198
198
}
0 commit comments