none
Incremental Backup of Sitecollections with PowerShell RRS feed

  • Question

  • Hello,

    I wanted to Backup a Site Collection once a week with a full-backup and every other day of the week with an incremental or differential backup, but I think that isn't possible that easily, Right? I'm relatively new to SharePoint and PowerShell, so please excuse my lack of Knowledge here.

    If it isn't possible with an easy command: Do you have any idea how I could manage that?

    I thought about looping through all sites and getting the data when it was last modified to know what it has to backup, but that's all I got:

    if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
        Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    }
    
    $sites = Get-SPSite -Limit All | Get-SPWeb -Limit All
    foreach ($site in $sites) {
    ??
    }
    }
    $sites.Dispose()

    Im really overchallenged as you can see. It isn't many code and it isn't nice, but I really don't know how to do it and I didn't find anything useful for me.

    Thank you for your help.

    Best Regards,

    Robin

    _________________________________________________________________________________

    EDIT:

    Hello everybody,

    thanks for your answers, but I think I have to write something, because everyone is just replying to the first section of my Question. I know About the Backup-SPfarm and Backup-Spsite commands and I know, that non of These commands can do an incremental backup of just one sitecollection.

    So, if this isn't possible with an easy one-liner, my second Question was: Is it possible with a PowerShell script that loops through all sites of the sitecollection?

    Best regards,

    Robin

    • Edited by N3ubi Wednesday, September 11, 2019 6:59 AM main question
    Tuesday, September 10, 2019 1:18 PM

Answers

  • Thanks to both of you.

    So I was Right, that this isn't possible with an easy one-liner, but can anyone think About how to do it otherwise? Do you think it could be possible with a PowerShell Script, that loops through all sitecollection sites and just Exports the items or objects that were recently modified?

    Or do you think it isn't possible and I have no choice, but backup the entire farm or database?

    Best regards,

    Robin

    Hi N3ubi,

    Yes, about your concern of Incremental Back of of Site Collection, there is no command with can achieve this directly.

    About looping the all sites in a site collection with PowerShell script, please refer the following code snippet:

    param($url = $(Read-Host -prompt "Root Site Collection Path"))
    
    #Get the PowerShell Snapin
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    
    #Get Root Site
    $root = Get-SPSite $url
    
    #If site was found / valid
    if($root -ne $null)
    {
    
         foreach($subSite in $root.AllWebs)
          {
           $subSiteTitle = $subSite.Title
                    Write-Host $subSiteTitle -ForegroundColor Magenta
                    $subSiteURL = $subSite.Url
                    Write-Host $subSiteURL -ForegroundColor Cyan
           $subSite.Dispose()
          }
    
            $root.Dispose()
    
    }

    Reference:

    PowerShell: Loop Through All Sub Sites in a Site Collection

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by N3ubi Wednesday, September 11, 2019 8:28 AM
    Wednesday, September 11, 2019 7:43 AM
  • Thanks to both of you.

    So I was Right, that this isn't possible with an easy one-liner, but can anyone think About how to do it otherwise? Do you think it could be possible with a PowerShell Script, that loops through all sitecollection sites and just Exports the items or objects that were recently modified?

    Or do you think it isn't possible and I have no choice, but backup the entire farm or database?

    Best regards,

    Robin

    The SPSite class includes a property named LastContentModifiedDate which is a System.DateTime structure that contains the date and time when the content was last changed: you could write a PowerShell script that loops through all the site collections, compare the value of this property against an established threshold and then backup or not the site collection.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    • Edited by Luigi BrunoMVP Wednesday, September 11, 2019 7:45 AM
    • Marked as answer by N3ubi Wednesday, September 11, 2019 9:53 AM
    Wednesday, September 11, 2019 7:44 AM
  • Thanks to Jerry Zy and Luigi Bruno.

    I wrote this PowerShell Script to Export the sites where the Content is newer than two days:

    $date = Get-Date -UFormat "%Y_%m_%d"
    
    $sites = Get-SPSite -Limit All
    foreach ($subsite in $sites.AllWebs) {
    	if ($subsite.LastItemModifiedDate -gt $date.AddDay(-2)) {
    		Export-SPWeb $subsite.Url -path $SitePath
    		}
    	$subsite.Dispose()
    }
    $sites.Dispose()

    I think that works and is a little bit of an incremental backup.

    Best regards,

    Robin/N3ubi


    • Marked as answer by N3ubi Wednesday, September 11, 2019 10:46 AM
    • Edited by N3ubi Wednesday, September 11, 2019 12:35 PM edited property
    Wednesday, September 11, 2019 10:43 AM

All replies

  • Place the below Script in Task Scheduler for Incremental backup and set it to every other day.

    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    Set-ExecutionPolicy -ExecutionPolicy "Unrestricted" -Force
    Backup-SPfarm -BackupMethod Differential -Directory \\YourServer\SPBackup
    Thanks & Regards,

     

    sharath aluri

    Tuesday, September 10, 2019 2:03 PM
  • Thanks for the Reply.

    But I don't want a backup of the entire farm, isn't that what your script is doing? I just want to backup one site Collection with an incremental or differential backup.

    Best Regards

    Robin

    Tuesday, September 10, 2019 2:22 PM
  • Yes that's true it's doing for an entire farm. but if you need backup of a Site Collection use the below command, but you won't be having incremental & Full at site collection level. It's just always full backup of a Site Collection. below article for your reference.

    Backup-SPSite https://server_name/sites/site_name -Path C:\Backup\site_name.bak

    https://docs.microsoft.com/en-us/powershell/module/sharepoint-server/backup-spsite?view=sharepoint-ps

    Thanks & Regards,


    sharath aluri

    Tuesday, September 10, 2019 2:34 PM
  • Hi Robin.

    If you want to backup a site collection, you have to use the Backup-SPSite cmdlet, but it doesn't allow you to perform a differential or incremental backup.
    The Backup-SPFarm cmdlet allows you to perform a full or differential backup of the whole farm or of a specific item, such as a content database: look at the below example

    Backup-SPFarm -Directory <BackupFolder> -BackupMethod {Full | Differential} -Item <ContentDatabaseName> [-Verbose]

    Remember that if the content database contains more than a site collection, you'll have a backup of all the site collections: this cmdlet does not allow you to backup a specific site collection.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    Tuesday, September 10, 2019 2:37 PM
  • Thanks to both of you.

    So I was Right, that this isn't possible with an easy one-liner, but can anyone think About how to do it otherwise? Do you think it could be possible with a PowerShell Script, that loops through all sitecollection sites and just Exports the items or objects that were recently modified?

    Or do you think it isn't possible and I have no choice, but backup the entire farm or database?

    Best regards,

    Robin

    Wednesday, September 11, 2019 6:19 AM
  • Hi N3ubi,

    Agreed with Sharath and Luigi's suggestion, Back-SPSite command not provide the Incremental Back up option, this option only available in Backup-SPFarm command, please refer the official document for the details of these two commands:

    Backup-SPSite:

    Backup-SPFarm

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    Wednesday, September 11, 2019 6:37 AM
  • Thanks to both of you.

    So I was Right, that this isn't possible with an easy one-liner, but can anyone think About how to do it otherwise? Do you think it could be possible with a PowerShell Script, that loops through all sitecollection sites and just Exports the items or objects that were recently modified?

    Or do you think it isn't possible and I have no choice, but backup the entire farm or database?

    Best regards,

    Robin

    Hi N3ubi,

    Yes, about your concern of Incremental Back of of Site Collection, there is no command with can achieve this directly.

    About looping the all sites in a site collection with PowerShell script, please refer the following code snippet:

    param($url = $(Read-Host -prompt "Root Site Collection Path"))
    
    #Get the PowerShell Snapin
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
    
    #Get Root Site
    $root = Get-SPSite $url
    
    #If site was found / valid
    if($root -ne $null)
    {
    
         foreach($subSite in $root.AllWebs)
          {
           $subSiteTitle = $subSite.Title
                    Write-Host $subSiteTitle -ForegroundColor Magenta
                    $subSiteURL = $subSite.Url
                    Write-Host $subSiteURL -ForegroundColor Cyan
           $subSite.Dispose()
          }
    
            $root.Dispose()
    
    }

    Reference:

    PowerShell: Loop Through All Sub Sites in a Site Collection

    Thanks

    Best Regards


    Please remember to mark the replies as answers if they helped. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    SharePoint Server 2019 has been released, you can click here to download it.
    Click here to learn new features. Visit the dedicated forum to share, explore and talk to experts about SharePoint Server 2019.

    • Marked as answer by N3ubi Wednesday, September 11, 2019 8:28 AM
    Wednesday, September 11, 2019 7:43 AM
  • Thanks to both of you.

    So I was Right, that this isn't possible with an easy one-liner, but can anyone think About how to do it otherwise? Do you think it could be possible with a PowerShell Script, that loops through all sitecollection sites and just Exports the items or objects that were recently modified?

    Or do you think it isn't possible and I have no choice, but backup the entire farm or database?

    Best regards,

    Robin

    The SPSite class includes a property named LastContentModifiedDate which is a System.DateTime structure that contains the date and time when the content was last changed: you could write a PowerShell script that loops through all the site collections, compare the value of this property against an established threshold and then backup or not the site collection.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    • Edited by Luigi BrunoMVP Wednesday, September 11, 2019 7:45 AM
    • Marked as answer by N3ubi Wednesday, September 11, 2019 9:53 AM
    Wednesday, September 11, 2019 7:44 AM
  • Thanks to Jerry Zy and Luigi Bruno.

    I wrote this PowerShell Script to Export the sites where the Content is newer than two days:

    $date = Get-Date -UFormat "%Y_%m_%d"
    
    $sites = Get-SPSite -Limit All
    foreach ($subsite in $sites.AllWebs) {
    	if ($subsite.LastItemModifiedDate -gt $date.AddDay(-2)) {
    		Export-SPWeb $subsite.Url -path $SitePath
    		}
    	$subsite.Dispose()
    }
    $sites.Dispose()

    I think that works and is a little bit of an incremental backup.

    Best regards,

    Robin/N3ubi


    • Marked as answer by N3ubi Wednesday, September 11, 2019 10:46 AM
    • Edited by N3ubi Wednesday, September 11, 2019 12:35 PM edited property
    Wednesday, September 11, 2019 10:43 AM
  • Thanks to Jerry Zy and Luigi Bruno.

    I wrote this PowerShell Script to Export the sites where the Content is newer than two days:

    $date = Get-Date -UFormat "%Y_%m_%d"
    
    $sites = Get-SPSite -Limit All
    foreach ($subsite in $sites.AllWebs) {
    	if ($subsite.LastContentModifiedDate -gt $date.AddDay(-2)) {
    		Export-SPWeb $subsite.Url -path $SitePath
    		}
    	$subsite.Dispose()
    }
    $sites.Dispose()

    I think that works and is a little bit of an incremental backup.

    Best regards,

    Robin/N3ubi

    Please note that $subsite represents an object of type SPWeb, which is a web (site), not a site collection: that object does not have the LastContentModifiedDate property, but it has the LastItemModifiedDate property.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    Wednesday, September 11, 2019 11:31 AM
  • Oh yeah, you're Right, thank you.

    Best regards,

    Robin

    Wednesday, September 11, 2019 12:32 PM
  • Oh yeah, you're Right, thank you.

    Best regards,

    Robin

    You're welcome.

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE

    Wednesday, September 11, 2019 6:56 PM