############################################################################################# # #Ben Stegink #b.stegink@blue-granite.com #BlueGranite #5-14-2007 # #Overview: # This script is designed to recursive backup the root SharePoint site specified in the url # and recusively crawl through your SharePoint site backing up any subsites of your root site # to their own individual files. These files are all stored with a .site extension, compressed # into a single date/time name .zip file in a tmp_backup directory on your SharePoint server. # This .zip file is then moved to the backup path specified and stored in a folder (named after the # the root site) created in the backup location specified. After creating the .zip file and # moving it into the backup location, this script will delete all backup files on your SharePoint # server as well as any files in that location older than 7 days from the current date/time. # This prevents your backup location from getting to large and manully having to monitor/delete # files in the backup location. This script creates it's own backup log from the log files # normally created by stsadm -o backup as well as other commands issued within the script and this # log file is bundled into the .zip file. # #Usage: # SP_Subsite_Backup [url] [backup path] # [url] -> The url of the root site of all the subsites you wish to back up. # The script will backup this root site as well as every subsite beneath it. # [backup path] -> The path you wish to backup your files to. This path will be used # to store all or the backup files. The last part of the script will take all # the backup files located here and compress them into a date/time stamped zip file # located in a folder with the root site name. # #Included Files (must all be located in the same folder): # - SP_subsite_backup.ps1 # - PowerShell Comunity Extensions from http://www.codeplex.com/PowerShellCX/Release/ProjectReleases.aspx?ReleaseId=873 # This must be installed on the server running the backup script # #Features to add: # error checking to make sure http and file location are in the right order # write a log parser to pull out any important information from the backup log # ############################################################################################# #set the location of stsadm and the error for invalid parameters set-PSDebug -strict set-Variable -name STSADM_PATH -value "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\" -option constant set-Variable -name tmpBackup_Path -value "C:\tmp_backup" -option constant set-Variable -name PARAM_ERROR -value "Please provide the Backup URL and Backup Path parameters (in that order)" -option constant #initialize temp backup path if (-not (test-Path $tmpBackup_Path\$strFolder)){ New-Item $tmpBackup_Path\$strFolder -type directory } #initialize variables for passed parameters $strBackup_Url = [string] $strBackup_Path = [string] $strStart_Time = get-date -format "MM-dd-yy hhmm" $Current_Dir = get-Location #switch to SharePoint directory with admin commands cd $STSADM_PATH #recursive function to backup all subsites function backupSubSite([string]$strURL){ $objSubsites = stsadm -o enumsubwebs -url $strURL $xmlSubsites = [xml]$objSubsites $count=$xmlSubsites.Subwebs.Count if ($count -gt 1){ For ($j=0;$j -lt $count; $j++){ $urlSubsite = $xmlSubsites.Subwebs.Subweb[$j] $strFileName = $urlSubsite -Replace("http://","") $strFileName = $strFileName -Replace("/","_") $strFileName = $tmpBackup_Path + "\" + $strFileName + ".site" $strStartSite = "Initiated a job backing up site " + $urlSubsite + " to file " + $strFileName Write-Host $strStartSite add-Content $tmpBackup_Path\backup.txt $strStartSite stsadm -o export -url $urlSubsite -filename $strFileName -includeusersecurity > $tmpBackup_Path\temp.txt Get-Content $tmpBackup_Path\temp.txt | Foreach-Object {add-Content $tmpBackup_Path\backup.txt $_} backupSubsite $urlSubsite } } elseif ($count -eq 1){ $urlSubsite = $xmlSubsites.Subwebs.Subweb $strFileName = $urlSubsite -Replace("http://","") $strFileName = $strFileName -Replace("/","_") $strFileName = $tmpBackup_Path + "\" + $strFileName + ".site" $strStartSite = "Initiated a job backing up site " + $urlSubsite + " to file " + $strFileName Write-Host $strStartSite add-Content $tmpBackup_Path\backup.txt $strStartSite stsadm -o export -url $urlSubsite -filename $strFileName -includeusersecurity > $tmpBackup_Path\temp.txt Get-Content $tmpBackup_Path\temp.txt | Foreach-Object {add-Content $tmpBackup_Path\backup.txt $_} backupSubsite $urlSubsite } else{ return } } #check for the correct number of arguments If ($($args.count) -lt 2){ Write-Host $PARAM_ERROR Exit } $strBackup_Url = $args[0] $strBackup_Path = $args[1] #check for blank arguments If ($strBackup_Url -eq "" -or $tmpBackup_Path -eq ""){ Write-Host $PARAM_ERROR Exit } $objSiteFiles = Get-ChildItem -path $tmpBackup_Path -filter *.site $objLogFiles = Get-ChildItem -path $tmpBackup_Path -filter *.log Write-Host "Begin site(s) backup" # Delete all backup files currently present in the backup folder. remove-Item "$tmpBackup_Path\*.txt" remove-Item "$tmpBackup_Path\*.site" remove-Item "$tmpBackup_Path\*.log" #backup root site of the URL entered $urlSubsite = $strBackup_Url $strFileName = $urlSubsite -Replace("http://","") $strFileName = $strFileName -Replace("/","_") $strFolder=$strFileName $strFileName = $tmpBackup_Path + "\" + $strFileName + ".site" $strStartSite = "Initiated a job backing up site " + $urlSubsite + " to file " + $strFileName add-Content $tmpBackup_Path\backup.txt $strStartSite stsadm -o export -url $urlSubsite -filename $strFileName -includeusersecurity > $tmpBackup_Path\temp.txt Get-Content $tmpBackup_Path\temp.txt | Foreach-Object {add-Content $tmpBackup_Path\backup.txt $_} #call fuction to back up all subsites backupSubSite $strBackup_Url if (-not (test-Path $strBackup_Path\$strFolder)){ New-Item $strBackup_Path\$strFolder -type directory } remove-Item "$tmpBackup_Path\*.log" remove-Item "$tmpBackup_Path\temp.txt" write-Zip -path $tmpBackup_Path\*.* -OutputPath $tmpBackup_Path\$strStart_Time.zip -Level 9 Move-Item -path $tmpBackup_Path\*.zip -destination $strBackup_Path\$strFolder $objBackupFiles = get-ChildItem $strBackup_Path\$strFolder $oldFileDate = [dateTime]::Now.adddays(-7) $oldFileDate = $oldFileDate ForEach($objZipFile in $objBackupFiles){ if($objZipFile.LastWriteTime -lt $oldFileDate){ remove-Item $strBackup_Path\$strFolder\$objZipFile } } remove-Item "$tmpBackup_Path\*.txt" remove-Item "$tmpBackup_Path\*.site" Write-Host "Backup completed successfully"