Skip to content

Vinayak/dotfiles #5

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 5 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
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
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
}
65 changes: 45 additions & 20 deletions system/backup_integration.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,53 @@
package system

import (
"fmt"
"log"
"github.com/mdgspace/sysreplicate/system/backup"
"fmt"
"log"
"os"
"github.com/mdgspace/sysreplicate/system/backup"
)

// handle backup integration
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!")
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()

// Output path
outputPath := "dist/dotfile-backup.tar.gz"

// Ensure "dist" directory exists
if err := os.MkdirAll("dist", os.ModePerm); err != nil {
fmt.Printf("Failed to create output directory: %v\n", err)
return
}

// Run the backup
err := manager.CreateDotfileBackup(outputPath)
if err != nil {
fmt.Printf("Backup failed: %v\n", err)
return
}

fmt.Println("Backup complete!")
}
2 changes: 1 addition & 1 deletion system/output/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ func BuildSystemJSON(osType, distro, baseDistro string, packages []string) ([]by
Packages: packages,
}
return json.MarshalIndent(info, "", " ")
}
}
74 changes: 71 additions & 3 deletions system/output/tarball.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"os"
"time"
)

//backupData structure for tarball creation
// backupData structure for tarball creation
type BackupData struct {
Timestamp time.Time `json:"timestamp"`
Timestamp time.Time `json:"timestamp"`
SystemInfo SystemInfo `json:"system_info"`
EncryptedKeys map[string]EncryptedKey `json:"encrypted_keys"`
EncryptionKey []byte `json:"encryption_key"`
Expand All @@ -29,7 +31,21 @@ type EncryptedKey struct {
Permissions uint32 `json:"permissions"`
}

//create a compressed tarball with the backup data
type Dotfile struct {
Path string
RealPath string
IsDir bool
IsBinary bool
Mode os.FileMode
Content string // ignore for the binary files
}
type BackupMetadata struct {
Timestamp time.Time `json:"timestamp"`
Hostname string `json:"hostname"`
Files []Dotfile `json:"files"`
}

// create a compressed tarball with the backup data
func CreateBackupTarball(backupData *BackupData, tarballPath string) error {
//create tarball file
file, err := os.Create(tarballPath)
Expand Down Expand Up @@ -69,3 +85,55 @@ func CreateBackupTarball(backupData *BackupData, tarballPath string) error {

return nil
}

func CreateDotfilesBackupTarball(meta *BackupMetadata, tarballPath string) error {
// Create the tarball file
file, err := os.Create(tarballPath)
if err != nil {
return fmt.Errorf("failed to create tarball: %w", err)
}
defer file.Close()

gzipWriter := gzip.NewWriter(file)
defer gzipWriter.Close()
tarWriter := tar.NewWriter(gzipWriter)
defer tarWriter.Close()

jsonData, err := json.MarshalIndent(meta, "", " ")
if err != nil {
return fmt.Errorf("failed to marshal metadata: %w", err)
}

// Add metadata as backup.json
header := &tar.Header{
Name: "backup.json",
Mode: 0644,
Size: int64(len(jsonData)),
}
if err := tarWriter.WriteHeader(header); err != nil {
return fmt.Errorf("failed to write header for metadata: %w", err)
}
if _, err := tarWriter.Write(jsonData); err != nil {
return fmt.Errorf("failed to write metadata to tar: %w", err)
}

// Add dotfiles
for _, f := range meta.Files {
if f.IsDir {
continue
}
file, err := os.Open(f.Path)
if err != nil {
continue
}
defer file.Close()

info, _ := file.Stat()
hdr, _ := tar.FileInfoHeader(info, "")
hdr.Name = f.RealPath
tarWriter.WriteHeader(hdr)
io.Copy(tarWriter, file)
}

return nil
}
Loading