Skip to content

Commit 0e658c0

Browse files
committed
add cassandra data dir existence tet before backup or restore, fixes #7
1 parent cfa713f commit 0e658c0

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

pkg/cain/cain.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ func Backup(o BackupOptions) (string, error) {
4242
return "", err
4343
}
4444

45+
log.Println("Testing existence of data dir")
46+
if err := utils.TestK8sDirectory(k8sClient, pods, o.Namespace, o.Container, o.CassandraDataDir); err != nil {
47+
return "", err
48+
}
49+
4550
log.Println("Backing up schema")
4651
dstBasePath, err := BackupKeyspaceSchema(k8sClient, dstClient, o.Namespace, pods[0], o.Container, o.Keyspace, dstPrefix, dstPath)
4752
if err != nil {
@@ -101,6 +106,11 @@ func Restore(o RestoreOptions) error {
101106
return err
102107
}
103108

109+
log.Println("Testing existence of data dir")
110+
if err := utils.TestK8sDirectory(k8sClient, existingPods, o.Namespace, o.Container, o.CassandraDataDir); err != nil {
111+
return err
112+
}
113+
104114
log.Println("Getting current schema")
105115
_, sum, err := DescribeKeyspaceSchema(k8sClient, o.Namespace, existingPods[0], o.Container, o.Keyspace)
106116
if err != nil {

pkg/utils/path.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,19 @@ func ChangeFilesOwnership(iK8sClient interface{}, pods []string, namespace, cont
158158
}
159159
return nil
160160
}
161+
162+
// TestK8sDirectory checks if a path exists
163+
func TestK8sDirectory(iK8sClient interface{}, pods []string, namespace, container, cassandraDataDir string) error {
164+
k8sClient := iK8sClient.(*skbn.K8sClient)
165+
command := []string{"ls", cassandraDataDir}
166+
for _, pod := range pods {
167+
stderr, err := skbn.Exec(*k8sClient, namespace, pod, container, command, nil, nil)
168+
if len(stderr) != 0 {
169+
return fmt.Errorf("STDERR: " + (string)(stderr))
170+
}
171+
if err != nil {
172+
return fmt.Errorf(cassandraDataDir + " does not exist. " + err.Error())
173+
}
174+
}
175+
return nil
176+
}

0 commit comments

Comments
 (0)