您的位置:首页 > 运维架构 > Shell

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):

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.

http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2010/12/21/sharepoint-2010-cookbook-powershell-script-to-backup-a-site-collection-automatically.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐