SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script
2015-03-02 00:00
537 查看
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.
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
cd /d %~dp0
powershell -file ".\autobackupSiteCollection.ps1" "http://sp/sites/UAT" "F:\SharePoint 2013 Backup" "UAT"
@pause
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.
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 parameterscd /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.
相关文章推荐
- SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script
- SharePoint 2013 How to Backup Site Collection Automatically With a PowerShell Script
- SharePoint 2010 Cookbook: Backup a Site Collection Automatically with a PowerShell Script
- SharePoint 2013 Backup Farm Automatically With a Powershell and Windows Task Schedule
- SharePoint 2013 Backup Farm Automatically With a Powershell and Windows Task Schedule
- SharePoint 2013 Backup Farm Automatically With a Powershell and Windows Task Schedule
- SharePoint 2013 Step by Step——How to Create a Lookup Column to Another Site(Cross Site)
- How To Create AD Users with PowerShell
- how to solve Error cannot add duplicate collection entry of type add with unique key attribute 'valu
- How To Optimize Your Site With HTTP Caching(中、英文版)
- SharePoint 2013 Step by Step—— How to Upload Multiple Documents in Document Library
- How to automatically backup SourceSafe?
- HOW TO Load a Custom Script with Dependencies
- How to create custom navigation menu in SharePoint with XML data source 使用XML数据源在SharePoint创建自定义导航菜单
- SharePoint 2007 - /_layouts and how to create pages that run in site context
- How to deal with Windows Schedule&Task by PowerShell in Win7
- Howto Backup PostgreSQL Databases Server With pg_dump command
- How to fix: Recieve error "Unable to display this Web Part" on Data Views after restoring site from backup
- How to write comment in PowerShell Script
- How to Backup Partition of Range Partitioned Table with Local Indexes