前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script

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

作者头像
用户1161731
发布2018-01-11 15:47:13
7810
发布2018-01-11 15:47:13
举报
文章被收录于专栏:木宛城主

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

代码语言:js
复制
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.
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014-03-22 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 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
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档