Quantcast
Channel: Powershell – ..:: I like SharePoint ::..
Viewing all articles
Browse latest Browse all 41

SharePoint Backup Script

$
0
0

SharePoint offers inbuilt Backup und Restore functionality. It’s always good to have a backup. There are a lot of 3rd Party tools available. But in case you would like to check how to deal with it using Powershell Script and SharePoint native possibilities this post might be interesting for you. Microsoft posted some best practices to this topic of Backup and restore.

SharePoint Backup Considerations

This task of backups should be automated and running fluid. You should not do it manually. You should think about what kind of backups do you plan (full or differential) and how often. In my case we do a full backup each Saturday and all others day we perform a differential backup. A differential backup always need at least one full backup.

The script should run as windows tasks each day. If you create the windows task, keep in mind, to run this task with “highest privileges”.

These backup need storage. Depending on how much data you have in your SharePoint Farm, it can increase rapid. Let’s have a look at the script parts.

Check if you have enough Space before starting backup

Check your storage and exit script if there is not enough space.

#check Storage
$storage = Get-WmiObject Win32_LogicalDisk -ComputerName computerName | where { $_.DeviceID -eq "F:"} 
$freespace = $storage.FreeSpace /1024 /1024 /1024

#exit script if not enough space
if($freespace -lt 300)
{
    #Send Mail Function to inform aboout not enough space
    break
}

This will exit script. You can before send an e-mail to inform you or create a ticket in your ticket system.

Create Backups

#Check Date
$today = Get-Date

if($today.DayOfWeek -eq "Saturday")
    {
    #Creation of the FULL backup
    Write-Host "Backing up the Farm Full... " -f Yellow
    try{
        Backup-SPFarm -Directory "\\Server\SP19_Backup\" -BackupMethod Full -Percentage 5 -Verbose -ErrorAction Stop
        Write-Host "Congratulations! The FULL backup completed successfully!" -f Green
        #Send Mail to inform about success

    }
    catch
    {
        #Send Mail or create ticket about error
    }
}
else{
    #Creation of the Differential backup
    Write-Host "Backing up the Farm Differential... " -f Yellow
    try{
        Backup-SPFarm -Directory "\\\Server\SP19_Backup\" -BackupMethod Differential -Percentage 5 -Verbose -ErrorAction Stop
        Write-Host "Congratulations! The FULL backup completed successfully!" -f Green
        #Send Mail to inform about success
    }
    catch
    {
        #Send Mail or create ticket about error
    }
}

With this script part you create a full backup on Saturday and a differential backup the other days.

Cleanup process – remove backups older than 14 days

#Delete Backups older than 14 Days and remove reference from the XML file
$Now = Get-Date
$Days = "14"
$TargetFolder = "\\Servername\SP19_Backup\"
$LastWrite = $Now.AddDays(-$days)

#Get a list of backup folders that begin with "spbr" and were modified more than 14 days ago
$Folders = get-childitem $TargetFolder -include spbr* -exclude *.* -recurse | Where {$_.LastWriteTime -le "$LastWrite"} 

try{
    $xml = [XML] Get-Content -Path  $TargetFolder\spbrtoc.xml

    foreach ($Folder in $Folders)
    {
        if ($Folder)
        {
            write-host "Deleting Folder $Folder" -foregroundcolor "Red"
            Remove-Item $Folder -recurse | out-null
 
            $FolderName = split-path $Folder -Leaf
            $nodes = $xml.SelectNodes("SPBackupRestoreHistory//SPHistoryObject[SPDirectoryName='$FolderName']")
            [void]$xml.SPBackupRestoreHistory.RemoveChild($nodes.item(0));
        }
    } 
 
    $xml.save("$TargetFolder\spbrtoc.xml")
    #Send Mail or create ticket about success
}
catch
{
    #Send Mail or create ticket about error
}

With this script you can remove older backups from your storage. I got this script from this post.


Viewing all articles
Browse latest Browse all 41

Trending Articles