using System;
using System.Collections.Generic;
using System.Management.Automation;
using PSProxmox.Client;
using PSProxmox.Models;
using PSProxmox.Session;
using PSProxmox.Utilities;
namespace PSProxmox.Cmdlets
{
///
/// Creates a new cluster backup in Proxmox VE.
/// The New-ProxmoxClusterBackup cmdlet creates a new cluster backup in Proxmox VE.
///
/// Create a new cluster backup
/// $backup = New-ProxmoxClusterBackup -Connection $connection -Compress
///
///
[Cmdlet(VerbsCommon.New, "ProxmoxClusterBackup")]
[OutputType(typeof(ProxmoxClusterBackup))]
public class NewProxmoxClusterBackupCmdlet : PSCmdlet
{
///
/// The connection to the Proxmox VE server.
///
[Parameter(Mandatory = true, Position = 0)]
public ProxmoxConnection Connection { get; set; }
///
/// Whether to compress the backup.
///
[Parameter(Mandatory = false)]
public SwitchParameter Compress { get; set; }
///
/// Whether to wait for the backup to complete.
///
[Parameter(Mandatory = false)]
public SwitchParameter Wait { get; set; }
///
/// The timeout in seconds to wait for the backup to complete.
///
[Parameter(Mandatory = false)]
public int Timeout { get; set; } = 300;
///
/// Processes the cmdlet.
///
protected override void ProcessRecord()
{
try
{
var client = new ProxmoxApiClient(Connection, this);
// Create the backup
var parameters = new Dictionary();
if (Compress.IsPresent)
{
parameters["compress"] = "1";
}
WriteVerbose("Creating cluster backup");
string response = client.Post("cluster/backup", parameters);
var taskData = JsonUtility.DeserializeResponse(response);
string taskId = taskData.data;
if (Wait.IsPresent && !string.IsNullOrEmpty(taskId))
{
WriteVerbose($"Waiting for backup task {taskId} to complete");
int attempts = 0;
int maxAttempts = Timeout / 5;
bool completed = false;
while (attempts < maxAttempts)
{
string taskResponse = client.Get($"nodes/{Connection.Server}/tasks/{taskId}/status");
var taskStatus = JsonUtility.DeserializeResponse(taskResponse);
string status = taskStatus.data.status;
if (status == "stopped")
{
completed = true;
break;
}
System.Threading.Thread.Sleep(5000);
attempts++;
}
if (!completed)
{
WriteWarning($"Timeout waiting for backup task {taskId} to complete");
}
else
{
WriteVerbose($"Backup task {taskId} completed successfully");
}
}
// Get the latest backup
string backupsResponse = client.Get("cluster/backup");
var backupsData = JsonUtility.DeserializeResponse(backupsResponse);
var backups = backupsData.data;
if (backups.Count > 0)
{
var latestBackup = backups[0];
var backup = new ProxmoxClusterBackup
{
BackupID = latestBackup["backup-id"],
Time = latestBackup["time"],
Type = latestBackup["type"],
Version = latestBackup["version"],
Size = latestBackup["size"],
Compression = latestBackup["compression"],
Node = latestBackup["node"],
Path = latestBackup["path"]
};
WriteObject(backup);
}
else
{
WriteWarning("No backups found after creating backup");
}
}
catch (Exception ex)
{
WriteError(new ErrorRecord(ex, "NewProxmoxClusterBackupError", ErrorCategory.OperationStopped, Connection));
}
}
}
}