-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackup_integration.go
More file actions
151 lines (122 loc) · 3.8 KB
/
backup_integration.go
File metadata and controls
151 lines (122 loc) · 3.8 KB
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
147
148
149
150
151
package ui
import (
"bufio"
"fmt"
"log"
"os"
"strings"
"github.com/mdgspace/sysreplicate/internal/domain"
"github.com/mdgspace/sysreplicate/internal/core/backup"
)
// handle backup integration
func RunUnifiedBackup() {
fmt.Println("=== Unified System Backup (Keys + Dotfiles + Packages) ===")
// unified backup manager
ubm := backup.NewUnifiedBackupManager()
// gert all the custom key paths from user
fmt.Println("\nOptional: Add custom key locations")
customPaths := backup.GetCustomPaths()
// Create unified backup
err := ubm.CreateUnifiedBackup(customPaths)
if err != nil {
log.Printf("Unified backup failed: %v", err)
return
}
fmt.Println("Complete system backup completed successfully!")
fmt.Println()
fmt.Println("Your backup includes:")
fmt.Println("- SSH/GPG keys (encrypted)")
fmt.Println("- Dotfiles (.bashrc, .vimrc, .gitconfig, etc.)")
fmt.Println("- Package lists for reinstallation")
fmt.Println("- System automation files (SystemD services, timers, cronjobs)")
}
// system restoration from backup
func RunRestore() {
fmt.Println("=== System Restore from Backup ===")
scanner := bufio.NewScanner(os.Stdin)
fmt.Print("Enter backup tarball path: ")
if !scanner.Scan() {
fmt.Println("Failed to read input")
return
}
tarballPath := strings.TrimSpace(scanner.Text())
if tarballPath == "" {
fmt.Println("No tarball path provided")
return
}
// Normalize path separators (handle both Windows and Unix paths)
normalizedPath := strings.ReplaceAll(tarballPath, "\\", "/")
// Check if file exists and is a file (not directory)
fileInfo, err := os.Stat(normalizedPath)
if os.IsNotExist(err) {
fmt.Printf("Backup file does not exist: %s\n", normalizedPath)
return
}
if err != nil {
fmt.Printf("Error checking backup file: %v\n", err)
return
}
if fileInfo.IsDir() {
fmt.Printf("Path is a directory, not a file: %s\n", normalizedPath)
return
}
// Use normalized path for restoration
tarballPath = normalizedPath
// Confirm restoration
fmt.Printf("This will restore your system from: %s\n", tarballPath)
fmt.Print("WARNING: This will overwrite existing files. Continue? (y/N): ")
if !scanner.Scan() {
return
}
confirm := strings.ToLower(strings.TrimSpace(scanner.Text()))
if confirm != "y" && confirm != "yes" {
fmt.Println("Restoration cancelled")
return
}
// creating previously defined restore manager and run restoration
rm := backup.NewRestoreManager()
err = rm.RestoreFromBackup(tarballPath)
if err != nil {
log.Printf("Restoration failed: %v", err)
return
}
fmt.Println("\nSystem restoration completed!")
fmt.Println()
fmt.Println("Next steps:")
fmt.Println("1. Run the generated package installation script")
fmt.Println("2. Restart your shell or run 'source ~/.bashrc' (or ~/.zshrc)")
fmt.Println("3. Check that your SSH keys work: 'ssh-add -l'")
fmt.Println("4. Verify automation files were restored correctly")
}
// rest of the options
func RunBackup() {
fmt.Println("=== Key Backup Process ===")
//create backup manager
backupManager := backup.NewBackupManager()
//get custom paths from user
customPaths := backup.GetCustomPaths()
//create backup
err := backupManager.CreateBackup(customPaths)
if err != nil {
log.Printf("Backup failed: %v", err)
return
}
fmt.Println("Key backup completed successfully!")
}
func RunDotfileBackup() {
fmt.Println("=== SysReplicate: Distro Dotfile Backup ===")
// Create a backup manager
manager := backup.NewDotfileBackupManager()
// Ensure "dist" directory exists
if err := os.MkdirAll(domain.OutputScriptsDirPath, os.ModePerm); err != nil {
fmt.Printf("Failed to create output directory: %v\n", err)
return
}
// Run the backup
err := manager.CreateDotfileBackup(domain.DotfileOutputPath)
if err != nil {
fmt.Printf("Backup failed: %v\n", err)
return
}
fmt.Println("Backup complete!")
}