How to Backup IIS website folder physical Path before deploying using using Nant or PowerShell in Teamcity

923 views Asked by At

I am in the process of building Automated process using TeamCity.My plan is before deploying to prod Server creating a backup folder:

  1. Stop IIS and copy or Backup IIS website physical path folder VersionNo as X (using Nant or Powershell)
  2. Rename IIS website physical path folder as VersionNo as X + 1 (using Nant or Powershell) 3.Deploy to IIS website physical path folder X+1
  3. Restart IIS

How to accomplish this using Nant or PowerShell script in Nant? Any help would be a great help. Thanks

1

There are 1 answers

0
Baskar Lingam Ramachandran On

As far as the Powershell script is concerned this script will help with IIS steps and the backup.

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

The script does the below:

  1. Get the site name from the user
  2. If the site name is not a valid website name, the script will terminate
  3. From the provided site name, app pool and physical folder of the site will be determined.
  4. App pool is stopped
  5. Wait for 30 seconds and check if the app pool is stopped
  6. Using the physical folder path the backup folder path will be derived. For example, if the website path is D:\Sites\MySite, then the backup folder will be in D:\Backup. The backup folder name is SiteName+_+Date. The Date is of the format month.day.year.hour.min.seconds.
  7. Perform backup
  8. After the backup YOU NEED TO CARRY OUT THE DEPLOYMENT YOUR WAY. THE SCRIPT DOES NOT DO THE DEPLOYMENT.
  9. The script prompts you to confirm that you have carried out the deployment.
  10. If you confirm that the deployment is done by entering either true or yes or 0, then the script will start the app pool.
  11. Wait for 30 seconds and check if the app pool is started. If not it will let you know the same.

DO NOTE AGAIN THAT THE SCRIPT DOES NOT TAKE CARE OF YOUR DEPLOYMENT. YOU NEED TO DO THE DEPLOYMENT. TILL YOU CARRY OUT THE DEPLOYMENT YOU CAN KEEP THE POWERSHELL SESSION OPEN.

ALSO the script does not remove the contents of the website folder. Please include that step as needed.

Hope this helps.