Skip to content

Commit fe86e0c

Browse files
authored
Merge pull request #93 from davcarde/main
Adding 3 new code examples
2 parents f7ecda0 + c3bd696 commit fe86e0c

32 files changed

Lines changed: 1076 additions & 38 deletions

File tree

terraform/adb_from_subnet_private_endpoint/.terraform.lock.hcl

Lines changed: 0 additions & 25 deletions
This file was deleted.

terraform/adb_from_subnet_private_endpoint/terraform.tfvars

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ nsg_display_name = ""
2424
adb_display_name = ""
2525
adb_db_name = ""
2626
adb_admin_password = ""
27-
adb_workload_type = "DW" # OLTP=ATP | DW=ADW | AJD=JSON | APEX
27+
adb_workload_type = "LH" # OLTP=ATP | LH=LAKEHOUSE | DW=ADW | AJD=JSON | APEX
2828
adb_db_version = "26ai"
2929
adb_cpu_core_count = 2
3030
adb_storage_tbs = 1
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
# Terraform — Long-Term Backup Scheduling and Retention Policy
2+
3+
Configures automatic backup retention and a long-term backup schedule on an existing Autonomous Database. Designed for compliance scenarios where data must be retained beyond the 60-day automatic backup limit.
4+
5+
## Files
6+
7+
| File | Description |
8+
|---|---|
9+
| `versions.tf` | Terraform and provider version requirements |
10+
| `provider.tf` | OCI provider configuration |
11+
| `main.tf` | Automatic backup retention and long-term backup schedule |
12+
| `variables.tf` | All configurable parameters |
13+
| `outputs.tf` | Values exported after apply |
14+
| `terraform.tfvars` | Fill in your values |
15+
16+
## Quick Start
17+
18+
```bash
19+
# 1. Initialize Terraform
20+
terraform init
21+
22+
# 2. Import the existing ADB into the Terraform state
23+
terraform import oci_database_autonomous_database.adb <ADB_OCID>
24+
25+
# 3. Review the plan — the ADB should show only backup-related changes
26+
terraform plan
27+
28+
# 4. Apply
29+
terraform apply
30+
```
31+
32+
## Backup Architecture
33+
34+
```
35+
Existing Autonomous Database
36+
├── Automatic backups (daily, managed by OCI)
37+
│ └── Retained for backup_retention_days (1–60 days)
38+
39+
└── Long-term backup schedule (managed by OCI natively)
40+
├── Cadence: ONE_TIME | WEEKLY | MONTHLY | YEARLY
41+
├── Anchor: backup_schedule_time (RFC3339)
42+
└── Retained for long_term_backup_retention_days (90–3650 days)
43+
```
44+
45+
## Schedule Reference
46+
47+
| Cadence | Behavior |
48+
|---|---|
49+
| `ONE_TIME` | Single backup taken at `backup_schedule_time` |
50+
| `WEEKLY` | Repeats every 7 days at the same time and day of week |
51+
| `MONTHLY` | Repeats on the same day each month (last day if >= 29) |
52+
| `YEARLY` | Repeats on the same date each year |
53+
54+
## Retention Reference
55+
56+
| Period | Days |
57+
|---|---|
58+
| 3 months (minimum) | 90 |
59+
| 1 year | 365 |
60+
| 2 years | 730 |
61+
| 5 years | 1825 |
62+
| 7 years | 2555 |
63+
| 10 years (maximum) | 3650 |
64+
65+
## Variables
66+
67+
| Variable | Description | Default |
68+
|---|---|---|
69+
| `tenancy_ocid` | OCID of the OCI tenancy ||
70+
| `user_ocid` | OCID of the OCI user ||
71+
| `fingerprint` | Fingerprint of the API key ||
72+
| `private_key_path` | Path to the private key file (.pem) ||
73+
| `region` | OCI region where the ADB resides | `us-ashburn-1` |
74+
| `compartment_ocid` | OCID of the compartment where the ADB resides ||
75+
| `adb_ocid` | OCID of the existing Autonomous Database ||
76+
| `adb_db_name` | Technical database name (must match exactly) ||
77+
| `adb_admin_password` | ADMIN password (required by provider, not modified) ||
78+
| `backup_retention_days` | Automatic daily backup retention in days (1–60) | `30` |
79+
| `backup_schedule_cadence` | Backup frequency: `ONE_TIME`, `WEEKLY`, `MONTHLY`, `YEARLY` | `MONTHLY` |
80+
| `backup_schedule_time` | RFC3339 anchor timestamp for the schedule ||
81+
| `long_term_backup_retention_days` | Long-term backup retention in days (90–3650) | `365` |
82+
83+
## Outputs
84+
85+
| Output | Description |
86+
|---|---|
87+
| `adb_id` | OCID of the Autonomous Database |
88+
| `backup_retention_days` | Automatic backup retention period in days |
89+
| `backup_schedule_cadence` | Frequency of the long-term backup schedule |
90+
| `backup_schedule_time` | Anchor timestamp for the long-term backup schedule |
91+
| `long_term_backup_retention_days` | Long-term backup retention period in days |
92+
93+
## Notes
94+
95+
- **Existing ADB required:** This Terraform is designed for existing databases only. Import the ADB before running `terraform apply` — see Quick Start above.
96+
97+
- **Automatic backup prerequisite:** OCI requires at least one automatic backup to exist before the long-term backup schedule activates. After provisioning a new ADB, wait up to 4 hours for the first automatic backup to complete.
98+
99+
- **`backup_schedule_time` format:** Must be a valid RFC3339 timestamp in UTC. Example: `2025-06-01T02:00:00Z`. This timestamp serves as both the first backup date and the anchor point for the recurring schedule.
100+
101+
- **MONTHLY cadence edge case:** If `backup_schedule_time` falls on day 29, 30, or 31, OCI takes the backup on the last day of months with fewer days.
102+
103+
- **`admin_password` in tfvars:** Required by the OCI provider schema but listed in `ignore_changes`. Terraform will never use it to modify the database password.
104+
105+
- **Automatic backup limit:** OCI automatic backups support a maximum of 60 days. For retention beyond 60 days, the long-term backup schedule is required.
106+
107+
- **Storage costs:** Long-term backups incur additional Object Storage costs beyond the standard ADB storage bill.
108+
109+
- **Restore from long-term backup:** Long-term backups can only be used to clone a new database, not to restore in-place. Go to your ADB in the OCI console → Backups → select the long-term backup → click Clone.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# ============================================================
2+
# main.tf — Existing ADB — backup retention and schedule
3+
# Updates automatic backup retention and configures a long-term
4+
# backup schedule on an existing Autonomous Database.
5+
# ============================================================
6+
7+
resource "oci_database_autonomous_database" "adb" {
8+
# Required by the provider schema — values must match the existing ADB.
9+
compartment_id = var.compartment_ocid
10+
db_name = var.adb_db_name
11+
admin_password = var.adb_admin_password
12+
13+
# ── Automatic backup retention ────────────────────────────
14+
# Retention period for daily automatic backups (1–60 days).
15+
# For retention beyond 60 days use long_term_backup_schedule below.
16+
backup_retention_period_in_days = var.backup_retention_days
17+
18+
# ── Long-term backup schedule ─────────────────────────────
19+
# OCI manages the schedule natively — no crontab or external tooling required.
20+
# repeat_cadence options: ONE_TIME | WEEKLY | MONTHLY | YEARLY
21+
# time_of_backup: RFC3339 timestamp — anchor point for the recurring schedule.
22+
long_term_backup_schedule {
23+
repeat_cadence = var.backup_schedule_cadence
24+
retention_period_in_days = var.long_term_backup_retention_days
25+
time_of_backup = var.backup_schedule_time
26+
is_disabled = false
27+
}
28+
29+
# ── Lifecycle ─────────────────────────────────────────────
30+
# Ignore fields not relevant to backup configuration so that
31+
# Terraform does not attempt to modify the existing ADB.
32+
lifecycle {
33+
ignore_changes = [
34+
admin_password,
35+
display_name,
36+
db_workload,
37+
db_version,
38+
compute_model,
39+
compute_count,
40+
data_storage_size_in_tbs,
41+
license_model,
42+
is_mtls_connection_required,
43+
whitelisted_ips,
44+
freeform_tags,
45+
]
46+
}
47+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ============================================================
2+
# outputs.tf — Values exported after apply
3+
# ============================================================
4+
5+
# ── ADB ───────────────────────────────────────────────────────
6+
output "adb_id" {
7+
description = "OCID of the Autonomous Database"
8+
value = oci_database_autonomous_database.adb.id
9+
}
10+
11+
output "backup_retention_days" {
12+
description = "Automatic backup retention period in days"
13+
value = oci_database_autonomous_database.adb.backup_retention_period_in_days
14+
}
15+
16+
# ── Long-term backup schedule ─────────────────────────────────
17+
output "backup_schedule_cadence" {
18+
description = "Frequency of the long-term backup schedule"
19+
value = var.backup_schedule_cadence
20+
}
21+
22+
output "backup_schedule_time" {
23+
description = "Anchor timestamp for the long-term backup schedule"
24+
value = var.backup_schedule_time
25+
}
26+
27+
output "long_term_backup_retention_days" {
28+
description = "Long-term backup retention period in days"
29+
value = var.long_term_backup_retention_days
30+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
provider "oci" {
2+
tenancy_ocid = var.tenancy_ocid
3+
user_ocid = var.user_ocid
4+
fingerprint = var.fingerprint
5+
private_key_path = var.private_key_path
6+
region = var.region
7+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# ── OCI Credentials ──────────────────────────────────────────
2+
tenancy_ocid = ""
3+
user_ocid = ""
4+
fingerprint = ""
5+
private_key_path = ""
6+
region = "us-ashburn-1"
7+
8+
compartment_ocid = ""
9+
10+
# ── Existing ADB ──────────────────────────────────────────────
11+
adb_ocid = ""
12+
adb_db_name = ""
13+
adb_admin_password = ""
14+
15+
# ── Backup — automatic ────────────────────────────────────────
16+
backup_retention_days = 60 # 1–60 days
17+
18+
# ── Backup — long-term schedule ───────────────────────────────
19+
backup_schedule_cadence = "MONTHLY" # ONE_TIME | WEEKLY | MONTHLY | YEARLY
20+
backup_schedule_time = "2026-06-01T02:00:00Z" # RFC3339 — first backup and recurring anchor
21+
long_term_backup_retention_days = 365 # 365=1yr | 730=2yr | 1825=5yr | 3650=10yr
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
# ============================================================
2+
# variables.tf — Configurable parameters
3+
# ============================================================
4+
5+
# ── OCI Credentials ──────────────────────────────────────────
6+
variable "tenancy_ocid" {
7+
description = "OCID of the Oracle Cloud tenancy"
8+
type = string
9+
}
10+
11+
variable "user_ocid" {
12+
description = "OCID of the OCI user"
13+
type = string
14+
}
15+
16+
variable "fingerprint" {
17+
description = "Fingerprint of the user's API key"
18+
type = string
19+
}
20+
21+
variable "private_key_path" {
22+
description = "Path to the private key file (.pem)"
23+
type = string
24+
}
25+
26+
variable "region" {
27+
description = "OCI region where the ADB resides"
28+
type = string
29+
default = "us-ashburn-1"
30+
}
31+
32+
variable "compartment_ocid" {
33+
description = "OCID of the compartment where the ADB resides"
34+
type = string
35+
}
36+
37+
# ── Existing ADB ──────────────────────────────────────────────
38+
variable "adb_ocid" {
39+
description = "OCID of the existing Autonomous Database"
40+
type = string
41+
}
42+
43+
variable "adb_db_name" {
44+
description = "Technical database name of the existing ADB (must match exactly)"
45+
type = string
46+
}
47+
48+
variable "adb_admin_password" {
49+
description = "ADMIN password of the existing ADB (required by provider schema, not modified)"
50+
type = string
51+
sensitive = true
52+
}
53+
54+
# ── Backup — automatic ────────────────────────────────────────
55+
variable "backup_retention_days" {
56+
description = "Retention period for automatic daily backups (1–60 days)"
57+
type = number
58+
default = 30
59+
60+
validation {
61+
condition = var.backup_retention_days >= 1 && var.backup_retention_days <= 60
62+
error_message = "Must be between 1 and 60 days."
63+
}
64+
}
65+
66+
# ── Backup — long-term schedule ───────────────────────────────
67+
variable "backup_schedule_cadence" {
68+
description = "Frequency of the long-term backup schedule: ONE_TIME | WEEKLY | MONTHLY | YEARLY"
69+
type = string
70+
default = "MONTHLY"
71+
72+
validation {
73+
condition = contains(["ONE_TIME", "WEEKLY", "MONTHLY", "YEARLY"], var.backup_schedule_cadence)
74+
error_message = "Must be one of: ONE_TIME, WEEKLY, MONTHLY, YEARLY."
75+
}
76+
}
77+
78+
variable "backup_schedule_time" {
79+
description = "RFC3339 timestamp — anchor point for the recurring schedule. Example: 2025-06-01T02:00:00Z"
80+
type = string
81+
}
82+
83+
variable "long_term_backup_retention_days" {
84+
description = "Retention period for long-term backups in days (90–3650)"
85+
type = number
86+
default = 365
87+
88+
validation {
89+
condition = var.long_term_backup_retention_days >= 90 && var.long_term_backup_retention_days <= 3650
90+
error_message = "Must be between 90 and 3650 days. Reference: 365=1yr, 730=2yr, 1825=5yr, 3650=10yr."
91+
}
92+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.5.0"
3+
4+
required_providers {
5+
oci = {
6+
source = "oracle/oci"
7+
version = ">= 6.0.0"
8+
}
9+
}
10+
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
# ── OCI Credentials ──────────────────────────────────────────
2-
tenancy_ocid = "ocid1.tenancy.oc1..aaaaaaaakc2xkehakt7bmhmipdbhz3tbkej53jzwmrlmuqoloydlotthkrbq"
3-
user_ocid = "ocid1.user.oc1..aaaaaaaas3nxkab5ct2mhfno7j2ltpcsiyhi5xzrs7xwcprpmpbfoxvsylva"
4-
fingerprint = "ae:0d:91:96:d9:10:07:81:ba:f4:b2:af:db:48:06:25"
5-
private_key_path = "/Users/davcarde/.oci/oci_api_key_t5.pem"
2+
tenancy_ocid = ""
3+
user_ocid = ""
4+
fingerprint = ""
5+
private_key_path = ""
66
region = "us-ashburn-1"
77

8-
compartment_ocid = "ocid1.tenancy.oc1..aaaaaaaakc2xkehakt7bmhmipdbhz3tbkej53jzwmrlmuqoloydlotthkrbq"
8+
compartment_ocid = ""
99

1010
# ── ADB Configuration ─────────────────────────────────────────
11-
adb_display_name = "xiaTest2"
12-
adb_db_name = "xiaTest2"
13-
adb_admin_password = "HolaMundo1330"
14-
adb_workload_type = "AJD" # OLTP=ATP | DW=ADW | AJD=JSON | APEX
11+
adb_display_name = ""
12+
adb_db_name = ""
13+
adb_admin_password = ""
14+
adb_workload_type = "LH" # OLTP=ATP | LH=LAKEHOUSE | DW=ADW | AJD=JSON | APEX
1515
adb_cpu_core_count = 2 # ECPU: minimum 2, multiples of 2
1616
adb_storage_tbs = 1
1717
adb_auto_scaling = false

0 commit comments

Comments
 (0)