【问题标题】:How to Backup IIS website folder physical Path before deploying using using Nant or PowerShell in Teamcity如何在 Teamcity 中使用 Nant 或 PowerShell 在部署之前备份 IIS 网站文件夹物理路径
【发布时间】:2017-02-14 08:18:48
【问题描述】:

我正在使用 TeamCity 构建自动化流程。我的计划是在部署到 prod 服务器之前创建一个备份文件夹:

  1. 停止 IIS 并复制或备份 IIS 网站物理路径文件夹 VersionNo 为 X(使用 Nant 或 Powershell)
  2. 将 IIS 网站物理路径文件夹重命名为 VersionNo 为 X + 1(使用 Nant 或 Powershell) 3.部署到IIS网站物理路径文件夹X+1
  3. 重启 IIS

如何在 Nant 中使用 Nant 或 PowerShell 脚本来完成此任务?任何帮助将是一个很大的帮助。谢谢

【问题讨论】:

  • 为什么要重命名 IIS 网站文件夹?您可以停止相应的应用程序池或网站,将感兴趣的文件夹备份到不同的时间戳文件夹,部署新代码并重新启动 IIS..
  • 感谢 Baskar Lingam Ramchandran。我是新手,在部署之前备份文件夹而不重命名的最佳方法是什么。
  • 我不确定您如何在 TeamCity 中进行部署。但是我可以提供 Powershell 脚本来停止 IIS 中的应用程序池、备份和启动应用程序池。备份完成后,脚本将提示您查看部署是否完成.. 如果您提供 true 或 yes 或 0,它将继续并启动应用程序池..

标签: powershell nant team-city-10.0


【解决方案1】:

就 Powershell 脚本而言,此脚本将有助于 IIS 步骤和备份。

Import-Module WebAdministration

function Get-SiteName
{    
    [cmdletBinding()]
    param(
        [Parameter(Mandatory)]
        $site)
    if(Test-Path IIS:\Sites\$site)
    {
        Write-Host "The provided website name is $site and it is a valid website`r`n" -ForegroundColor Cyan
    }
    else
    {
        Write-Host "There is not a website present in the name provided`r`n" -ForegroundColor Red
        Exit
    }
}

function Stop-AppPool
{
    # Get the app pool of the website
    $appPool = Get-Item IIS:\Sites\$site | Select-Object applicationPool

    # Get the app pool name
    $appPoolName = $appPool.applicationPool

    # Stop the app pool. If already stopped then a message will be shown
    if ((Get-WebAppPoolState $appPoolName).Value -ne "Stopped")
    {
        Stop-WebAppPool $appPoolName
        Write-Host "App pool $appPoolName is $((Get-WebAppPoolState $appPoolName).Value)"
    }
    else
    {
        Write-Host "App pool $appPoolName is already stopped`r`nNo need to stop it again`r`n" -ForegroundColor DarkGreen
    }
}

function Test-IsStoppedOrNot
{
    # Check the status of the app pool before taking backup
    sleep -Seconds 30
    if ((Get-WebAppPoolState $appPoolName).Value -eq "Stopped")
    {
        Write-Host "App pool $appPoolName is in stopped state`r`nBackup and Deploy can start`r`n" -ForegroundColor Cyan
    }
    else
    {
        Write-Host "App pool is not yet stopped`r`nTry after sometime`r`n" -ForegroundColor Red
        Exit
    }
}

function Make-BackupFolder
{
    # Get the website physical folder of the website
    $SiteFolder = Get-Item IIS:\Sites\$site
    $SiteFolderName = $SiteFolder.physicalPath
    Write-Host "The physical folder of the website is $SiteFolderName" -ForegroundColor DarkCyan

    # Forming backup folder name

    $backupFolder = $site + "_" + (Get-Date).ToString('MM.dd.yyyy.hh.mm.ss')

    # Creating backup folder path

    $DriveName = Split-Path -Path $SiteFolderName -Qualifier

    $backupFolderPath = Join-Path "$DriveName" -ChildPath "Backup" | Join-Path -ChildPath "$backupFolder"

    # Creating backup folder

    mkdir $backupFolderPath
}

function Test-IsBackupFolderCreatedOrNot
{
    # Confirm if the backup folder is created

    if (Test-Path $backupFolderPath)
    {
        Write-Host "Backup folder $backupFolderPath is created`r`n" -ForegroundColor DarkGreen
        Get-ChildItem $SiteFolderName | Copy-Item -Destination $backupFolderPath
        Write-Host "Backup is complete`r`n" -ForegroundColor Blue
        Write-Host "Perform the deployment`r`n" -ForegroundColor Cyan
    }
    else
    {
        Write-Host "Backup folder is not created`r`nTake a look please" -ForegroundColor Red
        Exit
    }
}

# Start the app pool. If already started then a message will be shown
function Start-AppPool
{
    if ((Get-WebAppPoolState $appPoolName).Value -ne "Started")
    {
        Start-WebAppPool $appPoolName
        Write-Host "App pool $appPoolName is $((Get-WebAppPoolState $appPoolName).Value)"
    }
    else
    {
        Write-Host "App pool $appPoolName is already started`r`nNo need to start it again`r`n" -ForegroundColor DarkGreen
        Write-Host "THE DEPLOYMENT IS COMPLETE`r`n" -ForegroundColor DarkBlue        
    }
}

function Test-IsStartedOrNot
{
    # Check the status of the app pool once again
    sleep -Seconds 30
    if ((Get-WebAppPoolState $appPoolName).Value -eq "Started")
    {
        Write-Host "App pool $appPoolName is not in started state yet`r`nTAKE A LOOK" -ForegroundColor Cyan
    }
    else
    {
        Write-Host "App pool is in started state`r`n" -ForegroundColor Red
        Write-Host "THE DEPLOYMENT IS COMPLETE`r`n" -ForegroundColor DarkBlue
    }
}

function Confirm-Deployment
{
    [cmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [ValidateSet('true','false','yes','no','1','0')]
        $CanProceed)

    if (($CanProceed -eq 'true') -or ($CanProceed -eq 'yes') -or ($CanProceed -eq '0'))
    {
        Start-AppPool
    }
}

Get-SiteName

Stop-AppPool

Test-IsStoppedOrNot

Make-BackupFolder

Confirm-Deployment

Test-IsStartedOrNot

脚本执行以下操作:

  1. 从用户那里获取站点名称
  2. 如果站点名称不是有效的网站名称,脚本将终止
  3. 根据提供的站点名称,将确定站点的应用程序池和物理文件夹。
  4. 应用程序池已停止
  5. 等待 30 秒,然后检查应用程序池是否已停止
  6. 将使用物理文件夹路径导出备份文件夹路径。例如,如果网站路径是 D:\Sites\MySite,那么备份文件夹将位于 D:\Backup。备份文件夹名称为 SiteName+_+Date。日期的格式为month.day.year.hour.min.seconds。
  7. 执行备份
  8. 备份后,您需要按照自己的方式进行部署。脚本不进行部署。
  9. 脚本会提示您确认已执行部署。
  10. 如果您通过输入 true 或 yes 或 0 确认部署已完成,则脚本将启动应用程序池。
  11. 等待 30 秒,然后检查应用程序池是否已启动。如果不是,它会让您知道。

请再次注意,脚本不会处理您的部署。您需要进行部署。在您执行部署之前,您可以保持 POWERSHELL 会话打开。

此外,该脚本不会删除网站文件夹的内容。请根据需要包含该步骤。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 2013-06-08
    • 1970-01-01
    • 2019-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-25
    • 1970-01-01
    相关资源
    最近更新 更多