-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.go
84 lines (69 loc) · 2.03 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
package main
import (
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/robfig/cron"
)
var (
// flagPort is the open port the application listens on
flagPort = flag.String("port", "9000", "Port to listen on")
)
// PostHandler converts post request body to string
func PostHandler(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
log.Println("Manual backup requested.")
AppFunction()
log.Println("Manual backup completed.")
} else {
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed)
}
}
func init() {
log.SetFlags(log.Lmicroseconds | log.Lshortfile)
flag.Parse()
}
// AppFunction is a function to run the application
func AppFunction() {
fileName := fmt.Sprintf("%s_backup_%d", os.Getenv("COCKROACH_DATABASE"), time.Now().Unix())
sqlFileNamePath := "/data/" + fileName + ".sql"
Backup(sqlFileNamePath)
zipFileName := fileName + ".tar.gz"
zipDestinationFilePath := "/data/" + zipFileName
RecursiveZip(sqlFileNamePath, zipDestinationFilePath)
log.Printf("%s zip created.\n", zipDestinationFilePath)
if os.Getenv("ACCESS_KEY_ID") != "" {
if os.Getenv("BUCKET_NAME") == "" {
log.Fatalln("BUCKET_NAME can't be blank.")
}
if os.Getenv("S3_URL") == "" {
log.Fatalln("S3_URL can't be blank")
}
if os.Getenv("SECRET_ACCESS_KEY") == "" {
log.Fatalln("SECRET_ACCESS_KEY can't be blank.")
}
Upload(zipFileName, sqlFileNamePath, zipDestinationFilePath)
}
}
func main() {
if os.Getenv("COCKROACH_DATABASE") == "" {
log.Fatalln("Database env is not defined. Please set is by passing -e COCKROACH_DATABASE=.")
}
if os.Getenv("CRON_SCHEDULE") == "" {
log.Fatalln("CRON_SCHEDULE can't be blank.")
}
if os.Getenv("COCKROACH_USER") == "" {
os.Setenv("COCKROACH_USER", "root")
}
c := cron.New()
c.AddFunc(os.Getenv("CRON_SCHEDULE"), AppFunction)
c.Start()
log.Println("Backup scheduler started successfully.")
mux := http.NewServeMux()
mux.HandleFunc("/backup", PostHandler)
log.Printf("listening on port %s", *flagPort)
log.Fatal(http.ListenAndServe(":"+*flagPort, mux))
}