Cleaning up Sharepoint 2013 backups

Sharepoint is pretty horrible when it comes to backups, but presuming that you're doing Farm backups rather than just your content database, you'll have to deal with them.

I do one full backup per week and differentials in between from Task Scheduler. This generates a lot of data that needs cleaning up, so here's a bit of script. Most of this is directly lifted from a script by Meat Head Nerd which I changed to look at the full backups rather than just age of backup.

This will remove all full and differential backups prior to the number of full backups you want to keep.


 # Number of full back ups you want to keep  
 $numfullbackups = 1  
 # Import the Sharepoint backup report xml file  
 $spbrtoc = “c:\temp\spbrtoc.xml”  
 [xml]$sp = gc $spbrtoc  
 # Find the full backups in spbrtoc.xml  
 $fullbackups = $sp.SPBackupRestoreHistory.SPHistoryObject | ? { $_.SPBackupMethod -eq 'Full' }  
 # Get the start time of the last full backup  
 $lastfullstarttime = $fullbackups[$numfullbackups-1].SPStartTime  
 Write-host $lastfullstarttime  
 # Select all the backups that finished prior to the last full back up you want to keep  
 $old = $sp.SPBackupRestoreHistory.SPHistoryObject | ? { $_.SPFinishTime -lt $lastfullstarttime }  
 if ($old -eq $Null) { write-host “No backups before required full backups to be removed.`n” ; break}  
 # Delete the old backups from the Sharepoint backup report xml file  
 $old | % { $sp.SPBackupRestoreHistory.RemoveChild($_) }  
 # Delete the physical folders in which the old backups were located  
 $old | % { Remove-Item $_.SPBackupDirectory -Recurse }  
 # Save the new Sharepoint backup report xml file  
 $sp.Save($spbrtoc)  

Comments

Popular posts from this blog

Avoid Microsoft Intune if you use G-Suite and Android

DFS "Waiting for Initial Replication"

Setting Wallpaper for a Remote Desktop Session