Share via


SPBackupRestoreConsole.DiskSizeRequired method (Guid, SPBackupRestoreObject)

Gets the amount of disk space needed to backup the specified node (and its children) in the tree of components in the specified backup operation.

Namespace:  Microsoft.SharePoint.Administration.Backup
Assembly:  Microsoft.SharePoint (in Microsoft.SharePoint.dll)

Syntax

'Declaration
Public Shared Function DiskSizeRequired ( _
    id As Guid, _
    node As SPBackupRestoreObject _
) As ULong
'Usage
Dim id As Guid
Dim node As SPBackupRestoreObject
Dim returnValue As ULong

returnValue = SPBackupRestoreConsole.DiskSizeRequired(id, _
    node)
public static ulong DiskSizeRequired(
    Guid id,
    SPBackupRestoreObject node
)

Parameters

Return value

Type: System.UInt64
A UInt64 that represents the space, in bytes, needed for the backup operation.

Remarks

The DiskSizeRequired method sums the DiskSizeRequired values of all the objects included in the backup operation. It also adds 1K bytes to ensure that it always returns a minimum of 1K.

If MaxValue() is returned, then id does not contain a valid backup object.

Examples

The following example shows the DiskSizeRequired method being used to determine if the backup volume has enough space to hold the components to be backed up. For the full example and a detailed discussion of it, see How to: Programmatically Back Up Content.

private static Boolean EnsureEnoughDiskSpace(String location, Guid backup, SPBackupRestoreObject node)
{
    UInt64 backupSize = SPBackupRestoreConsole.DiskSizeRequired(backup, node);
    UInt64 diskFreeSize = 0;
    UInt64 diskSize = 0;
    Boolean hasEnoughSpace = true;

    try
    {
        SPBackupRestoreConsole.DiskSize(location, out diskFreeSize, out diskSize);
    }
    catch
    {
        diskFreeSize = diskSize = UInt64.MaxValue;
    }

    if (backupSize > diskFreeSize)
    {
        // Report through your UI that there is not enough disk space.
        Console.WriteLine("{0} bytes of space is needed but the disk hosting {1} has only {2}.", backupSize, location, diskFreeSize);
        Console.WriteLine("Please try again with a different backup location or a smaller component.");
        hasEnoughSpace = false;
    }
    else if (backupSize == UInt64.MaxValue || diskFreeSize == 0)
    {
        // Report through your UI that it cannot be determined whether there is enough disk space.
        Console.WriteLine("Cannot determine if that location has enough disk space.");
        Console.WriteLine("Please try again with a different backup location or a smaller component.");
        hasEnoughSpace = false;
    }
    return hasEnoughSpace;

}// end EnsureEnoughDiskSpace
Private Shared Function EnsureEnoughDiskSpace(ByVal location As String, ByVal backup As Guid, ByVal node As SPBackupRestoreObject) As Boolean
    Dim backupSize As UInt64 = SPBackupRestoreConsole.DiskSizeRequired(backup, node)
    Dim diskFreeSize As UInt64 = 0
    Dim diskSize As UInt64 = 0
    Dim hasEnoughSpace As Boolean = True

    Try
        SPBackupRestoreConsole.DiskSize(location, diskFreeSize, diskSize)
    Catch
        diskSize = UInt64.MaxValue
        diskFreeSize = diskSize
    End Try

    If backupSize > diskFreeSize Then
        ' Report through your UI that there is not enough disk space.
        Console.WriteLine("{0} bytes of space is needed but the disk hosting {1} has only {2}.", backupSize, location, diskFreeSize)
        Console.WriteLine("Please try again with a different backup location or a smaller component.")
        hasEnoughSpace = False
    ElseIf backupSize = UInt64.MaxValue OrElse diskFreeSize = 0 Then
        ' Report through your UI that it cannot be determined whether there is enough disk space.
        Console.WriteLine("Cannot determine if that location has enough disk space.")
        Console.WriteLine("Please try again with a different backup location or a smaller component.")
        hasEnoughSpace = False
    End If
    Return hasEnoughSpace

End Function ' end EnsureEnoughDiskSpace

See also

Reference

SPBackupRestoreConsole class

SPBackupRestoreConsole members

DiskSizeRequired overload

Microsoft.SharePoint.Administration.Backup namespace