-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
102 lines (83 loc) · 2.15 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package main
import (
"bytes"
"context"
"flag"
"log"
"os"
"os/exec"
"regexp"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/google/uuid"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
var emailRegex = regexp.MustCompile(`\t[a-z0-9._%+!$&*=^|~#'?{}/\-]+?@[a-z0-9\- ]+?\.[^@' ]+?\t`)
func main() {
// read in flags
flag.Parse()
if flag.NArg() < 1 {
log.Fatal("Usage: go run main.go <host> <port> <user> <password> <endpoint> <bucket> <key> <secret> <path>")
}
host := flag.Arg(0)
port := flag.Arg(1)
user := flag.Arg(2)
password := flag.Arg(3)
endpoint := flag.Arg(4)
bucket := flag.Arg(5)
key := flag.Arg(6)
secret := flag.Arg(7)
path := flag.Arg(8)
// set password
_ = os.Setenv("PGPASSWORD", password)
// execute pg_dump
cmd := exec.Command(
"pg_dump",
"-h", host,
"-p", port,
"-U", user,
"-b",
"--quote-all-identifiers",
"--exclude-table-data", "user_sessions",
)
output, err := cmd.CombinedOutput()
if err != nil {
log.Fatalf("error dumping data: %s\noutput: %s", err, string(output))
}
outStr := string(output)
// sanitize data
uuid.EnableRandPool()
// replace all emails
outStr = emailRegex.ReplaceAllStringFunc(outStr, func(s string) string {
return "\t" + uuid.New().String() + "@" + uuid.New().String() + ".com\t"
})
// save data
//if err := os.WriteFile("out.sql", []byte(outStr), 0644); err != nil {
// log.Fatalf("error saving output: %s", err)
//}
// upload to s3
cfg, err := config.LoadDefaultConfig(context.TODO(),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(key, secret, "")),
config.WithRegion("us-west-2"),
)
if err != nil {
log.Fatalf("failed loading config: %s", err)
}
client := s3.NewFromConfig(
cfg,
func(o *s3.Options) {
o.BaseEndpoint = aws.String(endpoint)
o.UsePathStyle = true
},
)
_, err = client.PutObject(context.TODO(), &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(path),
Body: bytes.NewReader([]byte(outStr)),
})
if err != nil {
log.Fatalf("failed uploading object: %s", err)
}
log.Println("successfully uploaded object")
}