Wednesday, November 20, 2013

PowerShell script - Back up all site collections of a web application

Below is the PowerShell script to back up all site collections of a web application. We can schedule it from windows task scheduler, to run it from Monday to Friday.

I feel it's handly, because, only the latest 5 copies will be kept, which save the housekeeping work.

================

Add-PSSnapin Microsoft.SharePoint.PowerShell –ErrorAction SilentlyContinue 

#For .net 4.5, we can compress backup files through build-in components.
#Add-Type -As System.IO.Compression.FileSystem
#or else, we can do it through 7-zip or winrar

# The compression level (defaults to Optimal):
#   Optimal - The compression operation should be optimally compressed, even if the operation takes a longer time to complete.
#   Fastest - The compression operation should complete as quickly as possible, even if the resulting file is not optimally compressed.
#   NoCompression - No compression should be performed on the file.
[System.IO.Compression.CompressionLevel]$Compression = "Optimal"
    
$FileNameTime=(get-date).DayOfWeek 

$FolderPath = "E:\EricFang\Scripts\Backup\" 

function BackupWebApplication([string]$webApplicationURL) 

    $SPWebApplication = Get-SPWebApplication $webApplicationURL 
$WebApplicationName = $webApplicationURL.Replace("https://","") 
$WebApplicationName = $WebApplicationName.Replace("http://","") 
$WebApplicationName = $WebApplicationName.Replace(".","-") 

    foreach ($Site in $SPWebApplication.Sites) 
    { 
        if ($Site.ServerRelativeUrl -notmatch "Office_Viewing_Service_Cache") 
        { 
            $SitePath = $Site.ServerRelativeUrl 
            if ($SitePath -eq "/") 
            { 
                $SitePath = "" 
            } 
            else 
            { 
                $SitePath = $Site.ServerRelativeUrl.Replace("/",".") 
            } 
             
            $Filename = $FolderPath + $WebApplicationName + $SitePath + "." + $FileNameTime + ".dat" 
            Write-Host "Back up site " $Site.Url -nonewline 
            backup-spsite -identity $Site.URL -path $FileName -Force 
            Write-Host "......completed." 
            
#WinRAR, shareware
            #&$WinRar a "$FileName.rar" $FileName -df -r -m1 -ilog
            
#.Net 4.5           
            #$Archive = [System.IO.Compression.ZipFile]::Open( "$FileName.zip", "Update" )
            #$relative = (Resolve-Path $FileName -Relative).TrimStart(".\")
            #$null = [System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($Archive, $FileName, $relative, $Compression)
            #$Archive.Dispose()
            
#### http://mats.gardstad.se/matscodemix/2009/02/05/calling-7-zip-from-powershell/  
#7-zip, freeware
            #if (-not (test-path "$env:ProgramFiles\7-Zip\7z.exe")) {throw "$env:ProgramFiles\7-Zip\7z.exe needed"} 
            #set-alias sz "$env:ProgramFiles\7-Zip\7z.exe"           
            #sz a -t7z "$FileName.7z" "$FileName"
            
            #Move-Item "$FileName" "\\AnotherServer\DailyBackup" -force
            #if (Test-Path "$FileName")
            #{
            #    Remove-Item "$FileName" -force
            #}
        } 
    } 


BackupWebApplication http://webapp1.domain.local 

#Write-Host "Finished! Press enter key to exit." -ForegroundColor Green 
#Read-Host

No comments:

Post a Comment