This repository has been archived by the owner on Aug 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsnapshot.go
85 lines (69 loc) · 1.61 KB
/
snapshot.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
75
76
77
78
79
80
81
82
83
84
85
package main
import (
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/giantswarm/conair/btrfs"
)
var cmdSnapshot = &Command{
Name: "snapshot",
Description: "Manage snapshots",
Summary: "Manage snapshots",
Run: runSnapshot,
}
func runSnapshot(args []string) (exit int) {
if len(args) < 1 {
fmt.Fprintln(os.Stderr, "Snapshot command missing.")
return 1
}
switch args[0] {
case "create":
if len(args) < 2 {
fmt.Fprintln(os.Stderr, "Snapshot name missing.")
return 1
}
snapshot := args[1]
snapshotPath := fmt.Sprintf(".cnr-snapshot-%s", snapshot)
fs, _ := btrfs.Init(home)
if fs.Exists(snapshotPath) {
fmt.Fprintln(os.Stderr, "Snapshot already exists.")
return 1
}
if err := fs.Subvolume(snapshotPath); err != nil {
fmt.Fprintln(os.Stderr, "Couldn't create snapshot.", err)
return 1
}
case "rm":
if len(args) < 2 {
fmt.Fprintln(os.Stderr, "Snapshot name missing.")
return 1
}
snapshot := args[1]
snapshotPath := fmt.Sprintf(".cnr-snapshot-%s", snapshot)
fs, _ := btrfs.Init(home)
if !fs.Exists(snapshotPath) {
fmt.Fprintln(os.Stderr, "Snapshot doesn't exist.")
return 1
}
if err := fs.Remove(snapshotPath); err != nil {
fmt.Fprintln(os.Stderr, "Couldn't remove snapshot.", err)
return 1
}
case "ls":
snapshots, _ := ioutil.ReadDir(home)
if len(snapshots) < 1 {
fmt.Println("No snapshots found.")
return
}
for _, s := range snapshots {
if strings.HasPrefix(s.Name(), ".cnr-snapshot-") {
fmt.Println(s.Name())
}
}
default:
fmt.Fprintln(os.Stderr, "Snapshot command unknown.")
return 1
}
return 0
}