This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
borg_repo.go
74 lines (60 loc) · 1.59 KB
/
borg_repo.go
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
package main
import (
"bytes"
"encoding/json"
"fmt"
"os"
"os/exec"
)
type BorgRepo struct {
Archives []*BorgArchive `json:"archives"`
mountPoint string
}
func (br *BorgRepo) LoadBorgArchives() error {
// obtain a listing of the repo
cmd := exec.Command("borg", "list", "--json")
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
if err != nil {
return fmt.Errorf("unable to run borg list: %w", err)
}
err = json.Unmarshal(out.Bytes(), &br)
if err != nil {
return fmt.Errorf("unable to serialize borg list output: %w", err)
}
for _, borgArchive := range br.Archives {
err := borgArchive.ParseTimestamps()
if err != nil {
return fmt.Errorf("unable to parse timestamps for archive %v: %w", borgArchive.ID, err)
}
}
return nil
}
// Mount mounts an repo at the chosen destination path
// archiveName can be left to the empty string, in that case,
// a listing of all archives is provided at the root of the mount
func (br *BorgRepo) Mount(dest string) error {
if br.mountPoint != "" {
return fmt.Errorf("already mounted at %v", br.mountPoint)
}
args := []string{"mount", "-o", "ignore_permissions", "::", dest}
cmd := exec.Command("borg", args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
fmt.Printf("%+v", args)
br.mountPoint = dest
return cmd.Run()
}
// Unmount does unmount the repo.
func (br *BorgRepo) Unmount() error {
if br.mountPoint == "" {
return fmt.Errorf("nothing mounted")
}
cmd := exec.Command("fusermount", "-u", br.mountPoint)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
return cmd.Run()
}