Post

Site Collection backup with cleanup job
We use PowerShell to backup our SharePoint 2010 farms and we can select how many days we want to save the farm backup. We didn’t had this yet for site collections so i created a script that backups all site collections from SharePoint 2010 and deletes the folder after X number of days. Backup Script […]

We use PowerShell to backup our SharePoint 2010 farms and we can select how many days we want to save the farm backup. We didn’t had this yet for site collections so i created a script that backups all site collections from SharePoint 2010 and deletes the folder after X number of days.

Backup Script

I first created a backup location on my hard drive and created the necesarry .ps1 files.

image

Copy and paste the following script in ‘SiteCollBackup.ps1’

Add-PsSnapin Microsoft.SharePoint.Powershellfunction BackupSiteCollections

{

#Change below variables

$Location = "D:\SiteCollectionBackup\Backup\"

#You don’t have to change below variables

$Backupname = "SiteCollectionBackup_"

$date = get-date

$today = $date.ToString("ddMMyyyy")

$fileLocation = "$Location$Backupname$today"

[IO.Directory]::CreateDirectory("$filelocation")

$sitecollections = get-spsite -limit all

foreach($sitecollection in $sitecollections)

{

$tempname = $($sitecollection.url)

$tempname = $tempname.replace(":","_")

$name = $tempname.replace("/","")

Backup-SPSite $sitecollection -path "$($fileLocation)\$($name).bak" -force

}

}

BackupSiteCollections 

Change the backup location in this script

Cleanup Job

Copy paste or create a new empty .ps1 file and paste the following script in the .ps1 file

This script will delete the folders that are older then $Days

function CleanUpBackupSiteCollections{

#Change below variables

$Location = "D:\SiteCollectionBackup\Backup\"

$Days = 2

#You don’t have to change below variables

$date = get-date

$LastWrite = $date.AddDays(-$Days)

$Folders = Get-Childitem $Location | Where {$_.LastWriteTime -le "$LastWrite"}

foreach ($Folder in $Folders)

{

if ($Folder -ne $NULL)

{

Remove-Item $Folder.FullName -recurse -Confirm:$false | out-null

}

}

}

CleanUpBackupSiteCollections

Shedule task

We want to run the backup every day and delete folders older then X days. We can do this to run the above scripts using task sheduler

Use the following script to add these rules automatically.

function CreateTask{
#Change below variables$userdomain = “Domain name”$username = “User Account that has sufficient rights”$locationBackup = "D:\SiteCollectionBackup\SiteCollBackup.ps1"$locationCleanUp = "D:\SiteCollectionBackup\SiteCollBackupCleanup.ps1"

#You don’t have to change below variables

$TaskPassword = Read-Host "Please enter the password for $userdomain\$username" -AsSecureString

$TaskPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($TaskPassword))

schtasks /create /tn "SiteCollectionBackup" /tr "$env:windir\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file $locationBackup" /sc daily /st 02:00 /ru $userdomain\$username /rp $TaskPassword /RL Highest| Out-Null

schtasks /create /tn "SiteCollectionCleanUp" /tr "$env:windir\system32\windowspowershell\v1.0\powershell.exe -noninteractive -file $locationCleanUp" /sc daily /st 04:00 /ru $userdomain\$username /rp $TaskPassword /RL Highest| Out-Null

}

CreateTask

Run this script when you already have created the backup and cleanup job.

image

View if the tasks have been created

image

Start the backup task to view if the backup has been created.

Results

The backups will be located under D:\SiteCollectionBackup\Backup

image

The .bak files will be created like

http_<WebApp>.<Domain>.<extension><SiteCollection>

image

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Archive