Skip to content

Commit

Permalink
Automatically set Backblaze bucket
Browse files Browse the repository at this point in the history
  • Loading branch information
tom-collins-voormedia committed Jan 2, 2024
1 parent 0699fe1 commit 9207382
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 17 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,32 @@
## Installing

### Option 1 – prebuilt

1. Ensure you have `~/.bin` directory or similar that is in your `$PATH`
2. Install Voormedia Toolkit

- When using an Apple M1+ Mac run: `rm -f ~/.bin/vmt && curl -L $(curl -s https://api.github.com/repos/voormedia/voormedia-toolkit/releases/latest | grep browser_download_url | grep darwin_arm64 | cut -d '"' -f 4) -o ~/.bin/vmt && chmod +x ~/.bin/vmt`
- When using an Intel Mac run: `rm -f ~/.bin/vmt && curl -L $(curl -s https://api.github.com/repos/voormedia/voormedia-toolkit/releases/latest | grep browser_download_url | grep darwin_amd64 | cut -d '"' -f 4) -o ~/.bin/vmt && chmod +x ~/.bin/vmt`

### Option 2 – from source

1. Make sure you have a working `go` installation
2. Build Voormedia Toolkit from source: `go install github.com/voormedia/voormedia-toolkit`

## Restore script
To automatically connect to the correct Backblaze B2 bucket you should create the following environment variables:

The default Backblaze bucket is set depending on your selected GCP project (change this with `gcloud init`).

To connect to the Voormedia Backblaze B2 bucket, you should create the following environment variables:

- B2_ACCOUNT_ID (your Backblaze B2 account ID)
- B2_ACCOUNT_KEY (your Backblaze B2 account key)
- B2_ENCRYPTION_KEY (the key used to encrypt the backups)

To connect to the Taxology Backblaze B2 bucket, you should create the following environment variables:

- B2_TAXOLOGY_ACCOUNT_ID
- B2_TAXOLOGY_ACCOUNT_KEY
- B2_TAXOLOGY_ENCRYPTION_KEY

Alternatively you can pass in each value separately, see `vmt restore --help` for more information.
13 changes: 7 additions & 6 deletions cmd/backup.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
"github.com/voormedia/voormedia-toolkit/pkg/backup"
"github.com/voormedia/voormedia-toolkit/pkg/util"
)

var cmdBackup = &cobra.Command{
Expand All @@ -27,12 +26,14 @@ var cmdBackup = &cobra.Command{
}

func init() {
b2bucketName, b2encrypt, b2id, b2key := util.GetB2Config()

cmdRoot.AddCommand(cmdBackup)
cmdBackup.Flags().String("shard", "", "Specifies the shard that should be backup up (when multiple shards exist)")
cmdBackup.Flags().String("b2id", os.Getenv("B2_ACCOUNT_ID"), "Specifies the Backblaze B2 account ID")
cmdBackup.Flags().String("b2key", os.Getenv("B2_ACCOUNT_KEY"), "Specifies the Backblaze B2 account key")
cmdBackup.Flags().String("b2encrypt", os.Getenv("B2_ENCRYPTION_KEY"), "Specifies the Backblaze B2 encryption key")
cmdBackup.Flags().String("b2bucket", "voormedia-eu-db-backups", "Specifies the Backblaze B2 backup bucket")
cmdBackup.Flags().String("b2id", b2id, "Specifies the Backblaze B2 account ID")
cmdBackup.Flags().String("b2key", b2key, "Specifies the Backblaze B2 account key")
cmdBackup.Flags().String("b2encrypt", b2encrypt, "Specifies the Backblaze B2 encryption key")
cmdBackup.Flags().String("b2bucket", b2bucketName, "Specifies the Backblaze B2 backup bucket")
cmdBackup.Flags().String("port", "3307", "Specifies the port to use to reach the source database")
cmdBackup.Flags().String("host", "127.0.0.1", "Specifies the host to use to reach the source database.")
cmdBackup.Flags().String("dbconfig", "./config/database.yml", "Specifies the location of the application's database configuration file")
Expand Down
13 changes: 7 additions & 6 deletions cmd/restore.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package cmd

import (
"os"

"github.com/spf13/cobra"
"github.com/voormedia/voormedia-toolkit/pkg/restore"
"github.com/voormedia/voormedia-toolkit/pkg/util"
)

var cmdRestore = &cobra.Command{
Expand Down Expand Up @@ -32,13 +31,15 @@ var cmdRestore = &cobra.Command{
}

func init() {
b2bucketName, b2encrypt, b2id, b2key := util.GetB2Config()

cmdRoot.AddCommand(cmdRestore)
cmdRestore.Flags().String("target", "development", "Specifies the target environment of the backup restore")
cmdRestore.Flags().String("shard", "", "Specifies the shard that should be restored to (when multiple shards exist)")
cmdRestore.Flags().String("b2id", os.Getenv("B2_ACCOUNT_ID"), "Specifies the Backblaze B2 account ID")
cmdRestore.Flags().String("b2key", os.Getenv("B2_ACCOUNT_KEY"), "Specifies the Backblaze B2 account key")
cmdRestore.Flags().String("b2encrypt", os.Getenv("B2_ENCRYPTION_KEY"), "Specifies the Backblaze B2 encryption key")
cmdRestore.Flags().String("b2bucket", "voormedia-eu-db-backups", "Specifies the Backblaze B2 backup bucket")
cmdRestore.Flags().String("b2id", b2id, "Specifies the Backblaze B2 account ID")
cmdRestore.Flags().String("b2key", b2key, "Specifies the Backblaze B2 account key")
cmdRestore.Flags().String("b2encrypt", b2encrypt, "Specifies the Backblaze B2 encryption key")
cmdRestore.Flags().String("b2bucket", b2bucketName, "Specifies the Backblaze B2 backup bucket")
cmdRestore.Flags().String("dbconfig", "./config/database.yml", "Specifies the location of the application's database configuration file")
cmdRestore.Flags().String("port", "3306", "Specifies the port to use when restoring the target database")
cmdRestore.Flags().String("host", "127.0.0.1", "Specifies the host to use when restoring the target database.")
Expand Down
32 changes: 29 additions & 3 deletions pkg/util/b2.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package util
import (
"context"
"fmt"
"github.com/AlecAivazis/survey"
"github.com/kurin/blazer/b2"
"github.com/pkg/errors"
"io"
"log"
"os"
"os/exec"
"strings"

"github.com/AlecAivazis/survey"
"github.com/kurin/blazer/b2"
"github.com/pkg/errors"
)

// B2Bucket instance for Backblaze
Expand Down Expand Up @@ -120,3 +122,27 @@ func B2Setup(b2id string, b2key string, b2bucket string, b2encrypt string) (cont

return B2Bucket(credentials.B2id, credentials.B2key, credentials.B2bucket, credentials.B2encrypt, true)
}

// Returns the Backblaze B2 configuration for the current GCP project
func GetB2Config() (string, string, string, string) {
gcloudProject, err := GetCurrentGCPProject()
if err != nil {
log.Fatal(err)
}

var b2bucketName, b2encrypt, b2id, b2key string
switch gcloudProject {
case "taxology-381314":
b2bucketName = "taxology-eu-db-backups"
b2encrypt = os.Getenv("B2_TAXOLOGY_ENCRYPTION_KEY")
b2id = os.Getenv("B2_TAXOLOGY_ACCOUNT_ID")
b2key = os.Getenv("B2_TAXOLOGY_ACCOUNT_KEY")
default:
b2bucketName = "voormedia-eu-db-backups"
b2encrypt = os.Getenv("B2_ENCRYPTION_KEY")
b2id = os.Getenv("B2_ACCOUNT_ID")
b2key = os.Getenv("B2_ACCOUNT_KEY")
}

return b2bucketName, b2encrypt, b2id, b2key
}
17 changes: 16 additions & 1 deletion pkg/util/gcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,27 @@ package util

import (
"bytes"
"github.com/pkg/errors"
"os/exec"
"path/filepath"
"strings"

"github.com/pkg/errors"
)

// Retrieve the current GCP project name
func GetCurrentGCPProject() (string, error) {
cmd := exec.Command("gcloud", "config", "get-value", "project")
var out, errOut bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errOut
if err := cmd.Run(); err != nil {
return "", errors.Errorf("Failed to get GCP project name: %s", errOut.String())
}

projectName := strings.TrimSpace(out.String())
return projectName, nil
}

// FindSQLInstances for Google Cloud
func FindSQLInstances() ([]string, error) {
cmd := exec.Command("gcloud", "sql", "instances", "list", "--uri")
Expand Down

0 comments on commit 9207382

Please sign in to comment.