@@ -11,30 +11,44 @@ import (
1111)
1212
1313const (
14- // Defining variable or secret readings.
15- VAR_ORG_ID = "SCW_DEFAULT_ORGANIZATION_ID"
16- VAR_AK = "SCW_ACCESS_KEY"
17- VAR_SK = "SCW_SECRET_KEY"
18- VAR_REGION = "REGION"
19- VAR_RDB_ID = "INSTANCE_ID"
20-
21- // optional, never expires if not definied.
22- VAR_EXPIRE_AT_DAYS = "EXPIRE_AT_DAYS"
14+ envOrgID = "SCW_DEFAULT_ORGANIZATION_ID" // Scaleway organization ID
15+ envAccessKey = "SCW_ACCESS_KEY" // Scaleway API access key
16+ envSecretKey = "SCW_SECRET_KEY" // Scaleway API secret key
17+ envProjectID = "SCW_PROJECT_ID" // Scaleway project ID
18+
19+ envRegion = "SCW_REGION"
20+ envDatabaseID = "SCW_RDB_ID"
21+ envBackupExpirationDays = "SCW_EXPIRATION_DAYS"
2322)
2423
24+ // Check for mandatory variables before starting to work.
25+ func init () {
26+ // Slice of environmental variables that must be set for the application to run
27+ mandatoryVariables := [... ]string {envOrgID , envAccessKey , envSecretKey , envProjectID , envRegion }
28+
29+ // Iterate through the slice and check if any variables are not set
30+ for idx := range mandatoryVariables {
31+ if os .Getenv (mandatoryVariables [idx ]) == "" {
32+ panic ("missing environment variable " + mandatoryVariables [idx ])
33+ }
34+ }
35+ }
36+
2537func main () {
2638 fmt .Println ("creating backup of managed database..." )
2739
28- // Create a Scaleway client with credentials from environment variables.
40+ // Create a Scaleway client with credentials provided via environment variables.
41+ // The client is used to interact with the Scaleway API
2942 client , err := scw .NewClient (
3043 // Get your organization ID at https://console.scaleway.com/organization/settings
31- scw .WithDefaultOrganizationID (os .Getenv (VAR_ORG_ID )),
44+ scw .WithDefaultOrganizationID (os .Getenv (envOrgID )),
3245
3346 // Get your credentials at https://console.scaleway.com/iam/api-keys
34- scw .WithAuth (os .Getenv (VAR_AK ), os .Getenv (VAR_SK )),
47+ scw .WithAuth (os .Getenv (envAccessKey ), os .Getenv (envSecretKey )),
3548
36- // Get more about our availability zones at https://www.scaleway.com/en/docs/console/my-account/reference-content/products-availability/
37- scw .WithDefaultRegion (scw .RegionFrPar ),
49+ // Get more about our availability
50+ // zones at https://www.scaleway.com/en/docs/console/my-account/reference-content/products-availability/
51+ scw .WithDefaultRegion (scw .Region (os .Getenv (envRegion ))),
3852 )
3953 if err != nil {
4054 panic (err )
@@ -49,15 +63,15 @@ func main() {
4963
5064func createRdbSnapshot (rdbAPI * rdb.API ) error {
5165 rdbInstance , err := rdbAPI .GetInstance (& rdb.GetInstanceRequest {
52- Region : scw .Region (os .Getenv (VAR_REGION )),
53- InstanceID : os .Getenv (VAR_RDB_ID ),
66+ Region : scw .Region (scw . Region ( os .Getenv (envRegion ) )),
67+ InstanceID : os .Getenv (envDatabaseID ),
5468 })
5569 if err != nil {
5670 return fmt .Errorf ("error while getting database instance %w" , err )
5771 }
5872
5973 databasesList , err := rdbAPI .ListDatabases (& rdb.ListDatabasesRequest {
60- Region : scw .Region (os .Getenv (VAR_REGION )),
74+ Region : scw .Region (os .Getenv (envRegion )),
6175 InstanceID : rdbInstance .ID ,
6276 })
6377 if err != nil {
@@ -69,13 +83,16 @@ func createRdbSnapshot(rdbAPI *rdb.API) error {
6983 return fmt .Errorf ("error while getting expiration date %w" , err )
7084 }
7185
72- tn := time .Now ()
73- backupName := fmt .Sprintf ("backup_%s_%d%d%d" , rdbInstance .Name , tn .Year (), tn .Month (), tn .Day ())
86+ now := time .Now ()
7487
7588 for _ , database := range databasesList .Databases {
89+ backupName := fmt .Sprintf ("backup-%s-%s-%s" ,
90+ database .Name ,
91+ now ,
92+ os .Getenv (envRegion ))
7693
7794 backup , err := rdbAPI .CreateDatabaseBackup (& rdb.CreateDatabaseBackupRequest {
78- Region : scw .Region (os .Getenv (VAR_REGION )),
95+ Region : scw .Region (os .Getenv (envRegion )),
7996 InstanceID : rdbInstance .ID ,
8097 Name : backupName ,
8198 DatabaseName : database .Name ,
@@ -93,7 +110,7 @@ func createRdbSnapshot(rdbAPI *rdb.API) error {
93110
94111func getExpirationDate () (* time.Time , error ) {
95112 var expiresAt * time.Time
96- expireDays := os .Getenv (VAR_EXPIRE_AT_DAYS )
113+ expireDays := os .Getenv (envBackupExpirationDays )
97114
98115 if expireDays != "" {
99116 expireDaysInt , err := strconv .Atoi (expireDays )
@@ -109,25 +126,3 @@ func getExpirationDate() (*time.Time, error) {
109126
110127 return expiresAt , nil
111128}
112-
113- func init () {
114- if os .Getenv (VAR_ORG_ID ) == "" {
115- panic ("missing " + VAR_ORG_ID )
116- }
117-
118- if os .Getenv (VAR_AK ) == "" {
119- panic ("missing " + VAR_AK )
120- }
121-
122- if os .Getenv (VAR_SK ) == "" {
123- panic ("missing " + VAR_SK )
124- }
125-
126- if os .Getenv (VAR_RDB_ID ) == "" {
127- panic ("missing " + VAR_RDB_ID )
128- }
129-
130- if os .Getenv (VAR_REGION ) == "" {
131- panic ("missing " + VAR_REGION )
132- }
133- }
0 commit comments