PowerShell Backup and Restore Options
Backups can now be
completely controlled via a series of cmdlets that perform backups of
the SharePoint farm, site collection, and configuration database.
Every cmdlet comes with a restore
command. SharePoint 2010 also introduced commandlets that provide the
ability to export and import site collections, web applications, sites,
or lists.
Automating Site Collection Backup and Restore
To perform a backup of an individual site collection, type the following:
Backup-SPSite "http://portal.companyABC.com" –path "C:\Backups\portal.dat" -force
The command in the
preceding example backs up a site collection at the given url to the
designated path. Before running this cmdlet, make sure this path (for
example, C:\Backup) exists. In case there is a previous backup file at this location, use the –force parameter to overwrite an existing backup file.
To restore the site collection, type the following:
Restore-SPSite "http://portal.companyABC.com" –path "C:\Backups\portal.dat" –force –confirm:$false
There are two parameters that must be used for silent site collection restore. -force ensures any existing site collection at http://portal.comapanyabc.com will be overwritten and -confirm:$false suppresses the overwrite confirmation dialog. By suppressing the confirmation, you can easily automate a restore operation.
Exporting SharePoint Sites and Content
In
SharePoint 2010, Microsoft introduced a new ability to export and
import SharePoint objects. These new operations enable administrators to
more easily move content between different site collections. During
import and export operations, administrators can control which content
will be transferred and how versions will be affected by the move.
Here is a sample for exporting a site:
Export-SPWeb "http://portal.companyABC.com/Team-Site" –path "C:\Exports\Team-Site.dat"
The syntax to export a list is similar, but the list path, relative to site path, must be passed as -ItemUrl parameter:
Export-SPWeb "http://portal.companyABC.com/Team-Site" –path "C:\Exports\Doc-Lib.dat" –ItemUrl "/Doc-Lib"
When exporting lists and
libraries, associated workflows and alerts will not be exported along
with the content. In addition, item-level permissions will not be
maintained.
Sometimes a business might
ask for a snapshot of SharePoint for archiving or compliance purposes,
and now PowerShell comes to the rescue. The cmdlet Export-SPWeb enables administrators to control versions that will be exported to an export file.
To export content of a document library, use the same code as previously given. By using optional parameter –IncludeVersions, you can choose to export: LastMajor, CurrentVersion, LastMajorAndMinor, or All.
Importing Exported Content
Export operations can be
performed via central administration but import is only available via
PowerShell. Before importing, you need to create an empty site in your
site collection. A new site can be created via UI or PowerShell, but
make sure you use the same site template as the site that was exported.
The following command lists all installed site templates:
To create a site with a
desired template, use the following cmdlet (in this case, a site based
on a blank site template will be created):
New-SPWeb -url "http://portal.companyABC.com/New-Site" -Template "STS#1"
To import a document library, type the following:
Get-SPSite "http://portal.companyABC.com/New-Site" | Import-SPWeb –path "C:\Exports\Doc-Lib.dat" –force –UpdateVersions Overwrite
The preceding example imports a document library that was previously exported to a file. –Force parameter ensures that existing data will be overwritten, and –UpdateVersions instructs command to overwrite all existing versions. There is also an append switch (versions will be appended to existing versions).
Monitoring SharePoint Databases and Site Collection Usage
To monitor SharePoint databases, first use the following commandlet to retrieve the list of databases:
Get-SPWebApplication | Get-SPContentDatabase | Select WebApplication, Name, Server, | Format-Table
The preceding command
displays all SharePoint web applications available in this farm, along
with their respective database names and SQL server name.
To check the current size (in bytes) of each site collection, use the following cmdlet:
Get-SPSite | Select RootWeb, Url,
@{Name="Size"; Expression={"{0:N2} GB" -f ($_.Usage.Storage/1E9)}},
@{Name="Storage Warning"; Expression={"{0:N2} GB" -f ($_.Quota.StorageWarningLevel/1E9)}},
@{Name ="Storage Max"; Expression={"{0:N2} GB" -f ($_.Quota.StorageMaximumLevel/1E9)}} | ConvertTo-HTML | Out-File "C:\Temp\SiteUsage.html"
The sample might look
complicated but actually gets all the site collections and formats the
output to make it easy to read. Here are a few notes:
SPSite.Usage property is a complex object, and to retrieve storage usage we had to construct an expression.
A similar expression was used to retrieve quota properties.
An expression also demonstrates PowerShell’s capability to convert results to HTML and save it to a file (ConvertTo-HTML | Out – File).
Figure 2 shows sample results that could be retrieved by running such a command in CompanyABC’s farm.
Set-SPSite also
gives you the ability to change quotas for a site collection via
PowerShell. To change a quota for a site collection, type this ("My Quota Template" uses the name of an existing quota template available in your farm. A new template can be created via central administration):
Set-SPSite "http://portal.companyABC.com" –QuotaTemplate "My Quota Template"
Managing Content Databases and Site Collections
The
built-in cmdlets enable you to manage content databases. If a
SharePoint farm is not adequately planned, one of your content databases
might grow too large. In such scenarios, you can rely on PowerShell to
fix the problem. Type the following command to create a new database:
#$webApplication = Get-SPWebApplication "http://portal.companyABC.com"
#New-SPContentDatabase -Name "WSS_Portal_Content_New" -WebApplication $webApplication
The preceding command will
create a new content database for the portal web application. All the
other database settings (for example, database server, warning site
count, and maximum site count) can be changed at a later stage with the Set-SPContentDatabase cmdlet.
The following example
demonstrates how warning and maximum site levels can be changed via
PowerShell. Type the following to change the warning and maximum site
count to 1000 and 2000, respectively:
Set-SPContentDatabases "WSS_Portal_Content_New" –WarningSiteCount 1000 –MaxSite Count 2000
With multiple databases
per web application, you can now easily move site collections between
databases. Use the following code (make sure you already have "WSS_Portal_Content_New" in place):
Move-SPSite "http://portal.companyABC.com/sites/sub-site-collection" –
DestinationDatabase "WSS_Portal_Content_New" –confirm:$false
The preceding command moves the site collection at the given URL (“http://portal.companyABC.com/sites/sub-site-collection”) to the new database. If you omit the -confirm
parameter, you will be prompted to confirm this operation. As noted in
the warning, IIS must be restarted to complete the move. This can be
accomplished by using the iisreset command.
Analyzing Events in the Event Log
SharePoint 2010 introduced a
number of improvements for easier logging and monitoring for SharePoint
farms. One of the crucial improvements is the correlation ID displayed
to end users in the browser when an error occurs.
PowerShell comes to the rescue when troubleshooting problems with SharePoint. The most important cmdlet for reviewing events is Get-SPLogEvent.
Running this cmdlet without additional parameters might take a while,
so it is advisable to add some additional parameters before running this
cmdlet. Here are some examples:
# Displays all events that occurred between January 1st and 2nd 2010.
Get-SPLogEvent -StartTime "01/01/2010 00:00" -EndTime "01/02/2010 23:59:59"
# Finds event that occurred between January 1st and 2nd 2010 with the CorrelationID "20ce1e39-5027-4db3-87c2-e0fa17154365"
Get-SPLogEvent -StartTime "01/01/2010 00:00" -EndTime "01/02/2010 23:59:59" | Where {$_.Correlation -eq "20ce1e39-5027-4db3-87c2-e0fa17154365"}
Beside the Get cmdlet, there are also Clear, Merge, and New cmdlets that enable additional operations over logs.
With PowerShell, administrators can change logging level for different event categories:
# Displays logging level for each logging category
Get-SPLogLevel
# Displays logging level for each Access Services categories in a GridView
Get-SPLogLevel | Where {$_.Area –like "*Access Service*"} | Out-GridView
# Changes the least error logging level for "Access Services" to "Error" level
Set-SPLogLevel -Identity "Access Services:*" -EventSeverity Error