locked
sharepoint 2010 backup using powershell RRS feed

  • Question

  • hi,

    I am new to sharepoint 2010 and powershell.  I performed a full farm backup using Central Admin but I need to make scheduled nightly backups using powershell or stsadm.  I was told to use powershell as stsadm is for backward compatible.  I would like to schedule regular backups using windows scheduled but do not know how to create the batch files for to run the powershell backup.  Does anyone have some example scripts that I could put into a batchfile to run.   Any other ideas would also be appreciated.
    Thursday, February 18, 2010 7:16 PM

Answers

  • When you do a Farm level backup, SharePoint tells SQL to do database dump to that location. Here are some things that have to work in order for a farm backup to work:

    1. Your Central Admin app pool account must have read/write access to the location of the backups.
    2. Your SQL Service account must have read/write access to the location of the backups.
    3. If you're running a farm backup from STSADM or Windows PowerShell, the account you're running it as must have read/write access to the location of the backups
    4. The location must be accessible from the SharePoint machine the backup is running on.
    5. The location must be accessible from the SQL instance that SharePoint is trying to back up.
    6. This is why all the examples are UNCs, \\server\share, and not local paths, C:\backups
    7. Hope that helps,

    tk

    Sunday, March 28, 2010 5:09 PM
  • Windows PowerShell script files are .ps1 files.


    You can use Backup-SPFarm to do it. For help and examples, type get-help Backup-SPFarm -examples.

    For example, the follow script will start a full backup to e:\backup. 

    Backup-SPFarm -Directory E:\Backup -BackupMethod full

    Save it as E:\Scripts\BackupSharePointFarm.ps1.  You may need to use "set-executionpolicy RemoteSigned" to allow a local unsigned ps1 file to run.


    And put the following as your scheduled command line, or inside your batch file....
    powershell -command E:\Scripts\BackupSharePointFarm.ps1

    In task scheduler, the account need to be set to the one who has admin permissions (or at least with powershell permission) otherwise it cannot run properly.

    Thursday, February 18, 2010 7:45 PM

All replies

  • Windows PowerShell script files are .ps1 files.


    You can use Backup-SPFarm to do it. For help and examples, type get-help Backup-SPFarm -examples.

    For example, the follow script will start a full backup to e:\backup. 

    Backup-SPFarm -Directory E:\Backup -BackupMethod full

    Save it as E:\Scripts\BackupSharePointFarm.ps1.  You may need to use "set-executionpolicy RemoteSigned" to allow a local unsigned ps1 file to run.


    And put the following as your scheduled command line, or inside your batch file....
    powershell -command E:\Scripts\BackupSharePointFarm.ps1

    In task scheduler, the account need to be set to the one who has admin permissions (or at least with powershell permission) otherwise it cannot run properly.

    Thursday, February 18, 2010 7:45 PM

  • thanks for you help


    I opened a powershell prompt on my Sharepoint Server 2010 and received the following error when I tried to run the Bacup-SPFarm command

    "The term 'Backup-SPFarm' is not recognized as the name of a cmdlet, function, script file, operable program.  Check the spelling of the name, or if a path was included , verifiy that the path is correct and try again."

    I am thinking that maybe some Sharepoint extensions were not added to Powershell.  Has anyone seen this or have any ideas?

    Tuesday, February 23, 2010 3:31 PM
  • You can launch SharePoint Management Shell, or load snap in in the powershell prompt manually.

    Add-PSSnapin Microsoft.SharePoint.PowerShell

    Tuesday, February 23, 2010 3:35 PM
  • Hi,

    that worked!!  Powershell now understands my backup commands.  The backup started but it eventually failed with the following error in the backup log.  Does this mean it needs access to SQL or does something need access to the backup folder.  I read somewhere it may be due a timer job needing access to the back folder.  Anyone seen this?


    [2/23/2010 11:41:47 AM] Verbose: [PerformancePointService_f23ebbf3fc434fe699407a9c5606bc88] SQL Server Command: BACKUP DATABASE [PerformancePointService_f23ebbf3fc434fe699407a9c5606bc88] TO DISK=@db_loc WITH NAME=@db_name, STATS=5, NOINIT, NOSKIP, NOFORMAT, NOREWIND
     @db_name=PerformancePointService_f23ebbf3fc434fe699407a9c5606bc88, @db_loc=d:\farmbackup\spbr0004\00000068.bak
    [2/23/2010 11:41:47 AM] Verbose: [PerformancePointService_f23ebbf3fc434fe699407a9c5606bc88] SQL command timeout is set to 1.00 hours.
    [2/23/2010 11:41:47 AM] FatalError: Object PerformancePointService_f23ebbf3fc434fe699407a9c5606bc88 failed in event OnBackup. For more information, see the error log located in the backup directory.
     SqlException: Cannot open backup device 'd:\farmbackup\spbr0004\00000068.bak'. Operating system error 21(The device is not ready.).
    BACKUP DATABASE is terminating abnormally.
    Tuesday, February 23, 2010 6:45 PM
  • make sure your account which runs backup timerjob have full access to the folder. Otherwise backup will have this issue.
    Tuesday, February 23, 2010 9:51 PM
  • I wrote a blog entry showing how to automate SharePoint farm backups using powershell:

    http://imperfectit.blogspot.ca/2010/03/automate-sharepoint-2010-farm-backups.html

    Sunday, March 28, 2010 2:15 AM
  • When you do a Farm level backup, SharePoint tells SQL to do database dump to that location. Here are some things that have to work in order for a farm backup to work:

    1. Your Central Admin app pool account must have read/write access to the location of the backups.
    2. Your SQL Service account must have read/write access to the location of the backups.
    3. If you're running a farm backup from STSADM or Windows PowerShell, the account you're running it as must have read/write access to the location of the backups
    4. The location must be accessible from the SharePoint machine the backup is running on.
    5. The location must be accessible from the SQL instance that SharePoint is trying to back up.
    6. This is why all the examples are UNCs, \\server\share, and not local paths, C:\backups
    7. Hope that helps,

    tk

    Sunday, March 28, 2010 5:09 PM
  • Here's another great blog post about how to do it in 2010. Only thing you may need to do is a differential backup script to it.

     

    http://imperfectit.blogspot.com/search/label/backups

     

     

    Tuesday, June 8, 2010 11:29 PM
  • When you do a Farm level backup, SharePoint tells SQL to do database dump to that location. Here are some things that have to work in order for a farm backup to work:

    1. Your Central Admin app pool account must have read/write access to the location of the backups.
    2. Your SQL Service account must have read/write access to the location of the backups.
    3. If you're running a farm backup from STSADM or Windows PowerShell, the account you're running it as must have read/write access to the location of the backups
    4. The location must be accessible from the SharePoint machine the backup is running on.
    5. The location must be accessible from the SQL instance that SharePoint is trying to back up.
    6. This is why all the examples are UNCs, \\server\share , and not local paths, C:\backups
    7. Hope that helps,

    tk


    How about a least privilege approach?

    I'm trying to set up a user account dedicated to SP2010 Farm level backup. The shared folder is set up as described by Todd. The user was added with Add-SPShellAdmin. When I try to execute Backup-SPFarm from a shell I get an error "You need to have Machine administrator priviliges to run this cmdlet."

    Is this true? Does the user have to be a local administrator in order to perform a farm backup?

    Tuesday, July 13, 2010 11:19 AM
  • Thak you for all the explanations.

    ...

    I moved my question to new thread:

    Backup-SPFarm do not run in scheduler in batch mode - option "run whether user is logged on or not"

    http://social.technet.microsoft.com/Forums/en/sharepoint2010setup/thread/c0457b3b-0979-44a0-a0c6-1c6847f3cba8
    Wednesday, December 29, 2010 6:03 PM
  • Great Reply, Todd...
    Thursday, December 30, 2010 6:16 PM
  • Hi,

    That is indeed a nice topic.
    Nevertheless, I have a problem with my script or something else.

    My first script is a .bat file which contains:
    powershell -Command c:\Scripts\Backup_sharePoint_Full.ps1

    and the second script is the Backup_sharePoint_Full.ps1 file which contains:
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    Backup-SPFarm -Directory \\ServerName\Share -BackupMethod full

    The scheduled task in Windows is set to "Run whether the user is logged or not" and "Run with highest privileges".
    The domain account used for this purpose is a service account and has all rights on the shared folder \\ServerName\Share.

    When I manually run the scheduled task, the task is completed without backups being taken. The steps are "queued" then "Running" and then "finished". The "running" step lasts for couple of seconds and there are no backup files at  \\ServerName\Share.

    When I run the scripts manually, everything is going fine and backup is taken successfully.
    I assume there is a problem here with Windows scheduling.

    Can you please be a help?

    • Proposed as answer by Yaugen Friday, March 11, 2011 6:07 PM
    • Unproposed as answer by Yaugen Friday, March 11, 2011 6:13 PM
    Monday, February 28, 2011 10:28 AM
  • Hi,

    That is indeed a nice topic.
    Nevertheless, I have a problem with my script or something else.

    My first script is a .bat file which contains:
    powershell -Command c:\Scripts\Backup_sharePoint_Full.ps1

    and the second script is the Backup_sharePoint_Full.ps1 file which contains:
    Add-PSSnapin Microsoft.SharePoint.PowerShell
    Backup-SPFarm -Directory \\ServerName\Share -BackupMethod full

    The scheduled task in Windows is set to "Run whether the user is logged or not" and "Run with highest privileges".
    The domain account used for this purpose is a service account and has all rights on the shared folder \\ServerName\Share.

    When I manually run the scheduled task, the task is completed without backups being taken. The steps are "queued" then "Running" and then "finished". The "running" step lasts for couple of seconds and there are no backup files at  \\ServerName\Share.

    When I run the scripts manually, everything is going fine and backup is taken successfully.
    I assume there is a problem here with Windows scheduling.

    Can you please be a help?

    I have faced exactly the same issue. I have found out that I can run the task only when "Run only when user is logged on" option is selected. But the result does not seems to be a solution for me. Any other ideas?
    Friday, March 11, 2011 6:11 PM
  • If you build the Start-Transcript -Path <location> into your backup Powershell script, I think you will find that for some reason your backup account can not resolve UNC names when the user is not logged on.  I have struggled with this for days and I'm just about to give up on it.  It looks like the solution is that when scheduling a Powershell backup in Sharepoint 2010 I either need a service account to be logged into that machine at all times, or I need to back up the data to the web front end where I run the script.  In my opinion, this sucks.  That means I need to pull the data off the SQL box to my WFE, then if I restore I have to move it back.  Very nice indeed!

     

    I have tried everything from creating a new PS drive, mapping network drives, net use, etc.  Anyone else seen this?

    • Proposed as answer by wkucardinal Wednesday, April 27, 2011 7:58 PM
    Wednesday, April 27, 2011 6:51 PM
  • If you build the Start-Transcript -Path <location> into your backup Powershell script, I think you will find that for some reason your backup account can not resolve UNC names when the user is not logged on.  I have struggled with this for days and I'm just about to give up on it.  It looks like the solution is that when scheduling a Powershell backup in Sharepoint 2010 I either need a service account to be logged into that machine at all times, or I need to back up the data to the web front end where I run the script.  In my opinion, this sucks.  That means I need to pull the data off the SQL box to my WFE, then if I restore I have to move it back.  Very nice indeed!

     

    I have tried everything from creating a new PS drive, mapping network drives, net use, etc.  Anyone else seen this?


    Solved.  You have to be sure that you do not select the box for "Do not store password".  If you do check the box your script will not be able to authenticate against the remote UNC path.
    • Proposed as answer by wkucardinal Wednesday, April 27, 2011 7:58 PM
    Wednesday, April 27, 2011 7:58 PM
  • In my case it didn't untill I logged in to the box as user that sheduled task is run under.

    The first time you login windows creates user profile on the machine and I guess that was the case.

    After that, as mentioned above:

    1. Login to the box at leas once using user from Sheduled Task

    2. powershell -Command c:\Scripts\Backup_sharePoint_Full.ps1

    3. "Run whether the user is logged or not" and "Do not store password"

    4. "Run with highest privileges"

    5. RW access to the backup path granted for Central Admin App pool user, SQL Service user, user you run sheduled task under.

     

    Works like a char.

    Thanks guys.

     

    Thursday, November 3, 2011 11:35 AM
  • I have faced exactly the same issue. I have found out that I can run the task only when "Run only when user is logged on" option is selected. But the result does not seems to be a solution for me. Any other ideas?

     

     

     

     

     

    _______________

    Free Online Backup

    Tuesday, January 31, 2012 12:25 PM
  • Hi There,

    I'm totally new to SP2010 and your scripts helped me a lot. Thanks for the same.

    It would be great if you can let me know how long does it take to finish the backup (its already 30mins for me) I saw the whole farm is less than 2GB only and I need to use this back in another server ASAP. Can you please provide me further assistance on this

    Thanks

    Wednesday, February 8, 2012 7:55 PM
  • I just want to add that using this method with a DNS alias doesn't seem to work. I was getting errors 3041 and 18204 in the event log no matter what permissions I gave to the backup (Farm) account or the SQL service account. Once I started using the actual server name it worked no problem!
    Tuesday, February 21, 2012 11:21 AM
  • Hi There,

    I'm totally new to SP2010 and your scripts helped me a lot. Thanks for the same.

    It would be great if you can let me know how long does it take to finish the backup (its already 30mins for me) I saw the whole farm is less than 2GB only and I need to use this back in another server ASAP. Can you please provide me further assistance on this

    Thanks

    Hey

    I have written a Backup Script which will tell you how long it takes once complete. http://spfarmbackup.codeplex.com

    Also whilst the backup is running, if you download Baretail (there is a free version)  you can watch the spbackup.log update realtime and monitor the progress of your farm backup.


    blog: http://sharepointingitout.blogspot.com



    • Edited by D Marsden Tuesday, February 21, 2012 12:46 PM typo
    Tuesday, February 21, 2012 12:43 PM
  • Hello,

    I can run the PS1 script from PowerShell just fine, The problem when I try to run the batch file that points to the .PS1 script I am getting:

    The term 'Backup-SPFarm' is not recognized as the name of a cmdlet, function, s
    cript file, or operable program. Check the spelling of the name, or if a path w
    as included, verify that the path is correct and try again.
    At C:\Scripts\BackupSharePointFarm.ps1:1 char:14
    + Backup-SPFarm <<<<  -Directory C:\BackupSPFarm -BackupMethod full
        + CategoryInfo          : ObjectNotFound: (Backup-SPFarm:String) [], Comma
       ndNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Thank you,

    Morris

    Friday, March 9, 2012 6:19 PM
  • Hi ,

    Todd Nice description and Great to share us

    Regars,s

    Mahesh.K

    Tuesday, May 15, 2012 8:38 AM
  • When you do a Farm level backup, SharePoint tells SQL to do database dump to that location. Here are some things that have to work in order for a farm backup to work:

    1. Your Central Admin app pool account must have read/write access to the location of the backups.
    2. Your SQL Service account must have read/write access to the location of the backups.
    3. If you're running a farm backup from STSADM or Windows PowerShell, the account you're running it as must have read/write access to the location of the backups
    4. The location must be accessible from the SharePoint machine the backup is running on.
    5. The location must be accessible from the SQL instance that SharePoint is trying to back up.
    6. This is why all the examples are UNCs, \\server\share, and not local paths, C:\backups
    7. Hope that helps,

    tk

    Everything here is correct, but I just want to add an information.

    I had to put the shared folder in the SQL Server box (before it was placed in the SharePoint computer and I had a few errors in the log). After this change, it worked nicely.

    Thursday, July 26, 2012 8:33 PM
  • Proposing your own post as an answer is generally considered bad form.

    When your share was on the SharePoint server did the SQL service account have "Full Control" access to both the Share and the folder the share was exposing? I almost never save the Farm backups to the SQL server and never have any problems.

    tk


    SharePoint MVP My blog My SharePoint 2010 Admin book: Professional SharePoint 2010 Administration

    Thursday, July 26, 2012 8:57 PM
  • Windows PowerShell script files are .ps1 files.


    You can use Backup-SPFarm to do it. For help and examples, type get-help Backup-SPFarm -examples.

    For example, the follow script will start a full backup to e:\backup. 

    Backup-SPFarm -Directory E:\Backup -BackupMethod full

    Save it as E:\Scripts\BackupSharePointFarm.ps1.  You may need to use "set-executionpolicy RemoteSigned" to allow a local unsigned ps1 file to run.


    And put the following as your scheduled command line, or inside your batch file....
    powershell -command E:\Scripts\BackupSharePointFarm.ps1

    In task scheduler, the account need to be set to the one who has admin permissions (or at least with powershell permission) otherwise it cannot run properly.


    Worked!  Thanks Jie Li.

    Oh, I didn't have to set "set-executionpolicy RemoteSigned" for some reason.  Maybe it was already set.


    Best Regards, Mike Griffin - Seattle


    Monday, August 6, 2012 8:32 AM
  • You can launch SharePoint Management Shell, or load snap in in the powershell prompt manually.

    Add-PSSnapin Microsoft.SharePoint.PowerShell


    This worked as well!  Thank you so much!

    Best Regards, Mike Griffin - Seattle

    Monday, August 6, 2012 8:35 AM
  • backup Full and Differential SharePoint farm day by day.

    http://gallery.technet.microsoft.com/sharepoint/backup-Full-and-Differentia-5bbae819/view/Reviews


    Deepesh Yevle MCTS

    Thursday, August 23, 2012 1:08 PM
  • How can I run this as a scheduled task with PowerShell Version 3 installed?  I can try to go into do -Version 2 but do not know how to "call" my PS1 file that has the command to backup

    C:\Windows\System32\WindowsPowerShell\v1.0\PowerShell.exe -Version 2 -NoExit  " & ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CONFIG\POWERSHELL\Registration\\sharepoint.ps1 ' " " & ' C:\_IT\PowerShellScripts\Backup_SharePoint_Development-Full.ps1 ' "

    Dev Full PS1 file -

    Backup-SPFarm -Directory \\SERVERNAME\SQL_Full_Backups\SharePoint\spbackup\development-farm\ -BackupMethod full -force -verbose

    Monday, December 3, 2012 7:12 PM