Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,25 @@
module github.com/mdgspace/sysreplicate

go 1.24.3

require (
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/bubbletea v1.3.10 // indirect
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc // indirect
github.com/charmbracelet/lipgloss v1.1.0 // indirect
github.com/charmbracelet/x/ansi v0.10.1 // indirect
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd // indirect
github.com/charmbracelet/x/term v0.2.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.16 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.16.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e // indirect
golang.org/x/sys v0.36.0 // indirect
golang.org/x/text v0.3.8 // indirect
)
41 changes: 41 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
github.com/charmbracelet/bubbletea v1.3.10 h1:otUDHWMMzQSB0Pkc87rm691KZ3SWa4KUlvF9nRvCICw=
github.com/charmbracelet/bubbletea v1.3.10/go.mod h1:ORQfo0fk8U+po9VaNvnV95UPWA1BitP1E0N6xJPlHr4=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc h1:4pZI35227imm7yK2bGPcfpFEmuY1gc2YSTShr4iJBfs=
github.com/charmbracelet/colorprofile v0.2.3-0.20250311203215-f60798e515dc/go.mod h1:X4/0JoqgTIPSFcRA/P6INZzIuyqdFY5rm8tb41s9okk=
github.com/charmbracelet/lipgloss v1.1.0 h1:vYXsiLHVkK7fp74RkV7b2kq9+zDLoEU4MZoFqR/noCY=
github.com/charmbracelet/lipgloss v1.1.0/go.mod h1:/6Q8FR2o+kj8rz4Dq0zQc3vYf7X+B0binUUBwA0aL30=
github.com/charmbracelet/x/ansi v0.10.1 h1:rL3Koar5XvX0pHGfovN03f5cxLbCF2YvLeyz7D2jVDQ=
github.com/charmbracelet/x/ansi v0.10.1/go.mod h1:3RQDQ6lDnROptfpWuUVIUG64bD2g2BgntdxH0Ya5TeE=
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd h1:vy0GVL4jeHEwG5YOXDmi86oYw2yuYUGqz6a8sLwg0X8=
github.com/charmbracelet/x/cellbuf v0.0.13-0.20250311204145-2c3ea96c31dd/go.mod h1:xe0nKWGd3eJgtqZRaN9RjMtK7xUYchjzPr7q6kcvCCs=
github.com/charmbracelet/x/term v0.2.1 h1:AQeHeLZ1OqSXhrAWpYUtZyX1T3zVxfpZuEQMIQaGIAQ=
github.com/charmbracelet/x/term v0.2.1/go.mod h1:oQ4enTYFV7QN4m0i9mzHrViD7TQKvNEEkHUMCmsxdUg=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f h1:Y/CXytFA4m6baUTXGLOoWe4PQhGxaX0KpnayAqC48p4=
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97MGsxSvLiUE2X8qFplwetxpGLQrlU1Q9AUEIzCaM=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-localereader v0.0.1 h1:ygSAOl7ZXTx4RdPYinUpg6W99U8jWvWi9Ye2JC/oIi4=
github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+EiG4R1k4Cjx5p88=
github.com/mattn/go-runewidth v0.0.16 h1:E5ScNMtiwvlvB5paMFdw9p4kSQzbXFikJ5SQO6TULQc=
github.com/mattn/go-runewidth v0.0.16/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
golang.org/x/sys v0.0.0-20210809222454-d867a43fc93e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.36.0 h1:KVRy2GtZBrk1cBYA7MKu5bEZFxQk4NIDV6RLVcC8o0k=
golang.org/x/sys v0.36.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/text v0.3.8 h1:nAL+RVCQ9uMn3vJZbV+MRnydTJFPf8qqY42YiA6MrqY=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
54 changes: 53 additions & 1 deletion internal/core/automation/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"archive/tar"
"fmt"
"path/filepath"

"strings"
"github.com/mdgspace/sysreplicate/internal/domain"
)

Expand Down Expand Up @@ -52,7 +52,59 @@ func (am *AutomationManager) addFileToTarball(originalPath, content, tarballPref

return nil
}
///TODO(@jaadu): IMPROVE THE RESTORE LOGIC AND RESTORATION COMMAND
func (am *AutomationManager) GenerateRestorationCommands(data *domain.AutomationData) []string {
var commands []string
if len(data.SystemDServices) > 0 || len(data.SystemDTimers) > 0 {
commands = append(commands, "echo 'Restoring SystemD units...'")

for _, service := range data.SystemDServices {
commands = append(commands, fmt.Sprintf("sudo cp automation/systemd/%s %s",
filepath.Base(service.Path), service.Path))
}
for _, timer := range data.SystemDTimers {
commands = append(commands, fmt.Sprintf("sudo cp automation/systemd/%s %s",
filepath.Base(timer.Path), timer.Path))
}



// Reload SystemD daemon
commands = append(commands, "sudo systemctl daemon-reload")
// Enable and start services
for _, service := range data.SystemDServices {
if service.UnitType == "service" {
commands = append(commands, fmt.Sprintf("sudo systemctl enable --now %s || true",
strings.TrimSuffix(service.Name, ".service")))
}
}
// Enable and start timers
for _, timer := range data.SystemDTimers {
commands = append(commands, fmt.Sprintf("sudo systemctl enable --now %s || true",
strings.TrimSuffix(timer.Name, ".timer")))
}
}
if len(data.UserCronjobs) > 0 || len(data.SystemCronjobs) > 0 {
commands = append(commands, "echo 'Restoring cronjobs...'")
for _, cronjob := range data.UserCronjobs {
if cronjob.Type == "user" {
commands = append(commands, fmt.Sprintf("crontab automation/cron/%s || true",
filepath.Base(cronjob.Path)))
}
}
for _, cronjob := range data.SystemCronjobs {
if cronjob.Type == "system" {
commands = append(commands, fmt.Sprintf("sudo cp automation/cron/%s %s",
filepath.Base(cronjob.Path), cronjob.Path))
} else if cronjob.Type == "cron_d" {
commands = append(commands, fmt.Sprintf("sudo cp automation/cron/%s %s",
filepath.Base(cronjob.Path), cronjob.Path))
}
}
}

return commands
}
func (am *AutomationManager) ValidateAutomationData(data *domain.AutomationData) error {
unitNames := make(map[string]bool)

Expand Down
10 changes: 7 additions & 3 deletions internal/core/automation/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ import (
"github.com/mdgspace/sysreplicate/internal/domain"
)

func (am *AutomationManager) detectSystemDUnits() ([]domain.SystemDUnit, []domain.SystemDUnit, error) {
var services []domain.SystemDUnit
var timers []domain.SystemDUnit
type SystemDUnit = domain.SystemDUnit
type Cronjob = domain.Cronjob
type AutomationData = domain.AutomationData

func (am *AutomationManager) detectSystemDUnits() ([]SystemDUnit, []SystemDUnit, error) {
var services []SystemDUnit
var timers []SystemDUnit

if _, err := os.Stat(domain.SystemdDirPath); os.IsNotExist(err) {
fmt.Printf("SystemD directory %s does not exist, skipping SystemD detection\n", domain.SystemdDirPath)
Expand Down
14 changes: 8 additions & 6 deletions internal/core/backup/dotfiles_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"fmt"
"os"
"time"
"github.com/mdgspace/sysreplicate/system/output"

"github.com/mdgspace/sysreplicate/internal/core/generator"
"github.com/mdgspace/sysreplicate/internal/domain"
)

type BackupMetadata struct {
Expand All @@ -28,10 +30,10 @@ func (db *DotfileBackupManager) CreateDotfileBackup(outputTar string) error {

hostname, _ := os.Hostname()

// Convert []Dotfile to []output.Dotfile
outputFiles := make([]output.Dotfile, len(files))
// Convert []Dotfile to []domain.Dotfile
outputFiles := make([]domain.Dotfile, len(files))
for i, file := range files {
outputFiles[i] = output.Dotfile{
outputFiles[i] = domain.Dotfile{
Path: file.Path,
RealPath: file.RealPath,
IsDir: file.IsDir,
Expand All @@ -44,13 +46,13 @@ func (db *DotfileBackupManager) CreateDotfileBackup(outputTar string) error {

// Create backup metadata
// struct from output
meta := &output.BackupMetadata{
meta := &domain.BackupMetadata{
Timestamp: time.Now(),
Hostname: hostname,
Files: outputFiles,
}

if err := output.CreateDotfilesBackupTarball(meta, outputTar); err != nil {
if err := generator.CreateDotfilesBackupTarball(meta, outputTar); err != nil {
return fmt.Errorf("failed to create backup tarball: %w", err)
}

Expand Down
17 changes: 9 additions & 8 deletions internal/core/backup/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
"strings"
"time"

"github.com/mdgspace/sysreplicate/system/output"
"github.com/mdgspace/sysreplicate/internal/core/generator"
"github.com/mdgspace/sysreplicate/internal/domain"
)

// backup and restore operations
Expand Down Expand Up @@ -52,10 +53,10 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
}

//create backup data
backupData := &output.BackupData{
backupData := &domain.BackupData{
Timestamp: time.Now(),
SystemInfo: bm.getSystemInfo(),
EncryptedKeys: make(map[string]output.EncryptedKey),
EncryptedKeys: make(map[string]domain.EncryptedKey),
EncryptionKey: key, // Store the key in backup data
}

Expand All @@ -73,7 +74,7 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
fmt.Println("Creating backup tarball...")
tarballPath := fmt.Sprintf("dist/key-backup-%s.tar.gz",
time.Now().Format("2006-01-02-15-04-05"))
err = output.CreateBackupTarball(backupData, tarballPath)
err = generator.CreateBackupTarball(backupData, tarballPath)
if err != nil {
return fmt.Errorf("failed to create tarball: %w", err)
}
Expand All @@ -84,7 +85,7 @@ func (bm *BackupManager) CreateBackup(customPaths []string) error {
}

// processLocation processes a single key location
func (bm *BackupManager) processLocation(location KeyLocation, backupData *output.BackupData) error {
func (bm *BackupManager) processLocation(location KeyLocation, backupData *domain.BackupData) error {
for _, filePath := range location.Files {
//get file info for permissions
fileInfo, err := os.Stat(filePath)
Expand All @@ -100,7 +101,7 @@ func (bm *BackupManager) processLocation(location KeyLocation, backupData *outpu

// store encrypted key
keyID := filepath.Base(filePath) + "_" + strings.ReplaceAll(filePath, "/", "_")
backupData.EncryptedKeys[keyID] = output.EncryptedKey{
backupData.EncryptedKeys[keyID] = domain.EncryptedKey{
OriginalPath: filePath,
KeyType: location.Type,
EncryptedData: encryptedData,
Expand Down Expand Up @@ -161,13 +162,13 @@ func (bm *BackupManager) processCustomPaths(customPaths []string) []KeyLocation
}

// collect basic system information
func (bm *BackupManager) getSystemInfo() output.SystemInfo {
func (bm *BackupManager) getSystemInfo() domain.SystemInfo {
hostname, _ := os.Hostname()
username := os.Getenv("USER")
if username == "" {
username = os.Getenv("USERNAME")
}
return output.SystemInfo{
return domain.SystemInfo{
Hostname: hostname,
Username: username,
OS: "linux",
Expand Down
4 changes: 2 additions & 2 deletions internal/core/backup/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"path/filepath"
"strings"

"github.com/mdgspace/sysreplicate/internal/core/generator"
"github.com/mdgspace/sysreplicate/internal/domain"
"github.com/mdgspace/sysreplicate/system/output"
)

type RestoreManager struct {
Expand Down Expand Up @@ -261,7 +261,7 @@ func (rm *RestoreManager) generateInstallScript() error {
return fmt.Errorf("failed to create directory: %w", err)
}

return output.GenerateInstallScript(rm.backupData.BaseDistro, rm.backupData.Packages, rm.backupData.Automation, scriptPath)
return generator.GenerateInstallScript(rm.backupData.BaseDistro, rm.backupData.Packages, rm.backupData.Automation, scriptPath)
}

// decryptData decrypts base64 encoded data using AES-GCM
Expand Down
17 changes: 8 additions & 9 deletions internal/core/backup/unified_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,15 @@ import (

"github.com/mdgspace/sysreplicate/internal/domain"
"github.com/mdgspace/sysreplicate/internal/core/automation"
"github.com/mdgspace/sysreplicate/system/output"
"github.com/mdgspace/sysreplicate/internal/platform"
)

// all backup information in one structure
type UnifiedBackupData struct {
Timestamp time.Time `json:"timestamp"`
SystemInfo output.SystemInfo `json:"system_info"`
EncryptedKeys map[string]output.EncryptedKey `json:"encrypted_keys"`
Dotfiles []output.Dotfile `json:"dotfiles"`
SystemInfo domain.SystemInfo `json:"system_info"`
EncryptedKeys map[string]domain.EncryptedKey `json:"encrypted_keys"`
Dotfiles []domain.Dotfile `json:"dotfiles"`
Packages map[string][]string `json:"packages"`
Automation *domain.AutomationData `json:"automation"`
EncryptionKey []byte `json:"encryption_key"`
Expand Down Expand Up @@ -78,12 +77,12 @@ func (ubm *UnifiedBackupManager) CreateUnifiedBackup(customPaths []string) error
// Create unified backup data
backupData := &UnifiedBackupData{
Timestamp: time.Now(),
SystemInfo: output.SystemInfo{
SystemInfo: domain.SystemInfo{
Hostname: hostname,
Username: username,
OS: "linux",
},
EncryptedKeys: make(map[string]output.EncryptedKey),
EncryptedKeys: make(map[string]domain.EncryptedKey),
EncryptionKey: key,
Packages: packages,
Distro: distro,
Expand Down Expand Up @@ -177,7 +176,7 @@ func (ubm *UnifiedBackupManager) backupKeys(customPaths []string, backupData *Un

fmt.Printf(" - %s\n", filePath)
keyID := filepath.Base(filePath) + "_" + strings.ReplaceAll(filePath, "/", "_")
backupData.EncryptedKeys[keyID] = output.EncryptedKey{
backupData.EncryptedKeys[keyID] = domain.EncryptedKey{
OriginalPath: filePath,
KeyType: location.Type,
EncryptedData: encryptedData,
Expand Down Expand Up @@ -205,7 +204,7 @@ func (ubm *UnifiedBackupManager) backupDotfiles(backupData *UnifiedBackupData) e
}

// Convert to output format and show details
outputFiles := make([]output.Dotfile, len(files))
outputFiles := make([]domain.Dotfile, len(files))
dotfileCount := 0

for i, file := range files {
Expand All @@ -214,7 +213,7 @@ func (ubm *UnifiedBackupManager) backupDotfiles(backupData *UnifiedBackupData) e
dotfileCount++
}

outputFiles[i] = output.Dotfile{
outputFiles[i] = domain.Dotfile{
Path: file.Path,
RealPath: file.RealPath,
IsDir: file.IsDir,
Expand Down
Loading