-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: compress dashboards and backup to s3
* compress dashboards to a gzipped file by passing --compress * backup downloaded dashboards to s3. requires awscli to be configured Signed-off-by: Chinmay D. Pai <[email protected]>
- Loading branch information
1 parent
0aeb8b5
commit 7ec0b7c
Showing
7 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
"time" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/aws/session" | ||
"github.com/aws/aws-sdk-go/service/s3/s3manager" | ||
"github.com/mholt/archiver/v3" | ||
) | ||
|
||
// compress is a function that creates a gzipped | ||
// archive for the specified filepath | ||
func compress(filepath string) (string, error) { | ||
if _, err := os.Stat(filepath); os.IsNotExist(err) { | ||
// the filepath specified does not exist | ||
return "", err | ||
} | ||
|
||
now := time.Now() | ||
// create a timestamped filename. | ||
// dashboards/ => dashboards-20060102150405.tar.gz | ||
arcFn := strings.TrimSuffix(filepath, "/") + "-" + now.Format("20060102150405") + ".tar.gz" | ||
|
||
tarGZ := archiver.NewTarGz() | ||
if err := tarGZ.Archive([]string{filepath}, arcFn); err != nil { | ||
return "", err | ||
} | ||
|
||
return arcFn, nil | ||
} | ||
|
||
// backup is a function that takes a file and uploads | ||
// it to the specified s3 bucket | ||
func backup(filepath, bucket, key string) error { | ||
if _, err := os.Stat(filepath); os.IsNotExist(err) { | ||
return err | ||
} | ||
|
||
f, err := os.Open(filepath) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
|
||
sess, err := session.NewSession() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
uploader := s3manager.NewUploader(sess) | ||
_, err = uploader.Upload(&s3manager.UploadInput{ | ||
ACL: aws.String("private"), | ||
Body: f, | ||
Bucket: aws.String(bucket), | ||
Key: aws.String(key), | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |