Public/Backup-WebsiteRoot.ps1

function Backup-WebsiteRoot
{
    ##############################################################################
    #.SYNOPSIS
    # Backs up website root files (used with Restore-WebsiteRoot)
    #
    #.DESCRIPTION
    # Use this function to maintain backups using Robocopy before starting the deployment
    # Detailed information about the Robocopy exit codes is provided in the end of the operaton
    # Robocopy log file (BACKUP_DIR\_SitecoreDeploymentPS\RobocopyBackupLog.txt)
    # Ability to exclude folders and files from backup
    # Used with Restore-WebsiteRoot
    #
    #.PARAMETER SourceDir
    # Website root directory (absolute path)
    #
    #.PARAMETER TargetDir
    # Backup target directory (absolute path)
    #
    #.PARAMETER ExcludedDirs
    # [OPTIONAL] Directories and files which won't be backed up (coma separated, absolute paths)
    #
    #.EXAMPLE
    # $sourceDir = "C:\inetpub\wwwroot\website"
    # $targetDir = "C:\backups"
    # $excludedDirs = "$sourceDir\_DEV, $sourceDir\_CMP, $sourceDir\upload, $sourceDir\temp, $sourceDir\sitecore, $sourceDir\sitecore modules, $sourceDir\sitecore_files"
    # Backup-WebsiteRoot -SourceDir $sourceDir -TargetDir $targetDir -ExcludedDirs $excludedDirs
    ##############################################################################

    [CmdletBinding()]
    Param
    (
        $VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference'),
        
        [string]$SourceDir,
        [string]$TargetDir,
        [string]$ExcludedDirs
    )

    try
    {
        #region Begin

        $systemLog = NewSystemLog -Name "Backup-WebsiteRoot"

        WriteLog -Path $systemLog -Message "OUTPUT: Backup files started"
        WriteLog -Path $systemLog -Message "Backup source: '$SourceDir'"
        WriteLog -Path $systemLog -Message "Backup target: '$TargetDir'"

        #endregion Begin

        #region Process

        BackupWebsiteRoot -SourceDir $SourceDir -TargetDir $TargetDir -ExcludedDirs $ExcludedDirs -LogFile $systemLog
        
        #endregion Process

        #region End

        ShowRobocopyResultMessage -LogFile $systemLog

        if(($lastExitCode -eq 1) -or ($lastExitCode -eq 3)) 
        {
            # Robocopy exit codeс 1 if default log directory is used
            # Robocopy exit codeс 3 if custom log directory is used
            WriteLog -Path $systemLog -Message "OUTPUT: Backup files finished"
            WriteLog -Path $systemLog -Message "OUTPUT: Detailed information logged into '$systemLog'"
            Exit 0
        }
        else
        {
            Throw "Unexpected 'Robocopy exit code $lastExitCode'. (Successful exit codes are 'Robocopy exit code 1' and 'Robocopy exit code 3')"
        }

        #endregion End

    }
    catch
    {
        RegisterException
    }
}