Public/Restore-Files.ps1

function Restore-Files
{
    ##############################################################################
    #.SYNOPSIS
    # Restores files (Used with Backup-Files).
    #
    #.DESCRIPTION
    # Use this function to maintain backups restore using Robocopy if deployment has failed.
    # Detailed information about the Robocopy exit codes is provided in the end of the operaton.
    # Default log file (BACKUP_DIR\_SitecoreDeploymentPS\RestoreFilesLog.txt).
    # Reverts all changed files and deletes the newly created ones, except for excluded directories (if any).
    # Used with Backup-Files.
    #
    #.PARAMETER SourceDir
    # Restore soucrce directory (absolute path).
    # Root directory of backups.
    #
    #.PARAMETER TargetDir
    # Restore target directory (absolute path).
    # Usually website root.
    #
    #.PARAMETER LogFile
    # [OPTIONAL] Log file (absolute path).
    # [DEFAULT_VALUE] $SourceDir\yyyy-MM-dd_HH-mm\_SitecoreDeploymentPS\RestoreFilesLog.txt
    #
    #.EXAMPLE
    # $sourceDir = "C:\backup"
    # $targetDir = "C:\inetpub\wwwroot\website"
    # $logFile = "C:\logs\RestoreLog.txt"
    # Restore-Files -SourceDir $sourceDir
    # -TargetDir $targetDir
    # -LogFile $logFile (optional)
    ##############################################################################

    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true)]
        [ValidateScript({
            if (Test-Path $_) { $true }
            else { Throw "The $_ path is not valid. Please, provide a valid value." }})]
        [string]$SourceDir,

        [Parameter(Mandatory=$true)]
        [ValidateScript({
            if (Test-Path $_) { $true }
            else { Throw "The $_ path is not valid. Please, provide a valid value." }})]
        [string]$TargetDir,

        [string]$LogFile
    )

    Write-Host "Restore started."

    $lastBackup = Get-LastCreatedDir -Directory $SourceDir

    if([String]::IsNullOrWhiteSpace($lastBackup))
    {
        Throw "No backup found."
    }

    # Only backups which cotain log directory (created from Sitecore.Powershell) are considered valid for restore.
    $logDir = Get-LogDir -Directory $lastBackup
    if([String]::IsNullOrWhiteSpace($logDir))
    {
        Throw "The last created folder in backups root folder is not a valid backup. If you have created some backup manually, please restore it manually."
    }

    if ([String]::IsNullOrWhiteSpace($LogFile))
    {
        $LogFile = "$logDir\RestoreFilesLog.txt"
    }
    else
    {
        if(!(Test-Path $LogFile))
        {
            Throw "The '$LogFile' log path is not valid. Please, provide a valid value."
        }
    }

    Write-Host "Restore source: $lastBackup"
    Write-Host "Restore target: $TargetDir"

    $excludedDirsOption = @("/XD", $logDir)
    $excludedRelativeDirs = Get-ExcludedDirs -Directory $logDir

    ForEach($dir in $excludedRelativeDirs)
    {
        $excludedDirsOption += Join-Path -Path $lastBackup -ChildPath $dir
    }
   
    Robocopy $lastBackup $TargetDir /MIR $excludedDirsOption /LOG+:"$LogFile" /TEE
    
    $lastExitCode = $LASTEXITCODE
    Show-RobocopyResultMessage -RobocopyExitCode $lastExitCode
    Exit-Restore-Files -RobocopyExitCode $lastExitCode
}