Tuesday 14 February 2017

Task Scheduler to automate the execution of backup for SharePoint

Task Scheduler to automate the execution of backup files

In my Last article I have written about SharePoint Full and Differential Backup Using Central Admin and PowerShell, Today I am going to show you how to automate the execution of backup file using backup files script in task scheduler. Task Scheduler is a constituent of Microsoft Windows that provides the ability to schedule the launch of programs or scripts at pre-defined times or after specified time intervals for task scheduling. With the help of task scheduling we can reduce work for Administrative department as well we will get output efficiently without any extra resource utilized for the same.

Task scheduler was announced with Microsoft Windows 95 as System Agent which was changed in succeeded Operating system by Microsoft. A task is clearly defined by associating or relating a set of actions, which can include launching an application or taking some custom-defined action, to a set of triggers, which can either be time-based or event-based. The function which available for task scheduler with those we can defined task state and configured accordingly.

So let’s continue with the how to configure Task Scheduler to automate the execution of backup files

As we know the SharePoint 2013 backup structure and recovery processes include farm backup and recovery, Full backup and recovery, and recovery from an unattached content database which we have backup earlier also we can do restore content DB from different server in case we want to do migrate impacted server to new one. We can complete backup and recovery operations by using the SharePoint Central Administration website or Windows PowerShell cmdlets
Do refer below URL for more details about Backup in SharePoint 2013:

Below are the step by step configuration with screen shots:

  • First Start PowerShell ISE with Administrator rights and copy the below command with modification as per your environment.
  • One folder should be created on the local system or network and should be shared with full permission to copy the backup on destination folder
  • Below is the command syntax for the same also showing you how to run and what will be the result at the end?

Add-PSSnapin “Microsoft.SharePoint.PowerShell”
Set-ExecutionPolicy -ExecutionPolicy “Unrestricted” -Force
Backup-SPFarm -BackupMethod Full -Directory \\MyServer\SPBackups

  • If we are going to run differential backup we need to run below PowerShell command:

Add-PSSnapin “Microsoft.SharePoint.PowerShell”
Set-ExecutionPolicy -ExecutionPolicy “Unrestricted” –Force
Backup-SPFarm -BackupMethod Differential -Directory \\MyServer\SPBackups -Item <ContentDatabaseName> [-Verbose]
  • Required changes are highlighted with RED colour: Please do input your entry here for directory and content DB Name in case going for Differential
  • once modification done save those above script file on the local system drive with ending .PS1 file
  • So let’s create two files – one for the full backup and another for the diff backup.
  • Now save those above script file with appropriate name and one by one, you can save those file as fullbackup.PS1 and DiffBackup.PS1
Now Follow the below step by step to create the task for task scheduler to automate the execution of script backup file.
  • Open Tools called Tasks Scheduler using windows button or command  taskschd.msc
  • In the centre pane, right-click, and click Create Basic Task or just click on task in case we are going for full backup we need to schedule this task weekly or monthly
  • So Assign the task a meaning full name and description (Optional) and click on next

  • Moving to the next tab, select trigger when it wants to start this job, like as we are going with full backup it should be weekly or monthly.
  • Specify the parameter for the weekly task schedule like days and timing as well timing interval also we can mentioned here only and click on next to add action
  • Choose “Start a program” from the what action do we want the task to perform
https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/79/57/2577.step4.JPG
  • On the start a program screen type in the command as the screenshot below. The Task scheduler is intelligent enough to recognize that we want to run PowerShell and that we supplied arguments.
https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/79/57/6864.step5.JPG
  • Once we will click on the same it will appears to confirm for argument includes in the program text and will convert according the same
https://msdnshared.blob.core.windows.net/media/TNBlogsFS/prod.evol.blogs.technet.com/CommunityServer.Blogs.Components.WeblogFiles/00/00/00/79/57/8461.step6.JPG
  • Click on yes then select next button from down side of page, and click on finish after reading all those details.

Now we will go ahead and schedule differential backup with advance option available on task scheduler.

  • Following the same steps as above for opening task scheduler and creating path for copying the backup file in case if wish to copy backup file in network file should be shared with full permission.
  • This time select task scheduler and select Create task instead of basic task.
  • Now on the prompt screen, assign the name and description and select security option when this task should be triggered, here we can change the user or Group also can see on below screen shot, once all done move to next tab called Triggers.
  • On the triggers page, we need to add New trigger and select the schedule time from the settings page and select other more advanced task from advanced settings like how frequently it should be run and expiry and all, once done clock on ok and move to next tab, see below screen shot:
  • In the action tab we need to add PowerShell script file and click on ok, we need to add some argument to run automatically as Scheduler task is having more inelegancy to the that.
On the Action Page: we need to select action from the drop down of the page, next add program or script by browsing the file located on the system, add argument (optional)

  • Move to the next tab called conditions, kindly refer the screen shots and arrange according to the same and move to the settings tab

In the next tab settings: select available option from the screen if wish or we can compltet this process by selecting ok. Once we will select ok on this our task will be ready to start, so Just start first time by clciking on on task and RUN, see below screen shot for more details:
  • Once we have started task we can see the history from hostory tab for tracking the issue, same Log will be creted to Event log also so we can refer that also.
  • We can see fom above history our task has run successfully and we can see the result backup is stored to the given path as well.
Now this done, from here we will keep getting new backup copy for each time interval which we have mentioned above.

Few of important points are there to execute the above task:
  • Don’t forget to assign the SQL Server agent service account a write permission on the backup folder
  • Add-PSSnapin in the script file loads the Microsoft.SharePoint.PowerShell snap-in in the PowerShell sessionhttp://technet.microsoft.com/en-us/library/hh849705.aspx
  • Set-ExecutionPolicy -ExecutionPolicy Unrestricted allows you to run the powershell script http://technet.microsoft.com/en-us/library/ee176961.aspx
  • Backup-SPFarm -Directory   <Assign local system path for better performance>  -BackupMethod {Full | Differential} -Item <ContentDatabaseName> [-Verbose]

No comments:

Post a Comment