How to specify differential backup option?

Cataster 661 Reputation points
2021-08-21T17:26:03.767+00:00

Im implementing automated log shipping solution, and came accross differential backups. Im interested in this feature, however, the Backup-SqlDatabase documentation has no information about it... Is there a way to specify this option somewhere?

Here's my script so far:

#First, create a 1-time full backup of the the primary instance DB that we are interested in shipping over to the secondary server
Get-SqlDatabase -ServerInstance $PrimaryServerInstance -Database $DatabaseName | Backup-SqlDatabase -BackupFile "$BackupFilePath\$DatabaseName.bak"

#Restore the full .bak 1-time
Restore-SqlDatabase -ServerInstance $SecondaryServerInstance -Database $DatabaseName -BackupFile "$BackupFilePath\$DatabaseName.bak" -AutoRelocateFile -NoRecovery -PassThru

#Then, create a backup of the the primary instance DB transaction log that we are interested in shipping over to the secondary server
Get-SqlDatabase -ServerInstance $PrimaryServerInstance -Database $DatabaseName | Backup-SqlDatabase -BackupFile "$BackupFilePath\$DatabaseName.trn" -BackupAction Log

#Next, restore the transaction log for the DB on the secondary server/instance (scheduled at some point)
Restore-SqlDatabase -ServerInstance $SecondaryServerInstance -Database $DatabaseName -BackupFile "$BackupFilePath\$DatabaseName.trn" -RestoreAction Log
Windows for business | Windows Server | User experience | PowerShell
SQL Server | Other
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-08-21T17:38:09.257+00:00

    Hi @Cataster ,

    Backup-SqlDatabase -ServerInstance "Computer\Instance" -Database "MainDB" -Incremental  
    

    This command creates a differential backup of the database 'MainDB' to the default backup location of the server instance 'Computer\Instance'. The backup file is named "MainDB.bak".

    Source: https://learn.microsoft.com/en-us/powershell/module/sqlserver/backup-sqldatabase?view=sqlserver-ps#example-9--create-a-differential-backup

    It's not part of a log file backup.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


6 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-08-21T19:55:17.703+00:00

    Hi @Cataster ,

    the full backup contains the full db and the log files.
    The differential backup contains the db and log files but only the changes since full backup.
    The log backup contains just the log backups since db full backup and not the db file(s).

    The full backup is the base to restore the full DB.
    The differential backup is smaller than a full backup but contains db and log files.
    The log backup is the smallest/fastest backup because only the transaction logs since full or differential backup are in the backup.

    Lets assume the following backups (F= Full Backup, D = Differential Backup, L = Log Backup):
    F1 - L1 - L2 - L3 - D1 - L4 - L5 - L6

    If you want to restore the full db you need F1 + D1 + L4 +L5 + L6 (L1, L2 and L3 aren't needed because D1 contains everything that changed since F1).

    I don't think that differential backups are a good option for SQL Log Shipping. I know SQL log shipping using one Full Backup and than Log Backups in a short interval.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    1 person found this answer helpful.

  2. Erland Sommarskog 121.4K Reputation points MVP Volunteer Moderator
    2021-08-21T20:50:26.227+00:00

    If you want to implement a log-shipping solution, I don't really see much use for the differential backup. For log shipping you typically start with restoring a full backup, and the you apply transaction logs from there, using the option WITH STANDBY, so that you can apply more logs.

    Possibly, if you have a very large database you start with a full backup, but by the time you have it on the log-shipping server it is quite old, and for some reason you don't want to apply those logs, so you take a differential, to get a starting point more closer in time. But it does not sound compelling to me.

    1 person found this answer helpful.

  3. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2021-08-21T17:29:49.853+00:00

    Hi @Cataster ,

    maybe this helps: https://learn.microsoft.com/en-us/powershell/module/sqlserver/backup-sqldatabase?view=sqlserver-ps#example-9--create-a-differential-backup

    Backup-SqlDatabase -ServerInstance "Computer\Instance" -Database "MainDB" -Incremental  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  4. Cataster 661 Reputation points
    2021-08-22T19:56:26.847+00:00

    @Andreas Baumgarten
    I opted to use dbatools Invoke-DbaDbLogShipping command.
    I couldnt use it before because of the network shared drive requirement, but @Erland Sommarskog had suggested I can emulate my machine as a network drive, which didnt cross my mind last week

    One thing I dont understand is how is the 15 min interval in the example determined...The interval is set to "1" but I think 1 is used to specify a daily interval frequency TYPE, not interval TIME.

    So whats the parameter to increase the interval time from 15 mins to say 30 mins or 2 hours?

    Also, for now I utilized the example given and just changed the database to whatever my database is. i checked and looks like the backup/copy/restore operations are created, but Im seeing these errors:

    Backup Agent Log:

    Backing up transaction log. Primary Database: 'Test14'<c/> Log Backup File: 'C:\Users...\Documents\DB Log Shipping\Backups\Local\Test14\Test14_20210822193000.trn'<nl/>
    Error: Could not log history/error message.(Microsoft.SqlServer.Management.LogShipping) ***<nl/>
    *** Error: Failed to convert parameter value from a SqlGuid to a String.(System.Data) ***
    *** Error: Object must implement IConvertible.(mscorlib)
    First attempt to backup database 'Test14' to file 'C:\Users...\Documents\DB Log Shipping\Backups\Local\Test14\Test14_20210822193000.trn' failed because Cannot open backup device 'C:\Users...\Documents\DB Log Shipping\Backups\Local\Test14\Test14_20210822193000.trn

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.