File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -55,3 +55,32 @@ resource "harvester_virtualmachine" "this" {
5555 }
5656 }
5757}
58+
59+ # Optional scheduled backup — created only when backup_schedule is set.
60+ # Uses kubernetes_manifest because harvester_schedule_backup requires provider >= 1.8.
61+ resource "kubernetes_manifest" "scheduled_backup" {
62+ count = var. backup_schedule != null ? 1 : 0
63+
64+ manifest = {
65+ apiVersion = " harvesterhci.io/v1beta1"
66+ kind = " ScheduleVMBackup"
67+ metadata = {
68+ name = " ${ var . name } -backup"
69+ namespace = var.namespace
70+ }
71+ spec = {
72+ cron = var.backup_schedule
73+ retain = var.backup_retain
74+ maxFailure = var.backup_max_failure
75+ suspend = ! var.backup_enabled
76+ vmbackup = {
77+ source = {
78+ apiGroup = " kubevirt.io"
79+ kind = " VirtualMachine"
80+ name = harvester_virtualmachine.this.name
81+ }
82+ type = " backup"
83+ }
84+ }
85+ }
86+ }
Original file line number Diff line number Diff line change @@ -17,3 +17,8 @@ output "ssh_key_id" {
1717 value = var. ssh_public_key != null ? harvester_ssh_key. this [0 ]. id : null
1818 description = " Harvester SSH key ID attached to the VM, or null if no SSH key was provided."
1919}
20+
21+ output "backup_schedule_name" {
22+ value = var. backup_schedule != null ? kubernetes_manifest. scheduled_backup [0 ]. manifest . metadata . name : null
23+ description = " Name of the scheduled VM backup, or null if no backup schedule was configured."
24+ }
Original file line number Diff line number Diff line change @@ -87,3 +87,29 @@ variable "additional_disks" {
8787 description = " List of additional disks to attach to the VM."
8888 default = []
8989}
90+
91+ # --- Scheduled backups ---
92+
93+ variable "backup_schedule" {
94+ type = string
95+ description = " Cron schedule for VM backups in UTC (e.g. \" 0 2 * * *\" for daily at 2 AM). Set to null to disable scheduled backups."
96+ default = null
97+ }
98+
99+ variable "backup_retain" {
100+ type = number
101+ description = " Number of backups to retain when scheduled backups are enabled."
102+ default = 5
103+ }
104+
105+ variable "backup_enabled" {
106+ type = bool
107+ description = " Whether the backup schedule is active. Only applies when backup_schedule is set."
108+ default = true
109+ }
110+
111+ variable "backup_max_failure" {
112+ type = number
113+ description = " Maximum consecutive failed backup attempts before suspending the schedule."
114+ default = 4
115+ }
Original file line number Diff line number Diff line change @@ -5,5 +5,9 @@ terraform {
55 source = " harvester/harvester"
66 version = " ~> 1.7"
77 }
8+ kubernetes = {
9+ source = " hashicorp/kubernetes"
10+ version = " >= 2.0"
11+ }
812 }
913}
You can’t perform that action at this time.
0 commit comments