Thursday 6 March 2014

Schedule Site Collection / Farm Backup in SharePoint


  1.  Create one text file and Change filename is “SP2010_Site_Backup_With_Notification.ps1” and place below code into that file
 # NAME: SP2010_Site_Backup_With_Notification.ps1
 # AUTHOR: VENKATA SIVA KRISHNA DASARI
 # COMMENT: A Powerful Script to take backup of the Site Collection SharePoint 2010 with email notification (or) Maintain log File.


 Add-PsSnapin Microsoft.SharePoint.Powershell –ErrorAction SilentlyContinue

 try
  {
     $today = (Get-Date -Format dd-MM-yyyy)
     $today1 = (Get-Date -Format ddMMMyyyy)
     $month = (Get-Date -Format MMM-yyyy)
     $prevMonth=$((Get-Date).AddMonths(-1))
     write-host $prevMonth
   
     $prevDay= $((Get-Date).AddDays(-30))
     $day=$prevDay.ToString("dd-MM-yyyy")
     $prevMonthDay=$prevMonth.ToString("MMM-yyyy")
     write-host $day
   # Check if log file exists.
     $ChkFile = "D:\Backups\Sharepoint\Scheduled\" + $day 
     write-host $ChkFile
     $FileExists = (Test-Path $ChkFile)

   # Part 1.

     If (($FileExists)) 
     {
    write-host "Backup File Exists" -foregroundcolor Green
    Remove-Item -recurse $ChkFile 
   
    write-host "Backup File Deleted" -foregroundcolor Green
     }
     ElseIf (!($FileExists))
     {
    write-host "Backup File not Exists" -foregroundcolor Green
     }
  
    
   # Check if Monthly log file exists.
     $ChkMonthLog = "D:\Backups\Sharepoint\Scheduled\TestBackupLog_" + $prevMonthDay + ".txt" 
     write-host $ChkMonthLog
     $FileExists1 = (Test-Path $ChkMonthLog)

   # Part 2.

     If (($FileExists1)) 
     {
    write-host "Month Log File Exists" -foregroundcolor Green
    Remove-Item -recurse $ChkMonthLog 
   
write-host "Month Log Deleted" -foregroundcolor Green
     }
     ElseIf (!($FileExists1))
     {
      write-host "Month Log not Exists" -foregroundcolor Green
     }



   # Location of the Backup Folder
     [IO.Directory]::CreateDirectory("D:\Backups\Sharepoint\Scheduled\$today")
   # This will actually initiate the Site Collection backup.

     Backup-SPSite –Identity http://Domain/sites/testPortal -path D:\Backups\Sharepoint\Scheduled\$today\testPortal_$today1.bak

     Add-Content -Value "Test Backup done successfully" -Path D:\Backups\Sharepoint\Scheduled\testBackupLog_$month.txt

     Add-Content -Value (Get-Date -Format dd-MMM-yyyy) -Path D:\Backups\Sharepoint\Scheduled\testBackupLog_$month.txt

     Add-Content -Value "----------------------------------------------------------" -Path D:\Backups\Sharepoint\Scheduled\testBackupLog_$month.txt
  
   # Remove-SPShellAdmin –username administrator 

   # Edit the From Address as per your environment.
   # $emailFrom = "test@test.com"
   # Edit the mail address to which the Notification should be sent.
   # $emailTo = "dvsivakrishna@gmail.com"
   # Subject for the notification email. The + “$today” part will add the date in the subject.
   # $subject = "The SharePoint Farm Backup was Successful for "+"$today"
   # Body or the notification email. The + “$today” part will add the date in the subject.
   # $body = "The SharePoint Site Backup was Successful for "+"$today"
   # IP address of your SMTP server. Make sure relay Is enabled for the SharePoint server on your SMTP server
   # $smtpServer = "1.1.1.20"
   # $smtp = new-object Net.Mail.SmtpClient($smtpServer)
   # $smtp.Send($emailFrom, $emailTo, $subject, $body)
  }
 Catch
  {
     $ErrorMessage = $_.Exception.Message
   # Configure the below parameters as per the above.
   # $emailFrom = "Admin@test.com"
   # $emailTo = "dvsivakrishna@gmail.com"
   # $subject = "The SharePoint Farm Backup Job failed on "+"$today"
   # $body = "The SharePoint Farm Backup Job failed on "+"$today and the reason for failure was $ErrorMessage."
   # $smtpServer = "1.1.1.20"
   #  $smtp = new-object Net.Mail.SmtpClient($smtpServer)
   # $smtp.Send($emailFrom, $emailTo, $subject, $body)
     Add-Content -Value $ErrorMessage -Path D:\Backups\Sharepoint\Scheduled\TestBackupLogErrorFile_$month.txt
  }

Open Task scheduler in Administrative tools 
          Start --> Administrative Tolls --> Task Scheduler

Right Click on “Task Scheduler library” and Click on “Create Task”



  1. Please place Name of task.
  2. Select option "Run whether user is logged on or not".
  3. Check option "Run With highest privileges".


Click on new button and Schedule Trigger

Browse .ps1 file and place "PowerShell command"



After click ok button task will run automatically on what time you scheduled.


No comments:

Post a Comment