SharePoint 2010 Cookbook: Backup a Site Collection Automatically with a PowerShell Script

One of the most important things that all SharePoint users, especially administrators, should do regularly is back up data. Nevertheless, it is often overlooked by many people just because the process might seem to take hours, and we don’t have the time or the inclination to run backups on a regular basis. In recognition of this behavior, what if there was a script for backing up SharePoint data that could be scheduled to run automatically? Now that’s a whole different story! In this post, I will demonstrate how to automatically back up a SharePoint 2010 site collection using a PowerShell script.

Challenge:

How can I back up a SharePoint 2010 site collection automatically on a regular basis?

Solution:

For the sake of this exercise, I’m going to assume that you already have a backup location setup. Let’s say it’s “C:BackupMySite”.

First, verify that you possess the following permissions:

  • SharePoint Farm Administrator.
  • Local Server Administrator on all Web servers.
  • db_owner permission on the content database.
  • Full Control permission on the backup folder.

Step 1: Create a PowerShell script for backing up a site collection (BackupSite.ps1):

Add-PsSnapin Microsoft.SharePoint.PowerShell

Start-SPAssignment -Global            # This cmdlet takes care of the disposable objects to prevent memory leak.

$mySite="http://mySite"            # Replace with your site collection URL
$backupLocation="C:BackupMySite"        # Replace with your backup location
$logFile="$backupLocationBackupLog.log"    # Replace with your desired log file location

$today=Get-Date -format "MM-dd-yyyy HH.mm.ss"    # Get current date and format it to avoid invalid characters such as "/" and ":"

write-Host Start backing up $mySite to $backupLocation
try
{
    # Create a new backup file and name it based on current date. If you want to create only 1 backup file and overwrite it each time the backup is run, you can replace "$today.bak" with your desired file name.
    Backup-SPSite -Identity $mySite -Path $backupLocation$today.bak -force -ea Stop

    write-Host Backup succeeded.

    # Write success message to the log file
    write "$today    $mySite successfully backed up.">>$logFile
}
catch        # If the process failed
{
    write-Host Backup failed. See $logFile for more information.

    # Write error message to the log file
    write "$today    Error: $_">>$logFile
}

Stop-SPAssignment -Global

Remove-PsSnapin Microsoft.SharePoint.PowerShell

write-Host "Finished script."

Basically, the above script will do the following:

  • Assign all the needed information to start back up.
  • Try to create a new backup and name the file based on the 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 script (RunBackup.bat):

cd /d %~dp0
powershell -file    ".BackupSite.ps1"

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

Step 4: Run the batch file to start backing up the site collection immediately, or use Windows Task Scheduler to schedule it.

I have attached both the script and the batch file at the end of this post for your convenience.

Notes:

  • This method works with both SharePoint 2010 Foundation and Server.
  • While performing the backup, the SharePoint site collection will be set to Read-Only to prevent data corruption, so it’s recommended that you run this script during off-peak hours.

See Also:


jakistra
wrote
re: SharePoint 2010 Cookbook: Backup a Site Collection Automatically with a PowerShell Script
on Mon, Mar 14 2011 1:59 PM

in the Backup-SPSite cmdlet, what does the “-ea Stop” do?

Thanks.

SharePoint Online

The cloud parts are functional components that extend your SharePoint Online environment in Microsoft 365.

Supports Classic and Modern sites for SharePoint Online/Microsoft 365

Small Business Pricing and Discounts

SharePoint

Top SharePoint Online Products

Experience greater power and savings by bundling our SharePoint apps and cloud parts.


Calendar Plus


Carousel


Employee Directory Plus


Org Chart Plus


Simple Search


Tabify


Tree View

 

On-Premises Only

These web parts extend SharePoint beyond its out-of-the-box capabilities by tailoring it to your requirements with Bamboo Solution’s growing portfolio of SharePoint Web Parts.

SharePoint 2016, 2019, 2022 – Classic Pages Only

SharePoint

Top On-Premises Only Products

Experience greater power and savings by bundling our SharePoint apps and web parts.


Calendar Plus


Data Viewer


Password Change


Password Expiration


Password Reset

 

Our team of Microsoft 365 Technology Consultants helps you get the most out of your Microsoft technology, we have the best Microsoft 365 talent to streamline your organization.

Consulting to Streamline Your Department

M365 Plus

Managed Services

Microsoft 365

Consulting to Streamline Your Department


Human Resources


Information Technology


Marketing Campaigns


Healthcare


Sales

 

Our Consultants Have What You Need

Federal Contractors