Skip to content

Commit cfa713f

Browse files
authored
Merge pull request #9 from alexbarta/master
Dynamic Cassandra Data Dir
2 parents fcd8677 + 05cee6c commit cfa713f

4 files changed

Lines changed: 89 additions & 79 deletions

File tree

README.md

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ Usage:
5555
cain backup [flags]
5656
5757
Flags:
58-
-b, --buffer-size float in memory buffer size (MB) to use for files copy (buffer per file) (default 6.75)
59-
-c, --container string container name to act on (default "cassandra")
60-
--dst string destination to backup to. Example: s3://bucket/cassandra
61-
-k, --keyspace string keyspace to act on
62-
-n, --namespace string namespace to find cassandra cluster
63-
-p, --parallel int number of files to copy in parallel. set this flag to 0 for full parallelism (default 1)
64-
-l, --selector string selector to filter on
58+
-b, --buffer-size float in memory buffer size (MB) to use for files copy (buffer per file) (default 6.75)
59+
--cassandra-data-dir string cassandra data directory. Overrides $CAIN_CASSANDRA_DATA_DIR (default "/var/lib/cassandra/data")
60+
-c, --container string container name to act on (default "cassandra")
61+
--dst string destination to backup to. Example: s3://bucket/cassandra
62+
-h, --help help for backup
63+
-k, --keyspace string keyspace to act on
64+
-n, --namespace string namespace to find cassandra cluster
65+
-p, --parallel int number of files to copy in parallel. set this flag to 0 for full parallelism (default 1)
66+
-l, --selector string selector to filter on
6567
```
6668

6769
#### Examples
@@ -104,16 +106,18 @@ Usage:
104106
cain restore [flags]
105107
106108
Flags:
107-
-b, --buffer-size float in memory buffer size (MB) to use for files copy (buffer per file) (default 6.75)
108-
-c, --container string container name to act on (default "cassandra")
109-
-k, --keyspace string keyspace to act on
110-
-n, --namespace string namespace to find cassandra cluster
111-
-p, --parallel int number of files to copy in parallel. set this flag to 0 for full parallelism (default 1)
112-
-s, --schema string schema version to restore (optional)
113-
-l, --selector string selector to filter on
114-
--src string source to restore from. Example: s3://bucket/cassandra/namespace/cluster-name
115-
-t, --tag string tag to restore
116-
--user-group string user and group who should own restored files (default "cassandra:cassandra")
109+
-b, --buffer-size float in memory buffer size (MB) to use for files copy (buffer per file) (default 6.75)
110+
--cassandra-data-dir string cassandra data directory. Overrides $CAIN_CASSANDRA_DATA_DIR (default "/var/lib/cassandra/data")
111+
-c, --container string container name to act on (default "cassandra")
112+
-h, --help help for restore
113+
-k, --keyspace string keyspace to act on
114+
-n, --namespace string namespace to find cassandra cluster
115+
-p, --parallel int number of files to copy in parallel. set this flag to 0 for full parallelism (default 1)
116+
-s, --schema string schema version to restore (optional)
117+
-l, --selector string selector to filter on
118+
--src string source to restore from. Example: s3://bucket/cassandra/namespace/cluster-name
119+
-t, --tag string tag to restore
120+
--user-group string user and group who should own restored files (default "cassandra:cassandra")
117121
```
118122

119123
#### Examples

cmd/cain.go

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"strings"
1010

1111
"github.com/nuvo/cain/pkg/cain"
12-
12+
"github.com/nuvo/cain/pkg/utils"
1313
"github.com/spf13/cobra"
1414
)
1515

@@ -39,13 +39,14 @@ func NewRootCmd(args []string) *cobra.Command {
3939
}
4040

4141
type backupCmd struct {
42-
namespace string
43-
selector string
44-
container string
45-
keyspace string
46-
dst string
47-
parallel int
48-
bufferSize float64
42+
namespace string
43+
selector string
44+
container string
45+
keyspace string
46+
dst string
47+
parallel int
48+
bufferSize float64
49+
cassandraDataDir string
4950

5051
out io.Writer
5152
}
@@ -72,13 +73,14 @@ func NewBackupCmd(out io.Writer) *cobra.Command {
7273
},
7374
Run: func(cmd *cobra.Command, args []string) {
7475
options := cain.BackupOptions{
75-
Namespace: b.namespace,
76-
Selector: b.selector,
77-
Container: b.container,
78-
Keyspace: b.keyspace,
79-
Dst: b.dst,
80-
Parallel: b.parallel,
81-
BufferSize: b.bufferSize,
76+
Namespace: b.namespace,
77+
Selector: b.selector,
78+
Container: b.container,
79+
Keyspace: b.keyspace,
80+
Dst: b.dst,
81+
Parallel: b.parallel,
82+
BufferSize: b.bufferSize,
83+
CassandraDataDir: b.cassandraDataDir,
8284
}
8385
if _, err := cain.Backup(options); err != nil {
8486
log.Fatal(err)
@@ -94,21 +96,23 @@ func NewBackupCmd(out io.Writer) *cobra.Command {
9496
f.StringVar(&b.dst, "dst", "", "destination to backup to. Example: s3://bucket/cassandra")
9597
f.IntVarP(&b.parallel, "parallel", "p", 1, "number of files to copy in parallel. set this flag to 0 for full parallelism")
9698
f.Float64VarP(&b.bufferSize, "buffer-size", "b", 6.75, "in memory buffer size (MB) to use for files copy (buffer per file)")
99+
f.StringVarP(&b.cassandraDataDir, "cassandra-data-dir", "", utils.GetStringEnvVar("CAIN_CASSANDRA_DATA_DIR", "/var/lib/cassandra/data"), "cassandra data directory. Overrides $CAIN_CASSANDRA_DATA_DIR")
97100

98101
return cmd
99102
}
100103

101104
type restoreCmd struct {
102-
src string
103-
keyspace string
104-
tag string
105-
schema string
106-
namespace string
107-
selector string
108-
container string
109-
parallel int
110-
bufferSize float64
111-
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
112116

113117
out io.Writer
114118
}
@@ -138,16 +142,17 @@ func NewRestoreCmd(out io.Writer) *cobra.Command {
138142
},
139143
Run: func(cmd *cobra.Command, args []string) {
140144
options := cain.RestoreOptions{
141-
Src: r.src,
142-
Keyspace: r.keyspace,
143-
Tag: r.tag,
144-
Schema: r.schema,
145-
Namespace: r.namespace,
146-
Selector: r.selector,
147-
Container: r.container,
148-
Parallel: r.parallel,
149-
BufferSize: r.bufferSize,
150-
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,
151156
}
152157
if err := cain.Restore(options); err != nil {
153158
log.Fatal(err)
@@ -166,6 +171,7 @@ func NewRestoreCmd(out io.Writer) *cobra.Command {
166171
f.IntVarP(&r.parallel, "parallel", "p", 1, "number of files to copy in parallel. set this flag to 0 for full parallelism")
167172
f.Float64VarP(&r.bufferSize, "buffer-size", "b", 6.75, "in memory buffer size (MB) to use for files copy (buffer per file)")
168173
f.StringVar(&r.userGroup, "user-group", "cassandra:cassandra", "user and group who should own restored files")
174+
f.StringVarP(&r.cassandraDataDir, "cassandra-data-dir", "", utils.GetStringEnvVar("CAIN_CASSANDRA_DATA_DIR", "/var/lib/cassandra/data"), "cassandra data directory. Overrides $CAIN_CASSANDRA_DATA_DIR")
169175

170176
return cmd
171177
}

pkg/cain/cain.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ import (
1111

1212
// BackupOptions are the options to pass to Backup
1313
type BackupOptions struct {
14-
Namespace string
15-
Selector string
16-
Container string
17-
Keyspace string
18-
Dst string
19-
Parallel int
20-
BufferSize float64
14+
Namespace string
15+
Selector string
16+
Container string
17+
Keyspace string
18+
Dst string
19+
Parallel int
20+
BufferSize float64
21+
CassandraDataDir string
2122
}
2223

2324
// Backup performs backup
@@ -51,7 +52,7 @@ func Backup(o BackupOptions) (string, error) {
5152
tag := TakeSnapshots(k8sClient, pods, o.Namespace, o.Container, o.Keyspace)
5253

5354
log.Println("Calculating paths. This may take a while...")
54-
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)
5556
if err != nil {
5657
return "", err
5758
}
@@ -70,16 +71,17 @@ func Backup(o BackupOptions) (string, error) {
7071

7172
// RestoreOptions are the options to pass to Restore
7273
type RestoreOptions struct {
73-
Src string
74-
Keyspace string
75-
Tag string
76-
Schema string
77-
Namespace string
78-
Selector string
79-
Container string
80-
Parallel int
81-
BufferSize float64
82-
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
8385
}
8486

8587
// Restore performs restore
@@ -121,7 +123,7 @@ func Restore(o RestoreOptions) error {
121123

122124
log.Println("Calculating paths. This may take a while...")
123125
srcPath := filepath.Join(srcBasePath, o.Keyspace, sum, o.Tag)
124-
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)
125127
if err != nil {
126128
return err
127129
}
@@ -146,7 +148,7 @@ func Restore(o RestoreOptions) error {
146148
}
147149

148150
log.Println("Changing files ownership")
149-
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 {
150152
return err
151153
}
152154

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)