Skip to content

Commit 09235d0

Browse files
committed
Added restore cmd arg, set dynamic cassandra data dir in path utils
1 parent 3bff150 commit 09235d0

File tree

3 files changed

+43
-41
lines changed

3 files changed

+43
-41
lines changed

cmd/cain.go

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ type backupCmd struct {
4242
namespace string
4343
selector string
4444
container string
45-
cassandraDataDir string
4645
keyspace string
4746
dst string
4847
parallel int
4948
bufferSize float64
49+
cassandraDataDir string
5050

5151
out io.Writer
5252
}
@@ -102,16 +102,17 @@ func NewBackupCmd(out io.Writer) *cobra.Command {
102102
}
103103

104104
type restoreCmd struct {
105-
src string
106-
keyspace string
107-
tag string
108-
schema string
109-
namespace string
110-
selector string
111-
container string
112-
parallel int
113-
bufferSize float64
114-
userGroup string
105+
src string
106+
keyspace string
107+
tag string
108+
schema string
109+
namespace string
110+
selector string
111+
container string
112+
parallel int
113+
bufferSize float64
114+
userGroup string
115+
cassandraDataDir string
115116

116117
out io.Writer
117118
}
@@ -141,16 +142,17 @@ func NewRestoreCmd(out io.Writer) *cobra.Command {
141142
},
142143
Run: func(cmd *cobra.Command, args []string) {
143144
options := cain.RestoreOptions{
144-
Src: r.src,
145-
Keyspace: r.keyspace,
146-
Tag: r.tag,
147-
Schema: r.schema,
148-
Namespace: r.namespace,
149-
Selector: r.selector,
150-
Container: r.container,
151-
Parallel: r.parallel,
152-
BufferSize: r.bufferSize,
153-
UserGroup: r.userGroup,
145+
Src: r.src,
146+
Keyspace: r.keyspace,
147+
Tag: r.tag,
148+
Schema: r.schema,
149+
Namespace: r.namespace,
150+
Selector: r.selector,
151+
Container: r.container,
152+
Parallel: r.parallel,
153+
BufferSize: r.bufferSize,
154+
UserGroup: r.userGroup,
155+
CassandraDataDir: r.cassandraDataDir,
154156
}
155157
if err := cain.Restore(options); err != nil {
156158
log.Fatal(err)
@@ -169,6 +171,7 @@ func NewRestoreCmd(out io.Writer) *cobra.Command {
169171
f.IntVarP(&r.parallel, "parallel", "p", 1, "number of files to copy in parallel. set this flag to 0 for full parallelism")
170172
f.Float64VarP(&r.bufferSize, "buffer-size", "b", 6.75, "in memory buffer size (MB) to use for files copy (buffer per file)")
171173
f.StringVar(&r.userGroup, "user-group", "cassandra:cassandra", "user and group who should own restored files")
174+
f.StringVarP(&r.cassandraDataDir, "cassandra-data-dir", "", "/var/lib/cassandra/data", "cassandra data directory")
172175

173176
return cmd
174177
}

pkg/cain/cain.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ func Backup(o BackupOptions) (string, error) {
5252
tag := TakeSnapshots(k8sClient, pods, o.Namespace, o.Container, o.Keyspace)
5353

5454
log.Println("Calculating paths. This may take a while...")
55-
fromToPathsAllPods, err := utils.GetFromAndToPathsFromK8s(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, tag, dstBasePath)
55+
fromToPathsAllPods, err := utils.GetFromAndToPathsFromK8s(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, tag, dstBasePath, o.CassandraDataDir)
5656
if err != nil {
5757
return "", err
5858
}
@@ -71,16 +71,17 @@ func Backup(o BackupOptions) (string, error) {
7171

7272
// RestoreOptions are the options to pass to Restore
7373
type RestoreOptions struct {
74-
Src string
75-
Keyspace string
76-
Tag string
77-
Schema string
78-
Namespace string
79-
Selector string
80-
Container string
81-
Parallel int
82-
BufferSize float64
83-
UserGroup string
74+
Src string
75+
Keyspace string
76+
Tag string
77+
Schema string
78+
Namespace string
79+
Selector string
80+
Container string
81+
Parallel int
82+
BufferSize float64
83+
UserGroup string
84+
CassandraDataDir string
8485
}
8586

8687
// Restore performs restore
@@ -122,7 +123,7 @@ func Restore(o RestoreOptions) error {
122123

123124
log.Println("Calculating paths. This may take a while...")
124125
srcPath := filepath.Join(srcBasePath, o.Keyspace, sum, o.Tag)
125-
fromToPaths, podsToBeRestored, tablesToRefresh, err := utils.GetFromAndToPathsSrcToK8s(srcClient, k8sClient, srcPrefix, srcPath, srcBasePath, o.Namespace, o.Container)
126+
fromToPaths, podsToBeRestored, tablesToRefresh, err := utils.GetFromAndToPathsSrcToK8s(srcClient, k8sClient, srcPrefix, srcPath, srcBasePath, o.Namespace, o.Container, o.CassandraDataDir)
126127
if err != nil {
127128
return err
128129
}
@@ -147,7 +148,7 @@ func Restore(o RestoreOptions) error {
147148
}
148149

149150
log.Println("Changing files ownership")
150-
if err := utils.ChangeFilesOwnership(k8sClient, existingPods, o.Namespace, o.Container, o.UserGroup); err != nil {
151+
if err := utils.ChangeFilesOwnership(k8sClient, existingPods, o.Namespace, o.Container, o.UserGroup, o.CassandraDataDir); err != nil {
151152
return err
152153
}
153154

pkg/utils/path.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,13 @@ import (
88
"github.com/nuvo/skbn/pkg/skbn"
99
)
1010

11-
const cassandraDataDir = "/var/lib/cassandra/data"
12-
1311
// GetFromAndToPathsFromK8s aggregates paths from all pods
14-
func GetFromAndToPathsFromK8s(iClient interface{}, pods []string, namespace, container, keyspace, tag, dstBasePath string) ([]skbn.FromToPair, error) {
12+
func GetFromAndToPathsFromK8s(iClient interface{}, pods []string, namespace, container, keyspace, tag, dstBasePath, cassandraDataDir string) ([]skbn.FromToPair, error) {
1513
k8sClient := iClient.(*skbn.K8sClient)
1614
var fromToPathsAllPods []skbn.FromToPair
1715
for _, pod := range pods {
1816

19-
fromToPaths, err := GetFromAndToPathsK8sToDst(k8sClient, namespace, pod, container, keyspace, tag, dstBasePath)
17+
fromToPaths, err := GetFromAndToPathsK8sToDst(k8sClient, namespace, pod, container, keyspace, tag, dstBasePath, cassandraDataDir)
2018
if err != nil {
2119
return nil, err
2220
}
@@ -27,7 +25,7 @@ func GetFromAndToPathsFromK8s(iClient interface{}, pods []string, namespace, con
2725
}
2826

2927
// GetFromAndToPathsSrcToK8s performs a path mapping between a source and Kubernetes
30-
func GetFromAndToPathsSrcToK8s(srcClient, k8sClient interface{}, srcPrefix, srcPath, srcBasePath, namespace, container string) ([]skbn.FromToPair, []string, []string, error) {
28+
func GetFromAndToPathsSrcToK8s(srcClient, k8sClient interface{}, srcPrefix, srcPath, srcBasePath, namespace, container, cassandraDataDir string) ([]skbn.FromToPair, []string, []string, error) {
3129
var fromToPaths []skbn.FromToPair
3230

3331
filesToCopyRelativePaths, err := skbn.GetListOfFiles(srcClient, srcPrefix, srcPath)
@@ -56,7 +54,7 @@ func GetFromAndToPathsSrcToK8s(srcClient, k8sClient interface{}, srcPrefix, srcP
5654
}
5755

5856
// GetFromAndToPathsK8sToDst performs a path mapping between Kubernetes and a destination
59-
func GetFromAndToPathsK8sToDst(k8sClient interface{}, namespace, pod, container, keyspace, tag, dstBasePath string) ([]skbn.FromToPair, error) {
57+
func GetFromAndToPathsK8sToDst(k8sClient interface{}, namespace, pod, container, keyspace, tag, dstBasePath, cassandraDataDir string) ([]skbn.FromToPair, error) {
6058
var fromToPaths []skbn.FromToPair
6159

6260
pathPrfx := filepath.Join(namespace, pod, container, cassandraDataDir)
@@ -146,7 +144,7 @@ func PathFromSrcToK8s(k8sClient interface{}, fromPath, cassandraDataDir, srcBase
146144
}
147145

148146
// ChangeFilesOwnership changes the ownership of files after restoring them
149-
func ChangeFilesOwnership(iK8sClient interface{}, pods []string, namespace, container, userGroup string) error {
147+
func ChangeFilesOwnership(iK8sClient interface{}, pods []string, namespace, container, userGroup, cassandraDataDir string) error {
150148
k8sClient := iK8sClient.(*skbn.K8sClient)
151149
command := []string{"chown", "-R", userGroup, cassandraDataDir}
152150
for _, pod := range pods {

0 commit comments

Comments
 (0)