-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
113 lines (96 loc) · 2.73 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
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/robfig/cron/v3"
vscale "github.com/vscale/go-vscale"
)
const (
appName = "vscale-backup"
)
var (
flToken = flag.String("token", os.Getenv("API_TOKEN"), "Vscale API token")
flExpiration = flag.String("expiration", os.Getenv("BACKUP_EXPIRATION"), "Backups expiration time, older backups will be removed")
flCron = flag.String("cron", os.Getenv("CRON"), "Cron spec for periodically backup run")
)
var (
version = "dev"
c *vscale.WebClient
interval time.Duration
)
func main() {
flag.Parse()
if *flToken == "" || *flExpiration == "" || *flCron == "" {
fmt.Println(`Usage example: vscale-backup -token "vscale-api-token" -expiration "48h" -cron "15 3 * * *"`)
os.Exit(1)
}
log.Printf("starting %v version=%q", appName, version)
var err error
interval, err = time.ParseDuration(*flExpiration)
if err != nil {
log.Fatalf("Invalid backup expiration '%s': %v", *flExpiration, err)
}
c = vscale.NewClient(*flToken)
cr := cron.New()
_, err = cr.AddFunc(*flCron, processBackups)
if err != nil {
log.Fatalf("Error creating cron job with spec '%s': %v", *flCron, err)
}
cr.Start()
go func() {
for {
time.Sleep(time.Second)
}
}()
quitChannel := make(chan os.Signal, 1)
signal.Notify(quitChannel, syscall.SIGINT, syscall.SIGTERM)
<-quitChannel
}
// processBackups do create/delete jobs for vscale backups
func processBackups() {
backupValidTill := time.Now().Add(-interval)
servers, _, err := c.Scalet.List()
if err != nil {
log.Fatalf("Failed to retrieve servlets: %v", err)
}
backupDate := time.Now().Format("2006-01-02")
for _, s := range *servers {
backupName := fmt.Sprintf("%s_%s", s.Name, backupDate)
_, _, err := c.Scalet.Backup(s.CTID, backupName)
if err != nil {
log.Printf("Error creating backup for servlet %s: %v", s.Name, err)
continue
}
log.Printf("Backup %s for servlet %s successfully created", backupName, s.Name)
}
backups, _, err := c.Backup.List()
if err != nil {
log.Fatalf("Failed to fetch backup list: %v", err)
}
for _, b := range *backups {
if b.Status != "finished" {
log.Printf("Skipping backup %s deletion: unfinished status", b.Name)
continue
}
createdAt, err := time.ParseInLocation("02.01.2006 15:04:05", b.Created, time.Local)
if err != nil {
log.Printf("Skipping backup %s deletion: invalid creation time: %v", b.Name, err)
continue
}
if createdAt.After(backupValidTill) {
log.Printf("Skipping backup %s deletion: not expired", b.Name)
continue
}
_, _, err = c.Backup.Remove(b.ID)
if err != nil {
log.Printf("Error removing old backup %s: %v", b.Name, err)
continue
}
log.Printf("Backup '%s' successfully deleted", b.Name)
}
}