Friday 2 November 2012

Backup of SharePoint site with PowerShell

Hi All,

Today I will blog about important but simple topic of SharePoint project execution, i.e. Backup of SharePoint sites.

Environment: SharePoint 2010

Though there are many options and fine level of granularity in SharePoint 2010 central admin for taking the backup of sites, clearly one thing is missing i.e. recurring backups or scheduling backups which is crucial in SharePoint project execution.

Today I will provide step by step process to schedule backups with powerful powershell scripting.

1. Create a script that takes the backup of the site and stores in to a shared location.
2. After the backup is taken and stored in the shared location, it is better to delete the old backups as we can't afford infinite memory.
3. Create a batch file and call this powershell script and with any parameters.
4. Use windows scheduler to schedule this batch job.

And now the interesting code part ...


1. Typically the powershell script can be like this



function BackupNCopybak($urlsource,$bkpsrc,$bakname)
{ 
 #creating a folder at the backup location with the name of today in yyyymmdd format so that it is easy to sort the folders
 $bkpsrctoday=$bkpsrc+"\"+$today
 
 # if path exists then use it, else create a folder
 if(!(Test-Path $bkpsrctoday))
 {
     #write-host "creating directory with today's name"
     New-Item $bkpsrctoday -type directory -force
 }
 
 # appending the path and the backup name which is required while takeing the backup of the site collection
    $backupfilepathsrc=$bkpsrctoday+"\"+$bakname
 
    write-host "adding sharepoint snapin"
    add-pssnapin microsoft.sharepoint.powershell -erroraction "SilentlyContinue"
 
    write-host "back up started"
    backup-spsite -identity $urlsource -path $backupfilepathsrc -force
 
 #to delete the old backups, first step is to find the folder name that has the old backup.
 # this script stores the backup for past 8 days and delete the old ones, if you want more backups increase the number
 $last8thday=[System.DateTime]::Today.AddDays(-8).ToString("yyyyMMdd")
 $last8thdaypath=$bkpsrc+"\"+$last8thday
 
 # check if the old backup exists, if exists delete else just move on
 if((Test-Path $last8thdaypath))
 {
  Remove-Item -Path $last8thdaypath -Recurse -Force
 }
}
$today=[System.DateTime]::Today.ToString("yyyyMMdd")
$urlsource=$args[0]
$bkpsrc=$args[1]
$bakname=$args[2]
$bkpdestshare=$args[3]
BackupNCopybak $urlsource $bkpsrc $bakname $bkpdestshare
#BackupNCopybak "http://server:portnumber" "E:\Backups\AutomatedBackups" "SiteCollectionTitle.bak"


2. And the batch file will be 





powershell -command "<location of your powershell script.ps1> <url> <backuplocaton> <backupname.bak>" 


Example  powershell -command "E:\Kesava\Scripts\BackupScripts\SiteBackup.ps1 http://xxxx:2000 E:\Backups\AutomatedBackups xxx.bak"

3. Open Task shceduler in win server 
       3.1 Create a new task
       3.2 Give the name and description
3.3 Select the option "Run whether user selected or not"
3.4 Select the option "Run with highest privileges"
3.5 In triggers tab, click new and create a schedule as per your needs
3.6 In Actions tab, choose the batch (.bat) file location, so that task scheduler will run this once the trigger (schedule) is triggered
3.7 Change any settings in settings tab to your needs and click ok

if's

While doing this, if you see that the task scheduler is not opening then you can follow these troubleshooting tips
1. Check for the service Task Scheduler, if it is stopped then start it.
2. If it is greyed out, then check for the dependency services like windows log service, rpc service and others.
3. Even they are started, then you have to change the registry key HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\Schedule start dword from 4 to 2

Wednesday 31 October 2012

SharePoint publishing content tagging for search

Hi All, 

 I am quite happy to write to my first blog on my most recent success in SharePoint!!! 

Generally you might have come across a requirement where the clients require some kind of tagging for the page content so that its easy for users to search for that content in the site. 

Today I will guide you through the step by step process for doing exactly the same...... 

1. Enabling tagging for the page content so that the content comes on top in search results
2. Enabling clients to tag the page content from the page itself

 Environment: SharePoint 2010, publishing site, site collection already developed

1. There is very cool feature called Enterprise keywords available right out of the box 
2. Go to the root site, site content type gallery
3. Modify the "Page" content type under Publishing Content types.


Page Content type in root site content type gallery
4. Click on Add from existing site columns
5. Add "Enterprise Keywords" existing site column to Page content type.
6. Select Yes for Update all content types inheriting from this type.
7. Click OK.
8. This will add this Enterprise keywords to Page content type and to all other content types that inherit from this Page content type.
9. From this half job is done...

You can test the work so far we did by going to a page in pages library, edit the properties and enter a word in Enterprise Keywords field and check in the page. Do a full crawl of the site and then search with the word you entered in Enterprise Keyword field, now boom... you should get that page at the top of the search result.

10. Now to enable clients to enter/update the tags/keywords on the site, checkout the page layout in SharePoint designer (SPD) 2010.
11. At the end of the register tags, add the following line to register a dll.

<%@ Register Tagprefix="Taxonomy" Namespace="Microsoft.SharePoint.Taxonomy" Assembly="Microsoft.SharePoint.Taxonomy, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

12. And at any convinient location in the page layout, where you want the clients to add/update the tags/keywords add the following 

<Taxonomy:TaxonomyFieldControl FieldName="TaxKeyword" runat="server"></Taxonomy:TaxonomyFieldControl>

13. Check in the page layout and publish..
14. And boom.. now clients can tag the content with keywords from the site itself and can search for them easily....


If's

1. If the search result don't have the expected enterprise keyword's page on top, then it might be the problem with the crawling.
2. Restart the full crawl and see if this fixes the issue
3. If that still didnt, go to crawl history and enter the url of the page (where you entered the enterprise keyword and expecting this to be on top of the search result), and in the links that search crawled, select them and choose to retire those. So that sharepoint will crawl these pages again and now for sure you will have your pages on top of the search result...


Hope this helps at least a few in need...

And if you have any questions leave your comments so that I can reply...