Skip to content

Commit 1ac682e

Browse files
committed
refactor(output): relocate tarball creation logic to output
1 parent ea6121a commit 1ac682e

File tree

5 files changed

+87
-54
lines changed

5 files changed

+87
-54
lines changed

.gitattributes

Lines changed: 0 additions & 1 deletion
This file was deleted.

system/backup/dotfile_scanner.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ var DotfilePaths = []string{
2020

2121
type Dotfile struct {
2222
Path string
23-
RelPath string
23+
RealPath string
2424
IsDir bool
2525
IsBinary bool
2626
Mode os.FileMode
@@ -61,10 +61,10 @@ func ScanDotfiles() ([]Dotfile, error) {
6161
continue
6262
}
6363

64-
relPath, _ := filepath.Rel(home, full)
64+
realPath, _ := filepath.Rel(home, full)
6565
entry := Dotfile{
6666
Path: full,
67-
RelPath: relPath,
67+
RealPath: realPath,
6868
IsDir: info.IsDir(),
6969
Mode: info.Mode(),
7070
}

system/backup/dotfiles_backup.go

Lines changed: 7 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
package backup
22

33
import (
4-
"archive/tar"
5-
"compress/gzip"
6-
"encoding/json"
74
"fmt"
8-
"io"
95
"os"
106
"time"
7+
"github.com/mdgspace/sysreplicate/system/output"
118
)
129

1310
type BackupMetadata struct {
@@ -23,55 +20,24 @@ func NewDotfileBackupManager() *DotfileBackupManager {
2320
}
2421

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

3129
hostname, _ := os.Hostname()
3230

33-
meta := BackupMetadata{
31+
// Create backup metadata
32+
meta := &BackupMetadata{
3433
Timestamp: time.Now(),
3534
Hostname: hostname,
3635
Files: files,
3736
}
3837

39-
tarFile, err := os.Create(outputTar)
40-
if err != nil {
41-
return fmt.Errorf("failed to create tar file: %w", err)
42-
}
43-
defer tarFile.Close()
44-
45-
gzipWriter := gzip.NewWriter(tarFile)
46-
defer gzipWriter.Close()
47-
tarWriter := tar.NewWriter(gzipWriter)
48-
defer tarWriter.Close()
49-
50-
// Write metadata JSON
51-
metaBytes, _ := json.MarshalIndent(meta, "", " ")
52-
tarWriter.WriteHeader(&tar.Header{
53-
Name: "backup.json",
54-
Mode: 0644,
55-
Size: int64(len(metaBytes)),
56-
})
57-
tarWriter.Write(metaBytes)
58-
59-
// Add dotfiles
60-
for _, f := range files {
61-
if f.IsDir {
62-
continue
63-
}
64-
file, err := os.Open(f.Path)
65-
if err != nil {
66-
continue
67-
}
68-
defer file.Close()
69-
70-
info, _ := file.Stat()
71-
hdr, _ := tar.FileInfoHeader(info, "")
72-
hdr.Name = f.RelPath
73-
tarWriter.WriteHeader(hdr)
74-
io.Copy(tarWriter, file)
38+
// Tarball creation
39+
if err := CreateDotfilesBackupTarball(meta, outputTar); err != nil {
40+
return fmt.Errorf("failed to create backup tarball: %w", err)
7541
}
7642

7743
fmt.Println("Backup complete:", outputTar)

system/backup_integration.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package system
33
import (
44
"fmt"
55
"log"
6-
6+
"os"
77
"github.com/mdgspace/sysreplicate/system/backup"
88
)
99

@@ -33,9 +33,15 @@ func RunDotfileBackup() {
3333
// Create a backup manager
3434
manager := backup.NewDotfileBackupManager()
3535

36-
//Output to dist directory
36+
// Output path
3737
outputPath := "dist/dotfile-backup.tar.gz"
3838

39+
// Ensure "dist" directory exists
40+
if err := os.MkdirAll("dist", os.ModePerm); err != nil {
41+
fmt.Printf("Failed to create output directory: %v\n", err)
42+
return
43+
}
44+
3945
// Run the backup
4046
err := manager.CreateDotfileBackup(outputPath)
4147
if err != nil {

system/output/tarball.go

Lines changed: 69 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"archive/tar"
55
"compress/gzip"
66
"encoding/json"
7+
"fmt"
8+
"io"
79
"os"
810
"time"
911
)
1012

11-
//backupData structure for tarball creation
13+
// backupData structure for tarball creation
1214
type BackupData struct {
13-
Timestamp time.Time `json:"timestamp"`
15+
Timestamp time.Time `json:"timestamp"`
1416
SystemInfo SystemInfo `json:"system_info"`
1517
EncryptedKeys map[string]EncryptedKey `json:"encrypted_keys"`
1618
EncryptionKey []byte `json:"encryption_key"`
@@ -30,12 +32,20 @@ type EncryptedKey struct {
3032
}
3133

3234
type Dotfile struct {
33-
Path string `json:"path"`
34-
Content string `json:"content"`
35-
Mode uint32 `json:"mode"`
36-
IsBinary bool `json:"is_binary"`
35+
Path string
36+
RealPath string
37+
IsDir bool
38+
IsBinary bool
39+
Mode os.FileMode
40+
Content string // ignore for the binary files
3741
}
38-
//create a compressed tarball with the backup data
42+
type BackupMetadata struct {
43+
Timestamp time.Time `json:"timestamp"`
44+
Hostname string `json:"hostname"`
45+
Files []Dotfile `json:"files"`
46+
}
47+
48+
// create a compressed tarball with the backup data
3949
func CreateBackupTarball(backupData *BackupData, tarballPath string) error {
4050
//create tarball file
4151
file, err := os.Create(tarballPath)
@@ -75,3 +85,55 @@ func CreateBackupTarball(backupData *BackupData, tarballPath string) error {
7585

7686
return nil
7787
}
88+
89+
func CreateDotfilesBackupTarball(meta *BackupMetadata, tarballPath string) error {
90+
// Create the tarball file
91+
file, err := os.Create(tarballPath)
92+
if err != nil {
93+
return fmt.Errorf("failed to create tarball: %w", err)
94+
}
95+
defer file.Close()
96+
97+
gzipWriter := gzip.NewWriter(file)
98+
defer gzipWriter.Close()
99+
tarWriter := tar.NewWriter(gzipWriter)
100+
defer tarWriter.Close()
101+
102+
jsonData, err := json.MarshalIndent(meta, "", " ")
103+
if err != nil {
104+
return fmt.Errorf("failed to marshal metadata: %w", err)
105+
}
106+
107+
// Add metadata as backup.json
108+
header := &tar.Header{
109+
Name: "backup.json",
110+
Mode: 0644,
111+
Size: int64(len(jsonData)),
112+
}
113+
if err := tarWriter.WriteHeader(header); err != nil {
114+
return fmt.Errorf("failed to write header for metadata: %w", err)
115+
}
116+
if _, err := tarWriter.Write(jsonData); err != nil {
117+
return fmt.Errorf("failed to write metadata to tar: %w", err)
118+
}
119+
120+
// Add dotfiles
121+
for _, f := range meta.Files {
122+
if f.IsDir {
123+
continue
124+
}
125+
file, err := os.Open(f.Path)
126+
if err != nil {
127+
continue
128+
}
129+
defer file.Close()
130+
131+
info, _ := file.Stat()
132+
hdr, _ := tar.FileInfoHeader(info, "")
133+
hdr.Name = f.RealPath
134+
tarWriter.WriteHeader(hdr)
135+
io.Copy(tarWriter, file)
136+
}
137+
138+
return nil
139+
}

0 commit comments

Comments
 (0)