RestoreBackup restores a backup to the VM. If the VM exists, it will be overwritten.
(vm *VM, volID string)
| 245 | // RestoreBackup restores a backup to the VM. |
| 246 | // If the VM exists, it will be overwritten. |
| 247 | func (c *Client) RestoreBackup(vm *VM, volID string) (string, error) { |
| 248 | var path string |
| 249 | var data map[string]interface{} |
| 250 | |
| 251 | if vm.Type == VMTypeQemu { |
| 252 | path = fmt.Sprintf("/nodes/%s/qemu", vm.Node) |
| 253 | data = map[string]interface{}{ |
| 254 | "vmid": fmt.Sprintf("%d", vm.ID), |
| 255 | "archive": volID, |
| 256 | "force": "1", // Overwrite existing VM |
| 257 | } |
| 258 | } else if vm.Type == VMTypeLXC { |
| 259 | path = fmt.Sprintf("/nodes/%s/lxc", vm.Node) |
| 260 | data = map[string]interface{}{ |
| 261 | "vmid": fmt.Sprintf("%d", vm.ID), |
| 262 | "ostemplate": volID, |
| 263 | "restore": "1", |
| 264 | "force": "1", // Overwrite existing CT |
| 265 | } |
| 266 | } else { |
| 267 | return "", fmt.Errorf("unsupported VM type: %s", vm.Type) |
| 268 | } |
| 269 | |
| 270 | c.logger.Info("Restoring backup '%s' to %s %s (ID: %d)", volID, vm.Type, vm.Name, vm.ID) |
| 271 | |
| 272 | var result map[string]interface{} |
| 273 | err := c.httpClient.Post(context.Background(), path, data, &result) |
| 274 | if err != nil { |
| 275 | return "", fmt.Errorf("restore request failed: %w", err) |
| 276 | } |
| 277 | |
| 278 | if errMsg, ok := result["error"].(string); ok && errMsg != "" { |
| 279 | return "", fmt.Errorf("restore failed: %s", errMsg) |
| 280 | } |
| 281 | |
| 282 | // Get UPID |
| 283 | if upid, ok := result["data"].(string); ok && strings.HasPrefix(upid, "UPID:") { |
| 284 | return upid, nil |
| 285 | } |
| 286 | |
| 287 | return "", fmt.Errorf("failed to get restore task ID") |
| 288 | } |