Skip to content

Commit

Permalink
Add tag support to backup
Browse files Browse the repository at this point in the history
  • Loading branch information
nvtkaszpir committed Sep 28, 2021
1 parent 83b0e2a commit 1edd313
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
3 changes: 3 additions & 0 deletions cmd/cain.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type backupCmd struct {
cassandraDataDir string
username string
password string
tag string

out io.Writer
}
Expand Down Expand Up @@ -85,6 +86,7 @@ func NewBackupCmd(out io.Writer) *cobra.Command {
CassandraDataDir: b.cassandraDataDir,
Username: b.username,
Password: b.password,
Tag: b.tag,
}
if _, err := cain.Backup(options); err != nil {
log.Fatal(err)
Expand All @@ -103,6 +105,7 @@ func NewBackupCmd(out io.Writer) *cobra.Command {
f.StringVar(&b.cassandraDataDir, "cassandra-data-dir", utils.GetStringEnvVar("CAIN_CASSANDRA_DATA_DIR", "/var/lib/cassandra/data"), "cassandra data directory. Overrides $CAIN_CASSANDRA_DATA_DIR")
f.StringVarP(&b.username, "username", "u", utils.GetStringEnvVar("CAIN_USERNAME", "cassandra"), "username for the cassandra connection. Overrides $CAIN_USERNAME")
f.StringVarP(&b.password, "password", "w", utils.GetStringEnvVar("CAIN_PASSWORD", "cassandra"), "password for the cassandra connection. Overrides $CAIN_PASSWORD")
f.StringVarP(&b.tag, "tag", "t", utils.GetStringEnvVar("CAIN_TAG", ""), "tag to backup, if empty then will use current timestamp. Overrides $CAIN_TAG")

return cmd
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/cain/cain.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type BackupOptions struct {
CassandraDataDir string
Username string
Password string
Tag string
}

// Backup performs backup
Expand Down Expand Up @@ -56,7 +57,7 @@ func Backup(o BackupOptions) (string, error) {
}

log.Println("Taking snapshots")
tag := TakeSnapshots(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, o.Username, o.Password)
tag := TakeSnapshots(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, o.Username, o.Password, o.Tag)

log.Println("Calculating paths. This may take a while...")
fromToPathsAllPods, err := utils.GetFromAndToPathsFromK8s(k8sClient, pods, o.Namespace, o.Container, o.Keyspace, tag, dstBasePath, o.CassandraDataDir)
Expand Down
13 changes: 8 additions & 5 deletions pkg/cain/nodetool.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,27 @@ import (
)

// TakeSnapshots takes a snapshot using nodetool in all pods in parallel
func TakeSnapshots(iClient interface{}, pods []string, namespace, container, keyspace string, username string, password string) string {
func TakeSnapshots(iClient interface{}, pods []string, namespace, container, keyspace string, username string, password string, tag string) string {
k8sClient := iClient.(*skbn.K8sClient)
tag := utils.GetTimeStamp()
realtag := utils.GetTimeStamp()
if len(tag) == 0 {
realtag = tag
}
bwgSize := len(pods)
bwg := utils.NewBoundedWaitGroup(bwgSize)
for _, pod := range pods {
bwg.Add(1)

go func(k8sClient *skbn.K8sClient, namespace, pod, container, keyspace, username, password, tag string) {
if err := takeSnapshot(k8sClient, namespace, pod, container, keyspace, username, password, tag); err != nil {
if err := takeSnapshot(k8sClient, namespace, pod, container, keyspace, username, password, realtag); err != nil {
log.Fatal(err)
}
bwg.Done()
}(k8sClient, namespace, pod, container, keyspace, username, password, tag)
}(k8sClient, namespace, pod, container, keyspace, username, password, realtag)
}
bwg.Wait()

return tag
return realtag
}

// ClearSnapshots clears a snapshot using nodetool in all pods in parallel
Expand Down

0 comments on commit 1edd313

Please sign in to comment.