MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / CreateBackup

Method CreateBackup

pkg/api/vm_backups.go:149–207  ·  view source on GitHub ↗

CreateBackup creates a new backup for the VM. It returns the UPID of the backup task.

(vm *VM, options BackupOptions)

Source from the content-addressed store, hash-verified

147// CreateBackup creates a new backup for the VM.
148// It returns the UPID of the backup task.
149func (c *Client) CreateBackup(vm *VM, options BackupOptions) (string, error) {
150 path := fmt.Sprintf("/nodes/%s/vzdump", vm.Node)
151
152 data := map[string]interface{}{
153 "vmid": fmt.Sprintf("%d", vm.ID),
154 "mode": options.Mode,
155 }
156
157 if options.Storage != "" {
158 data["storage"] = options.Storage
159 } else {
160 return "", fmt.Errorf("target storage is required")
161 }
162
163 if options.Compression != "" {
164 data["compress"] = options.Compression
165 }
166
167 if options.Notes != "" {
168 // PVE uses 'notes-template' usually? No, 'notes-template' is for template string.
169 // Wait, API says `notes-template`. But maybe we just want to set notes after?
170 // Actually, vzdump usually doesn't take 'notes' directly as a simple string for the specific backup,
171 // unless we use `notes-template` without variables?
172 // Let's check API.
173 // API: `notes-template`. "Template string for generating notes...".
174 // There is no `notes` parameter for `vzdump` directly in the spec I saw earlier?
175 // Let's assume we can't set arbitrary notes easily during creation via API parameter `notes`.
176 // But we can use `notes-template` as the note if it doesn't contain variables.
177 data["notes-template"] = options.Notes
178 }
179
180 if options.Remove {
181 data["remove"] = "1"
182 }
183
184 // notification-mode?
185 if options.Notification != "" {
186 data["notification-mode"] = options.Notification
187 }
188
189 c.logger.Info("Starting backup for %s %s (ID: %d) to storage %s", vm.Type, vm.Name, vm.ID, options.Storage)
190
191 var result map[string]interface{}
192 err := c.httpClient.Post(context.Background(), path, data, &result)
193 if err != nil {
194 return "", fmt.Errorf("backup request failed: %w", err)
195 }
196
197 if errMsg, ok := result["error"].(string); ok && errMsg != "" {
198 return "", fmt.Errorf("backup failed: %s", errMsg)
199 }
200
201 // Get UPID
202 if upid, ok := result["data"].(string); ok && strings.HasPrefix(upid, "UPID:") {
203 return upid, nil
204 }
205
206 return "", fmt.Errorf("failed to get backup task ID")

Callers

nothing calls this directly

Calls 2

InfoMethod · 0.65
PostMethod · 0.45

Tested by

no test coverage detected