|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strconv" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/scaleway/scaleway-sdk-go/api/rdb/v1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +const ( |
| 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" |
| 23 | +) |
| 24 | + |
| 25 | +func main() { |
| 26 | + fmt.Println("creating backup of managed database...") |
| 27 | + |
| 28 | + // Create a Scaleway client with credentials from environment variables. |
| 29 | + client, err := scw.NewClient( |
| 30 | + // Get your organization ID at https://console.scaleway.com/organization/settings |
| 31 | + scw.WithDefaultOrganizationID(os.Getenv(VAR_ORG_ID)), |
| 32 | + |
| 33 | + // Get your credentials at https://console.scaleway.com/iam/api-keys |
| 34 | + scw.WithAuth(os.Getenv(VAR_AK), os.Getenv(VAR_SK)), |
| 35 | + |
| 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), |
| 38 | + ) |
| 39 | + if err != nil { |
| 40 | + panic(err) |
| 41 | + } |
| 42 | + |
| 43 | + rdbAPI := rdb.NewAPI(client) |
| 44 | + |
| 45 | + if err := createRdbSnapshot(rdbAPI); err != nil { |
| 46 | + panic(err) |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +func createRdbSnapshot(rdbAPI *rdb.API) error { |
| 51 | + rdbInstance, err := rdbAPI.GetInstance(&rdb.GetInstanceRequest{ |
| 52 | + Region: scw.Region(os.Getenv(VAR_REGION)), |
| 53 | + InstanceID: os.Getenv(VAR_RDB_ID), |
| 54 | + }) |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("error while getting database instance %w", err) |
| 57 | + } |
| 58 | + |
| 59 | + databasesList, err := rdbAPI.ListDatabases(&rdb.ListDatabasesRequest{ |
| 60 | + Region: scw.Region(os.Getenv(VAR_REGION)), |
| 61 | + InstanceID: rdbInstance.ID, |
| 62 | + }) |
| 63 | + if err != nil { |
| 64 | + return fmt.Errorf("error while listing databases %w", err) |
| 65 | + } |
| 66 | + |
| 67 | + expiresAt, err := getExpirationDate() |
| 68 | + if err != nil { |
| 69 | + return fmt.Errorf("error while getting expiration date %w", err) |
| 70 | + } |
| 71 | + |
| 72 | + tn := time.Now() |
| 73 | + backupName := fmt.Sprintf("backup_%s_%d%d%d", rdbInstance.Name, tn.Year(), tn.Month(), tn.Day()) |
| 74 | + |
| 75 | + for _, database := range databasesList.Databases { |
| 76 | + |
| 77 | + backup, err := rdbAPI.CreateDatabaseBackup(&rdb.CreateDatabaseBackupRequest{ |
| 78 | + Region: scw.Region(os.Getenv(VAR_REGION)), |
| 79 | + InstanceID: rdbInstance.ID, |
| 80 | + Name: backupName, |
| 81 | + DatabaseName: database.Name, |
| 82 | + ExpiresAt: expiresAt, |
| 83 | + }) |
| 84 | + if err != nil { |
| 85 | + return fmt.Errorf("error while creating database backup request %w", err) |
| 86 | + } |
| 87 | + |
| 88 | + fmt.Println("Created backup ", backup.Name) |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func getExpirationDate() (*time.Time, error) { |
| 95 | + var expiresAt *time.Time |
| 96 | + expireDays := os.Getenv(VAR_EXPIRE_AT_DAYS) |
| 97 | + |
| 98 | + if expireDays != "" { |
| 99 | + expireDaysInt, err := strconv.Atoi(expireDays) |
| 100 | + if err != nil { |
| 101 | + return nil, fmt.Errorf("error while getting %w", err) |
| 102 | + } |
| 103 | + |
| 104 | + if expireDaysInt > 0 { |
| 105 | + expiration := time.Now().AddDate(0, 0, expireDaysInt) |
| 106 | + expiresAt = &expiration |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + return expiresAt, nil |
| 111 | +} |
| 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