Created
July 7, 2019 19:56
-
-
Save odenijs/e3495d1c888b8d24ff27a98bb1e9dd0f to your computer and use it in GitHub Desktop.
Powershell script to backup IIS sites
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.SYNOPSIS | |
Backup IIS sites to an 7zip archive and keep a number of versions | |
.DESCRIPTION | |
Create date based backups of all IIS site directories by mirroring site directories to a date folder. Create a zip file for each | |
mirrored folder with naming convention YYYY-MM-DD-sitename_xx.zip. | |
Automatically clean up older zip files setting the number of backups to keep. | |
.EXAMPLE | |
#> | |
function Backup-IISSites | |
{ | |
Import-Module IISAdministration | |
# Module variables | |
[string]$PROGRAMFILES_DIRECTORY = $Env:Programfiles | |
[string]$7ZIP_EXE = Join-Path $PROGRAMFILES_DIRECTORY "\7-Zip\7z.exe" | |
# Path check | |
If (!(Test-Path -PathType Leaf $7ZIP_EXE)) | |
{ | |
Write-Warning "Cannot find path 7zip.exe in `"$PROGRAMFILES_DIRECTORY". 7Zip is required.` | |
} | |
$sites = Get-IISSite | |
$iisSites = "IIS:\Sites\" | |
$backupRootPath = "C:\Backup\Sites\" | |
$numberOfBackups = 7 | |
$day = Get-Date -UFormat %d | |
$month = Get-Date -UFormat %m | |
$year = Get-Date -UFormat %Y | |
foreach($site in $sites) | |
{ | |
$iisWebsitePath = Get-WebFilePath $iisSites\$site | |
If ($iisWebsitePath -ne $null) | |
{ | |
# Mirror site files to date directory with robocopy | |
Write-Host "Copying site files to backup directory" | |
$backupPath = $backupRootPath+$year+"-"+$month+"-"+$day+"\"+$site.Name | |
Write-Host "Backing up $site to path: $backupPath" | |
# Run robocopy command | |
& robocopy $iisWebsitePath.FullName $backupPath /MIR /XF UmbracoTraceLog* /XD *cache* /NFL /NDL /NJH /NJS | |
# Create zip files from mirrored site files | |
Write-Host "Zipping site ""$site"" to an zip archive" | |
$zipName = $year+"-"+$month+"-"+$day+"-"+$site.Name.Replace(".", "_")+".zip" | |
$zipFilePathAndName = $backupPath.Substring(0, $backupPath.LastIndexOf("\")+1)+$zipName | |
$7zcmd = "a" | |
$zipCommand = "`"$7ZIP_EXE`" $7zcmd `"$zipFilePathAndName`" `"$backupPath`"" | |
Write-Debug $zipCommand | |
Invoke-Expression "& $zipCommand" -OutVariable output #| findstr /i /v "Compressing" | |
Write-Output $output | Receive-Output | |
# Clean up temporary backup folders for creating zip archives | |
If (Test-Path $backupPath) | |
{ | |
Write-Verbose "Removing: $backupPath" | |
Remove-Item -Path $backupPath -Recurse -Force | |
} | |
} | |
} | |
# Move all older then number of backups zip files to oldzips folder | |
$tempPathZips = $backupRootPath+"oldzips" | |
If (!(Test-Path $tempPathZips)) | |
{ | |
Write-Host "Create old zips temp folder" | |
New-Item -ItemType Directory -Force -Path $tempPathZips | |
Write-Host "Moving zips older then $numberOfBackups days" | |
& robocopy $backupRootPath $tempPathZips /E /MOVE /MINAGE:$numberOfBackups /XD $tempPathZips /LOG+:$backupRootPath\robocopy.log | |
Write-Host "Remove oldzips folder with older backups" | |
Remove-Item -Path $tempPathZips -Recurse -Force | |
} | |
} | |
function Receive-Output { | |
process { Write-Host $_ -foreground Yellow } | |
} | |
Backup-IISSites |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment