SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Sc

 2023-09-05 阅读 177 评论 0

摘要:阅读目录 Step 1:Create a PowerShell Script for Backing up a site collectionStep 2:Create a batch file to run the automatically backup powershell scriptStep 3:Copy both the script and batch file to a folder on the SharePoint Server Step 4:Schedule a Batch

阅读目录

  • Step 1:Create a PowerShell Script for Backing up a site collection 
  • Step 2:Create a batch file to run the automatically backup powershell script
  • Step 3:Copy both the script and batch file to a folder on the SharePoint Server
  • Step 4:Schedule a Batch File to Run automatically

In this post I will introduce a way how to run a script for backing up SharePoint data which could be scheduled to run automatically.

回到顶部

Step 1:Create a PowerShell Script for Backing up a site collection 

param([string] $site,[string] $dir,[string] $type)

if(!(Get-PSSnapin |Where-Object {$_.Name -eq "Microsoft.SharePoint.PowerShell"}))

{

Add-PSSnapin "Microsoft.SharePoint.PowerShell"

}

Start-SPAssignment -Global #防止内存泄露 #

function IsNullOrEmpty($str)

{

if($str)

{

return $false

}

else

{

return $true

}

}

$currentDate=Get-Date -Format "yyyy-MM-dd HH-mm-ss"

$logFile="$dir\BackupLog.log"

try

{

Write-Host "开始备份 $site ($type) $dir"

Write "Site Collection Url:$site($type)">>$logFile

Write "Dir Path:$dir">>$logFile

if(IsNullOrEmpty($site))

{

Write-Host "Site Collection不能为空"

Write "Site Collection不能为空">>$logFile

return

}

if(IsNullOrEmpty($dir))

{

Write-Host "路径不能为空"

Write "路径不能为空">>$logFile

return

}

Backup-SPSite -Identity $site -Path $dir\$currentDate-$type.bak -Force

Write-Host "备份成功"

Write "成功于 $currentDate 备份 $site ">>$logFile

}

catch

{

Write-Host "备份失败,具体信息详见Log"

Write "$currentDate Error:$_">>$logFile

}

Stop-SPAssignment -Global

Write-Host "PowerShell 执行完毕"

Write "PowerShell 执行完毕">>$logFile

Basically,the above script will do the following:

  • Assign all the needed information to start backup.
  • Try to create a new backup script 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 automatically backup powershell script

Notes,it will pass 3 parameters

cd /d %~dp0

powershell -file ".\autobackupSiteCollection.ps1" "http://sp/sites/UAT" "F:\SharePoint 2013 Backup" "UAT"

@pause

回到顶部

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

Finally,Run the Batch File to start Backing up the site collection immediately.Or use Windows Task Scheduler to schedule it.

回到顶部

Step 4:Schedule a Batch File to Run automatically

There are occasions where you might need to schedule to run a batch file automatically in your windows server.So I will show you how to do it.

  • Click Start and under Search,type in "任务计划程序" and Open it
  • Select "创建基本任务" from "操作"Menu

  • Under "创建基本任务",type in Name you like and click Next

  • From the "触发器" Select,Select the option you like and click Next

  • I chose "每日" and Click Next,which brought me to this screen
  • Then click on "开始程序" and click next
  • Next Select "浏览" and Select the batch file you like run

  • Finally,Click on Finsh to create a Task

Now,that we have created a Task,we have to make sure it runs highest Privilege. we have to make sure that when you run the file it not should fail.

  • Right Click the task you just created and Select Property
  • Click on "使用最高权限运行" then click OK.

 

 

 

 

 

 

88x31.png
本博客为木宛城主原创,基于Creative Commons Attribution 2.5 China Mainland License发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名木宛城主(包含链接)。如您有任何疑问或者授权方面的协商,请给我留言。
分类: SharePoint In Action
标签: SharePoint, SharePoint 2013, PowerShell, Disaster Recovery

本文转自木宛城主博客园博客,原文链接:http://www.cnblogs.com/OceanEyes/p/sharepoint-2013-how-to-backup-site-collection-automatically-with-a-powershell-and-batch-file.html,如需转载请自行联系原作者

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/914.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息