Posted by: John Ferringer | July 14, 2011

How to Set Up a Scheduled Task to Backup Your SharePoint Farm


Shortly after my guest post at the HeyScriptingGuy blog on how you can use PowerShell to script out a backup of a SharePoint farm, I saw a post on Twitter from a friend of mine, @JoyKnows, mentioning that a system administrator that she works with had seen the post and was taking a crack and using it in a farm he managed. I was definitely thrilled to hear it had caught someone’s attention, and asked her to let me know if he ran into any issues or had any questions about what I had done. As it turned out, he hadn’t really ever used Windows Server’s Scheduled Tasks functionality before and was having some issues configuring the script to run properly through a Scheduled Task, mainly in the area of how to configure his Scheduled Task to correctly pass the necessary input parameters into the script when it is run. I initially thought about trying to respond on Twitter with some tips, but this is one of those things where 140 characters just aren’t going to cut it, so you get a follow-up blog post instead!

So, first of all, what is the Windows Task Scheduler? Well, it’s a handy tool that’s been around since Windows 2000 and Windows XP, so it’s in both the server and workstation versions of the Windows operating system. Basically, what it does is allow you to schedule an application or script to be run on your local computer at a pre-defined time or after a set period of time. The Task Scheduler allows you to automate the execution of these applications or scripts, which has some quite pleasant implications. Let me put it another way: would you like to be all nice and snuggly in your bed every night for the next several years while your SharePoint farm is being backed up the same way every time, or would you prefer to be staring at a computer screen with bleary eyes each of those nights trying to type in the parameters for your script correctly after three failed attempts (or worse, sleeping through your alarm each of those nights)??? Yeah, me too.

So, how do we set up the Windows Task Scheduler to run a script or an application? It may seem pretty straightforward, but as Joy’s friend above mentioned, it is actually a little more tricky when you want to include parameters for the application or script run by your Scheduled Task, which is something we definitely have to do for our SharePoint Farm Backup script. Let’s dive into it and I’ll try to walk you through what I’m talking about. (NOTE: these steps were executed in a VM running Windows Server 2008 R2, the experience is slightly different in Windows Server 2003 but to my knowledge not significantly different in Windows Server 2008).

  1. From your SharePoint server’s Start menu, go to Administrative Tools and select the Task Scheduler option.
  2. This will open the Task Scheduler in a new window.
  3. Feel free to take a look around the Task Scheduler when it opens. You’ll see a library of common scheduled tasks in the left-hand pane, an overview of Task Scheduler on the server in the center pane, and the right pane will contain a set of actions you perform in Task Scheduler. In this case, you’ll want to click on the Create Task… item to continue.
  4. This opens the Create Task window. In the first pane of the window, you’ll have several options to complete, a couple of which I’ll touch on below:
    – Name: this is important, give your scheduled task a descriptive yet concise name (remember, you may not always be the person looking at it and wondering what it does)
    – Description: also important. Give this task a good description, so someone can understand what it does and how it’s used.
    – When running the task, use the following account: this is where you’ll need to provide the credentials for the service account you want your backup script run as. This is very, very important. Use the Change User or Group… button to configure this.
    – Run…: select the Run whether user is logged on or not radio button, so you don’t have to be logged in for the task to run
    – Configure for: you can select the target operating system for your task. I would recommend selecting the same OS as your SharePoint server is running. To be very honest, I haven’t messed with this setting much, so you may want to do some investigating on your own if you’re worried about it.The screenshot below shows an example I’ve created in my VM. Notice that I’ve left the “Configure for” field at the default setting in my example even though I’m running Windows Server 2008 R2. I’ve also checked the Run With Highest Privileges check box, you’ll definitely want to do that if your server has User Account Control (UAC) enabled.
  5. Now click on the Triggers tab to move over to it. You’ll want to click on the New… button to set up a new scheduled Trigger for your Scheduled Task.
  6. This opens the New Trigger window. This is where we’re determine the schedule that our task will run on. For the backup script, I’m configuring a daily trigger because I want it to run once a day. For this backup script, think carefully about when it should run to avoid contending with other activities that may contend for the same farm resources, such as a search index crawl. Once your trigger is configured, click the OK button to save your trigger, then click the Actions tab to move on to the next section for the task. The screenshot below shows how I’ve configured the trigger:
  7. The Actions tab is much like the Triggers tab, it will be blank until you click the New… button to create a new Action. This is where you’ll tell the Scheduled Task which action it should take when it runs.
  8. The New Action window opens when you click the New… button, and presents several important fields you’ll need to fill out to run your script properly. Here’s how I filled them out for my script:
    – Action: select Start a program from the   menu.
    – Program/script: This is where things get a little tricky since we’re running a PowerShell script.
    – The first thing to remember is that the program we’re going to run is PowerShell, NOT script file name. We need to run PowerShell, the Task Scheduler won’t know how to run the script otherwise. (note that I’m not providing a full path to PowerShell, I’m letting the Windows PATH variable determine where PowerShell is stored. If you want to, go ahead and explicitly define that path, its up to you)
    – After PowerShell, you’ll notice the parameter switch -command. This indicates to the Task Scheduler that you have a parameter command that you want to pass to the program you’re calling (PowerShell). Now, you may see some examples where the -File parameter switch is used here, but you should only use when you’re not passing any parameters to the PowerShell script. Since my farm backup script needs additional parameters to run, I’m using the -command switch. After the -command parameter switch, I wrapped my script’s normal parameters up in double quotation marks (“). Also note here that where I was already using double quotation marks to wrap text, I changed them to single quotation marks (‘), so you’ll want to make sure to do that too. Finally, you may need to add an additional parameter to your call (after the quotation marks for the call to the script’s parameters) if your server’s PowerShell Execution Policy doesn’t allow unsigned or remotely signed scripts; rather than changing that policy for this one script just add -ExecutionPolicy (change <PolicyType> to your preferred policy type).
    – The screenshot below shows what I entered in the New Action window:
  9. After clicking the OK button in the New Action window, the Task Scheduler will show you a warning screen. (I would have loved it if they would have used Clippy instead of the question mark icon, seeing as how they went with the message “It appears…”. Thanks for bearing with me on that!) You’ll want to review what it’s showing you to confirm that you’ve entered your arguments correctly, click the Yes button once you’ve made sure they’re right:
  10. Now there should be a line item in your Actions tab for the new Action you just created. You can now go ahead and configure the Conditions and Settings tabs for your task if you want, but I left the defaults alone in mind. Once you’re all done setting up your script, click the OK to create your task.
  11. The tool did present me with a prompt after I clicked the button, asking me to provide credentials for the account that would be used to run the task, so you’ll need to confirm the account’s username and provide its password, then click the OK button.
  12. This brings us to the last step in the process, verifying that your task is now shown in the computer’s Scheduled Task library:

And that should be it. The nice thing is that you should now be able to right-click on your new task and manually execute it to test out and confirm that you’ve got everything configured properly. MAKE SURE TO TEST THIS, I CAN’T STRESS THIS ENOUGH!

I hope you find this useful, I definitely encountered a couple of interesting “Ah ha!” moments while going through it myself 🙂

John

Advertisements

Responses

  1. OK John, I am stumped!

    I think something in my script is wrong and I was wondering if you could check it for me. I followed everything to a “t” but not being able to see the entire path in your screenshot above, I’m sure mine is broken. Here’s what I have:

    -command “.\SP_Backup.ps1 -drive ‘C:\scripts’ -backupDir ‘\\vfs15\sqlbackups_ii$\SharePoint’ -fullBackupDay ‘Friday’ -configDBInstance SQL105-PROD3\PROD3 -configDBName DKC_SharePoint_Config -nbrDaysRetained 7”

    This is the exact entry I am using for the Programs/Scripts box just under step 9. I see the history in Task Scheduler that it kicks off but it returns with codes 1 and 2 and nothing is happening on the CA side. What am I doing wrong?

    Brian

    • OK, I took script out and pasted it into PS and executed it. I can see it’s running because the folder and files are being created on the share. However, I keep refreshing the Backup and Restore Status in CA and I don’t see it running. The last time I tested your script, I actually saw it running on the page after refreshing it.

      • Brian —

        First thing I would suggest doing is to check the event logs and the ULS logs on the server running the backup. You should also check the backup directory, there should be a log file there that may also have some information about what’s going on. Let me know what you see there, that might provide some additional useful info that can help. I’ll try to see if I can get a chance this weekend or Monday to dig into it further and let you know if I can come up with any other suggestions.

  2. OK, I think I “may” have figured out one issue, but a new one cropped up. So, the SP_Admin and SP_Farm accounts were nested in an AD group given access to the share, but I decided to add them directly to it and the crop of red errors went away. However, the script fails and in Event Viewer, I see this error:

    “There does not appear to be sufficient disk space available on the c: of to hold a new backup. Please resolve this issue before creating a new backup”

    What is the purpose of the “-drive” option in the script? Is this just a temp repository for files until they ultimately get copied over to the share? I already have an existing full back that I did within SharePoint during those failures last week, so I modified the script today to say that the full backup takes place on Sunday, thinking that the differential should kick off today, but I am getting that low disk error. The entire size of the backup is around 68 GBs, but I have 60 free on the VM we have SP running on. However, I thought the -backupDir is ultimately where the backup goes to. That dir has 300 GB total and only 68 are being used.

  3. John,

    Have you tried making this run without the .ps1 file by manually putting the code into a scheduled task? I had never been able to get the .ps1 file to work correctly with foundation 2010, but I did have some success when i created the full command in a scheduled task.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: