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

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="$dirBackupLog.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.

 

 

 

原文地址:https://www.cnblogs.com/OceanEyes/p/sharepoint-2013-how-to-backup-site-collection-automatically-with-a-powershell-and-batch-file.html