Skip to content

Dev/0.0.1 #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
dist/
dist/
testing/*
4 changes: 0 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
module github.com/mdgspace/sysreplicate

go 1.24.3

require golang.org/x/term v0.32.0

require golang.org/x/sys v0.33.0 // indirect
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +0,0 @@
golang.org/x/sys v0.33.0 h1:q3i8TbbEz+JRD9ywIRlyRAQbM0qF7hu24q3teo2hbuw=
golang.org/x/sys v0.33.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
golang.org/x/term v0.32.0 h1:DR4lr0TjUs3epypdhTOkMmuF5CDFJ/8pOnbzMZPQ7bg=
golang.org/x/term v0.32.0/go.mod h1:uZG1FhGx848Sqfsq4/DlJr3xGGsYMu/L5GW4abiaEPQ=
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package main

import "github.com/mdgspace/sysreplicate/system"

import (
"github.com/mdgspace/sysreplicate/system"
)
// main is the entry point for the program.
func main() {
system.Run()
Expand Down
Binary file added sysreplicate
Binary file not shown.
88 changes: 88 additions & 0 deletions system/backup/dotfile_scanner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package backup

import (
"os"
"path/filepath"
"strings"
)

var DotfilePaths = []string{
"~/.bashrc",
"~/.zshrc",
"~/.vimrc",
"~/.config",
"~/.bash_history",
"~/.zsh_history",
"~/.gitconfig",
"~/.profile",
"~/.npmrc",
}

type Dotfile struct {
Path string
RealPath string
IsDir bool
IsBinary bool
Mode os.FileMode
Content string // ignore for the binary files
}

// expand ~ to home dir
func expandHome(path string) string {
if strings.HasPrefix(path, "~") {
home, err := os.UserHomeDir()
if err == nil {
return filepath.Join(home, path[2:])
}
}
return path
}

// check for binary files
func containsNullByte(data []byte) bool {
for _, b := range data {
if b == 0 {
return true
}
}
return false
}

// ScanDotfiles scans all dotfiles and returns their metadata + content
func ScanDotfiles() ([]Dotfile, error) {
var results []Dotfile
home, _ := os.UserHomeDir()

for _, raw := range DotfilePaths {
full := expandHome(raw)

info, err := os.Stat(full)
if err != nil {
continue
}

realPath, _ := filepath.Rel(home, full)
entry := Dotfile{
Path: full,
RealPath: realPath,
IsDir: info.IsDir(),
Mode: info.Mode(),
}

if !info.IsDir() {
data, err := os.ReadFile(full)
if err != nil {
continue
}
if containsNullByte(data) {
entry.IsBinary = true
} else {
entry.Content = string(data)
}
}

results = append(results, entry)
}

return results, nil
}
59 changes: 59 additions & 0 deletions system/backup/dotfiles_backup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package backup

import (
"fmt"
"os"
"time"
"github.com/mdgspace/sysreplicate/system/output"
)

type BackupMetadata struct {
Timestamp time.Time `json:"timestamp"`
Hostname string `json:"hostname"`
Files []Dotfile `json:"files"`
}

type DotfileBackupManager struct{}

func NewDotfileBackupManager() *DotfileBackupManager {
return &DotfileBackupManager{}
}

func (db *DotfileBackupManager) CreateDotfileBackup(outputTar string) error {
// Scan dotfiles
files, err := ScanDotfiles()
if err != nil {
return fmt.Errorf("error scanning dotfiles: %w", err)
}

hostname, _ := os.Hostname()

// Convert []Dotfile to []output.Dotfile
outputFiles := make([]output.Dotfile, len(files))
for i, file := range files {
outputFiles[i] = output.Dotfile{
Path: file.Path,
RealPath: file.RealPath,
IsDir: file.IsDir,
IsBinary: file.IsBinary,
Mode: file.Mode,
Content: file.Content,
}
}


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

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

fmt.Println("Backup complete:", outputTar)
return nil
}
14 changes: 7 additions & 7 deletions system/backup/encrypt.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package backup

import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"os"
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"fmt"
"io"
"os"
)

//simplified encryption config without password
Expand Down
Loading