SharePoint 2010 Cookbook: Backup a Site Collection Automatically with a PowerShell Script

23 篇文章 0 订阅
18 篇文章 0 订阅

One of the most important things that all SharePoint users, and especially administrators, should do regularly is back up data. Nevertheless, it is often overlooked by many people just because the process might seem to take hours, and we don't have the time or the inclination to run backups on a regular basis. In recognition of this behavior, what if there was a script for backing up SharePoint data which could be scheduled to run automatically? Now that's a whole different story! In this post, I will demonstrate how to back up a SharePoint 2010 site collection automatically through the use of a PowerShell script.

Challenge:

How can I backup a SharePoint 2010 site collection automatically on a regular basis?

Solution:

For the sake of this exercise, I'm going to assume that you already have a backup location setup. Let's say it's "C:\Backup\MySite".

First, verify that you possess the following permissions:

  • SharePoint Farm Administrator.
  • Local Server Administrator on all Web servers.
  • db_owner permission on the content database.
  • Full Control permission on the backup folder.

Step 1: Create a PowerShell script for backing up a site collection (BackupSite.ps1):

Add-PsSnapin Microsoft.SharePoint.PowerShell
 
Start-SPAssignment -Global            # This cmdlet takes care of the disposable objects to prevent memory leak.
 
$mySite="http://mySite"            # Replace with your site collection URL
$backupLocation="C:\Backup\MySite"        # Replace with your backup location
$logFile="$backupLocation\BackupLog.log"    # Replace with your desired log file location
 
$today=Get-Date -format "MM-dd-yyyy HH.mm.ss"    # Get current date and format it to avoid invalid characters such as "/" and ":"
 
write-Host Start backing up $mySite to $backupLocation
try
{
    # Create a new backup file and name it based on current date. If you want to create only 1 backup file and overwrite it each time the backup is run, you can replace "$today.bak" with your desired file name.
    Backup-SPSite -Identity $mySite -Path $backupLocation\$today.bak -force -ea Stop
 
    write-Host Backup succeeded.
 
    # Write success message to the log file
    write "$today    $mySite successfully backed up.">>$logFile    
}
catch        # If the process failed
{
    write-Host Backup failed. See $logFile for more information.
 
    # Write error message to the log file
    write "$today    Error: $_">>$logFile
}    
 
Stop-SPAssignment -Global
 
Remove-PsSnapin Microsoft.SharePoint.PowerShell
 
write-Host "Finished script."

Basically, the above script will do the following:

  • Assign all the needed information to start backup.
  • Try to create a new backup and name the file based on current date.
  • If successful, it will write a success message to the log file. Otherwise, it will log the error/exception.

Step 2: Create a batch file to run the script (RunBackup.bat):

cd /d %~dp0
powershell -file    ".\BackupSite.ps1"

Step 3: Copy both the script and batch file to a folder on the SharePoint Server.

Step 4: Run the batch file to start backing up the site collection immediately or use Windows Task Scheduler to schedule it.

I have attached both the script and the batch file at the end of this post for your convenience.

Notes:

  • This method works with both SharePoint 2010 Foundation and Server.
  • While performing the backup, the SharePoint site collection will be set to Read-Only to prevent data corruption, so it's recommended that you run this script during off-peak hours.

 

http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2010/12/21/sharepoint-2010-cookbook-powershell-script-to-backup-a-site-collection-automatically.aspx

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值