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.
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:
Notes,it will pass 3 parameters
cd /d %~dp0
powershell -file ".\autobackupSiteCollection.ps1" "http://sp/sites/UAT" "F:\SharePoint 2013 Backup" "UAT"
@pause
Finally,Run the Batch File to start Backing up the site collection immediately.Or use Windows Task Scheduler to schedule it.
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.
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.