Skip to content

Commit 122e831

Browse files
chore: resolve merge conflicts of jaadu/packages
2 parents b89dd70 + 6aa829b commit 122e831

File tree

7 files changed

+348
-271
lines changed

7 files changed

+348
-271
lines changed

sysreplicate

3.65 MB
Binary file not shown.

system/backup/key.go

Lines changed: 158 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -7,192 +7,192 @@ import (
77
"path/filepath"
88
"strings"
99
"time"
10+
1011
"github.com/mdgspace/sysreplicate/system/output"
1112
)
1213

13-
//backup and restore operations
14+
// backup and restore operations
1415
type BackupManager struct {
15-
config *EncryptionConfig
16+
config *EncryptionConfig
1617
}
1718

1819
func NewBackupManager() *BackupManager {
19-
return &BackupManager{}
20+
return &BackupManager{}
2021
}
2122

22-
//create a complete backup of keys (no password required)
23+
// create a complete backup of keys (no password required)
2324
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
8384
}
8485

85-
8686
// processLocation processes a single key location
8787
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
111111
}
112112

113113
// processCustomPaths converts custom paths to KeyLocation objects
114114
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+
124124
homeDir, _ := os.UserHomeDir()
125125
path = filepath.Join(homeDir, path[2:])
126126
}
127127

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
161161
}
162162

163163
// collect basic system information
164164
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+
}
175175
}
176176

177177
// custom key path prompt to the userss
178178
func GetCustomPaths() []string {
179-
var paths []string
180-
scanner := bufio.NewScanner(os.Stdin)
181-
fmt.Println("\nEnter 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("\nEnter 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
198198
}

system/backup_integration.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ package system
33
import (
44
"fmt"
55
"log"
6+
"bufio"
67
"os"
8+
"strings"
9+
710
"github.com/mdgspace/sysreplicate/system/backup"
811
)
912

@@ -51,3 +54,13 @@ func RunDotfileBackup() {
5154

5255
fmt.Println("Backup complete!")
5356
}
57+
func restoreBackup() {
58+
fmt.Println("Restoring Backup")
59+
fmt.Println("Enter backup tarball path")
60+
61+
reader := bufio.NewReader(os.Stdin)
62+
name, _ := reader.ReadString('\n') // reads until newline
63+
name = strings.TrimSpace(name)
64+
65+
66+
}

0 commit comments

Comments
 (0)