SharePoint 2010 Cookbook: Backup a Site Collection Automatically with a PowerShell Script
2013-04-18 12:37
543 查看
OneofthemostimportantthingsthatallSharePointusers,andespeciallyadministrators,shoulddoregularlyisbackupdata.Nevertheless,itisoftenoverlookedbymanypeoplejustbecausetheprocessmightseemtotakehours,andwedon'thavethetime
ortheinclinationtorunbackupsonaregularbasis.Inrecognitionofthisbehavior,whatiftherewasascriptforbackingupSharePointdatawhichcouldbescheduledtorunautomatically?Nowthat'sawholedifferentstory!Inthispost,Iwilldemonstrate
howtobackupaSharePoint2010sitecollectionautomaticallythroughtheuseofaPowerShellscript.
Challenge:
HowcanIbackupaSharePoint2010sitecollectionautomaticallyonaregularbasis?
Solution:
Forthesakeofthisexercise,I'mgoingtoassumethatyoualreadyhaveabackuplocationsetup.Let'ssayit's"C:\Backup\MySite".
First,verifythatyoupossessthefollowingpermissions:
SharePointFarmAdministrator.
LocalServerAdministratoronallWebservers.
db_ownerpermissiononthecontentdatabase.
FullControlpermissiononthebackupfolder.
Step1:CreateaPowerShellscriptforbackingupasitecollection(BackupSite.ps1):
Basically,theabovescriptwilldothefollowing:
Assignalltheneededinformationtostartbackup.
Trytocreateanewbackupandnamethefilebasedoncurrentdate.
Ifsuccessful,itwillwriteasuccessmessagetothelogfile.Otherwise,itwilllogtheerror/exception.
Step2:Createabatchfiletorunthescript(RunBackup.bat):
Step3:CopyboththescriptandbatchfiletoafolderontheSharePointServer.
Step4:RunthebatchfiletostartbackingupthesitecollectionimmediatelyoruseWindowsTaskSchedulertoscheduleit.
Ihaveattachedboththescriptandthebatchfileattheendofthispostforyourconvenience.
Notes:
ThismethodworkswithbothSharePoint2010FoundationandServer.
Whileperformingthebackup,theSharePointsitecollectionwillbesettoRead-Onlytopreventdatacorruption,soit'srecommendedthatyourunthisscriptduringoff-peakhours.
http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2010/12/21/sharepoint-2010-cookbook-powershell-script-to-backup-a-site-collection-automatically.aspx
ortheinclinationtorunbackupsonaregularbasis.Inrecognitionofthisbehavior,whatiftherewasascriptforbackingupSharePointdatawhichcouldbescheduledtorunautomatically?Nowthat'sawholedifferentstory!Inthispost,Iwilldemonstrate
howtobackupaSharePoint2010sitecollectionautomaticallythroughtheuseofaPowerShellscript.
Challenge:
HowcanIbackupaSharePoint2010sitecollectionautomaticallyonaregularbasis?
Solution:
Forthesakeofthisexercise,I'mgoingtoassumethatyoualreadyhaveabackuplocationsetup.Let'ssayit's"C:\Backup\MySite".
First,verifythatyoupossessthefollowingpermissions:
SharePointFarmAdministrator.
LocalServerAdministratoronallWebservers.
db_ownerpermissiononthecontentdatabase.
FullControlpermissiononthebackupfolder.
Step1:CreateaPowerShellscriptforbackingupasitecollection(BackupSite.ps1):
Add-PsSnapinMicrosoft.SharePoint.PowerShell
Start-SPAssignment-Global#Thiscmdlettakescareofthedisposableobjectstopreventmemoryleak.
$mySite="http://mySite"#ReplacewithyoursitecollectionURL
$backupLocation="C:\Backup\MySite"#Replacewithyourbackuplocation
$logFile="$backupLocation\BackupLog.log"#Replacewithyourdesiredlogfilelocation
$today=Get-Date-format"MM-dd-yyyyHH.mm.ss"#Getcurrentdateandformatittoavoidinvalidcharacterssuchas"/"and":"
write-HostStartbackingup$mySiteto$backupLocation
try
{
#Createanewbackupfileandnameitbasedoncurrentdate.Ifyouwanttocreateonly1backupfileandoverwriteiteachtimethebackupisrun,youcanreplace"$today.bak"withyourdesiredfilename.
Backup-SPSite-Identity$mySite-Path$backupLocation\$today.bak-force-eaStop
write-HostBackupsucceeded.
#Writesuccessmessagetothelogfile
write"$today$mySitesuccessfullybackedup.">>$logFile
}
catch#Iftheprocessfailed
{
write-HostBackupfailed.See$logFileformoreinformation.
#Writeerrormessagetothelogfile
write"$todayError:$_">>$logFile
}
Stop-SPAssignment-Global
Remove-PsSnapinMicrosoft.SharePoint.PowerShell
write-Host"Finishedscript."
Basically,theabovescriptwilldothefollowing:
Assignalltheneededinformationtostartbackup.
Trytocreateanewbackupandnamethefilebasedoncurrentdate.
Ifsuccessful,itwillwriteasuccessmessagetothelogfile.Otherwise,itwilllogtheerror/exception.
Step2:Createabatchfiletorunthescript(RunBackup.bat):
cd/d%~dp0
powershell-file".\BackupSite.ps1"
Step3:CopyboththescriptandbatchfiletoafolderontheSharePointServer.
Step4:RunthebatchfiletostartbackingupthesitecollectionimmediatelyoruseWindowsTaskSchedulertoscheduleit.
Ihaveattachedboththescriptandthebatchfileattheendofthispostforyourconvenience.
Notes:
ThismethodworkswithbothSharePoint2010FoundationandServer.
Whileperformingthebackup,theSharePointsitecollectionwillbesettoRead-Onlytopreventdatacorruption,soit'srecommendedthatyourunthisscriptduringoff-peakhours.
相关文章推荐
- SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script
- SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script
- SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script
- SharePoint 2013 Backup Farm Automatically With a Powershell and Windows Task Schedule
- SharePoint 2013 Backup Farm Automatically With a Powershell and Windows Task Schedule
- SharePoint 2013 Backup Farm Automatically With a Powershell and Windows Task Schedule
- SharePoint 2010 Provisioning A New Farm with PowerShell
- SharePoint自动化系列——通过PowerShell创建SharePoint Site Collection
- [SharePoint 2010] Modify lookup mapping with PowerShell
- Powershell 功能函数大全(Sharepoint 2013/2010)
- MOSS2007中型服务场管理 教材<01>:SiteCollection Backup & Restore
- SharePoint 2010 PowerShell(3)使用PowerShell管理列表
- SharePoint 2013 Create Community Site with Moderate permission
- SharePoint 2010 权限提升-SPSecurity.RunWithElevatedPrivileges method (Microsoft.SharePoint)
- 小议:SharePoint 2013如何创建与Web Application不同的Host Header Site Collection?
- SharePoint 2010 查询不以某个字符开头的数据[How to Create a SharePoint “Does Not Begin With” Filtered List View]---计算栏的妙用
- SharePoint 2010 PowerShell(3)使用PowerShell管理列表
- SharePoint自动化系列——Manage "Site Subscriptions" using PowerShell
- PowerShell 在 SharePoint 2010 自动化部署中的应用(2)-编译打包
- SharePoint 2010 PowerShell 系列 之 文档管理 初级应用