Showing posts with label automating backup. Show all posts
Showing posts with label automating backup. Show all posts

Saturday, April 14, 2012

Automating SharePoint site backup using PowerShell and windows Task Scheduler (Part 2)

Task Scheduler
In the Part 1 we created the script which will take the backup of a site collection and will store it in new folder based on the current date. This script has been saved as .ps1 (PowerShell script) file. Now we want this script to run every day at 10:00 PM when I am taking rest at home. This automation can be achieved easily by windows task scheduler.



How to use windows scheduler to automate the Backup on a daily basis

a) First load the Task Scheduler from Start >> All Programs >> Accessories >> System    Tool>> Task Scheduler. I am using the version which ships with windows server 2008 R2 edition, but the   concept should be same for other release.

b) To create a new Task, click Create Task from the right hand side “Actions Panel”.


c) This will open a Create Task window which will have first tab as General; here we need to provide the following basic information.

    General Tab

  1. Name of the Task like “SharePoint Backup”. A Description which is not mandatory
  2. Select the User Account under which the following task will run. Ensure that the user account has the necessary permissions to use the PowerShell on the SharePoint farm, permission to perform script action on the SharePoint.
  3. Select Run whether the user is logged in or not and tick the Do not store password checkbox. This is necessary because at the time of backup being taken you may not be logged in.
  4. Also enable the Run with highest privileges option. I would recommend testing the script from Task Scheduler with the option disabled and only enable if the script does not work without it.

d)  In the “Triggers Tab
  1. Click New and in the “New Trigger”
  2. In “Begin the Task” dropdown select “On a Schedule”
  3. Under Setting radio button choose “Daily” also choose the time at 10:00 PM (according to your wish).
  4. Don’t change advanced settings for now.



e)  In the “Actions Tab
  1. Click “New” in the Actions Tab.
  2. In the “New Action” window ensure the “action” is set to Start a program.
  3. Under settings Program/Script write “Powershell.exe”
  4. In Add Arguments (optional) type &'d:\Backup-SiteCollections.ps1'. Note: Wrapping the file path and name in single quotation marks allows you to specify spaces in the text.



You should now see your script in the Task Scheduler Library (if not, click Refresh in the right-hand panel). To test the script, highlight it in the console and click Run from the right-hand panel.


Automating SharePoint site backup using PowerShell and windows Task Scheduler (Part 1)

Powershell Backup

SharePoint administrators need to run regular backups using PowerShell, the STSADM tool or in Central Administration. But taking these backups on a daily basis can be a tedious process, hence either we can sit back and take backup, waiting for it to get over or we can go home and sleep on the couch, while the PowerShell and Task Scheduler take cares of the rest.

So in this series we will see
  1. How to write a PowerShell script to take Backup. (Part 1)
  2. How to use windows scheduler to automate the Backup on a daily basis. (Part 2)
Creating PowerShell script to take Backup

I have included the whole script first which i have then explained below in phases
Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue
try
 {
    $today = (Get-Date -Format dd-MM-yyyy)
    $backupDirectory = "D:\Backup\DailySiteCollectionBackUp\$today"
  # Backup file Location
    $backupFile = "D:\Backup\DailySiteCollectionBackUp\$today\Backup.dat"
  # Log file location
    $logFile = "$backupDirectory\BackupLog.log"    
  # Address of the Site Collection to backup
    $Site = "http://a4md03082:10000/"
  
 # Location of the Backup Folder
    if (-not (Test-Path $backupDirectory)) 
    { 
      [IO.Directory]::CreateDirectory($backupDirectory)
      #New-Item $logPath -type $backupDirectory
    } 
 
 # Get backup start date and time 
    $backupStart = Get-Date -format "MM-dd-yyyy HH.mm.ss"
  
  # creates a log file  Start-Transcript -Path 
    Start-Transcript -Path $logFile
    
 # This will actually initiate the backup process. 
      Write-Host    
      Write-Host    
      Write-Host "Backup starting at $backupStart for $Site "    
      Write-Host "******************************************"
     Backup-SPSite -Identity $Site -Path $backupFile -Force
     $backupComplete = Get-Date -format "MM-dd-yyyy HH.mm.ss"
      Write-Host    
      Write-Host    
      Write-Host "Backup Completed at $backupComplete for $Site "    
      Write-Host "******************************************"
 
 Stop-Transcript 
 }
Catch
 {
  $ErrorMessage = $_.Exception.Message
  write "$today BackUp Failed   $ErrorMessage  ">>$logFile

 }

a) It will load the SharePoint snap IN
Including Powershell SnapIn
Including PowerShell SnapIN
b) It will create a folder with the current Date if not present.

Creating BackUp Folder
Note: Do change the path according to the place where you want to store the backup.

c) Then we will run the “Start Transcript/Stop-Transcript” block command. This will log all command that the user types and all output that appears on the console. Here we will require to pass the log file name. Between the “Start Transcript/Stop-Transcript” we will take the backup with the Backup-SPSite which will take the backup of the specified site

Create Backup Folder

Note: Start Transcript/Stop-Transcript command does not runs on the PowerShell ISE, this will run only on the console, which is the way we are going to do while running it from windows task scheduler.

d) Now after the whole script is created save it with a .ps1 extension.



Now in the Part 2 we will understand on how to run this script at a particular time of the day automatically.