Problem: How to get
information related to database Files, log files from a backup set.
Sometimes
we need to peek into the database backup and find the backup version of a
consolidated backup or backup date, compatibility and others. It also gives us
the Log Sequence Numbers(LSN) numbers to identify the full backup and
differential, gives us the backup type of database, who did and where did the
backup happen.
If
explored this has a lot of information and consuming this into an application
can be helpful to audit the backups.
NOTE:
An optional WITH FILE is issued when you want a specific
backup set number, if nothing is specified it gives the first backup set on the
file.
Example:
In this
article I am going to show you how we can retrieve header information of
database, logs from a backup set on a disk.
I have backed up a database few times both differential and full
to show you the difference between the database file backup version and backed
up dates.
Example 1:
--
To view the details of the database version backuped up and when it is backed
up.
RESTORE HEADERONLY FROM
DISK = 'C:\Temp\Db.bak'
WITH FILE =1
– this shows information about the first file on the disk
RESTORE HEADERONLY FROM
DISK = 'C:\Temp\Db.bak'
WITH FILE =2–
this shows information about the second file on the disk
RESTORE HEADERONLY FROM
DISK = 'C:\Temp\Db.bak'
WITH FILE =3–
this shows information about the third file on the disk
OUTPUT:
The above
output shows the different date and times when the backup occurred.
Featured
Script to retrieve selected information.
DECLARE @HeaderInformation TABLE (
BackupName NVARCHAR(128),
BackupDescription NVARCHAR(255),
BackupType SMALLINT,
ExpirationDate DATETIME,
Compressed TINYINT,
Position SMALLINT,
DeviceType TINYINT,
UserName NVARCHAR(128),
ServerName NVARCHAR(128),
DatabaseName NVARCHAR(128),
DatabaseVersion INT,
DatabaseCreationDate DATETIME,
BackupSize NUMERIC(20, 0),
FirstLSN NUMERIC(25,
0),
LastLSN NUMERIC(25, 0),
CHECKPOINTLSN NUMERIC(25, 0),
DatabaseBackupLSN NUMERIC(25, 0),
BackupStartDate DATETIME,
BackupFinishDate DATETIME,
SortOrder SMALLINT,
CodePage
SMALLINT,
UnicodeLocaleId INT,
UnicodeComparisonStyle INT,
CompatibilityLevel TINYINT,
SoftwareVendorId INT,
SoftwareVersionMajor INT,
SoftwareVersionMinor INT,
SoftwareVersionBuild INT,
MachineName NVARCHAR(128),
Flags INT,
BindingID UNIQUEIDENTIFIER,
RecoveryForkID UNIQUEIDENTIFIER,
Collation NVARCHAR(128),
FamilyGUID UNIQUEIDENTIFIER,
HasBulkLoggedData BIT,
IsSnapshot BIT,
IsReadOnly BIT,
IsSingleUser BIT,
HasBackupChecksums BIT,
IsDamaged BIT,
BeginsLogChain BIT,
HasIncompleteMetaData BIT,
IsForceOffline BIT,
IsCopyOnly BIT,
FirstRecoveryForkID UNIQUEIDENTIFIER,
ForkPointLSN NUMERIC(25, 0) NULL,
RecoveryModel NVARCHAR(60),
DifferentialBaseLSN NUMERIC(25, 0) NULL,
DifferentialBaseGUID UNIQUEIDENTIFIER,
BackupTypeDescription NVARCHAR(60),
BackupSetGUID UNIQUEIDENTIFIER NULL,
CompressedBackupSize BIGINT,
containment TINYINT not NULL,
KeyAlgorithm NVARCHAR(32),
EncryptorThumbprint VARBINARY(20),
EncryptorType NVARCHAR(32)
)
INSERT INTO @HeaderInformation
EXEC
('RESTORE
HEADERONLY FROM DISK = ''C:\Temp\Db.bak'' WITH FILE =3') -- WITH FILE IS OPTIONAL AND to check the particular backup
set on the file.
SELECT DatabaseName,
CASE
WHEN Backuptype = 1 THEN 'Database'
WHEN Backuptype = 2 THEN 'Transaction log'
WHEN Backuptype = 4 THEN 'File'
WHEN Backuptype = 5 THEN 'Differential database'
WHEN Backuptype = 6 THEN 'Differential file'
WHEN Backuptype = 7 THEN 'Partial'
WHEN Backuptype = 8 THEN 'Differential partial'
END AS BackupType,
DatabaseCreationDate,
Backupsize/1024 as
[Backupsize(MB)],
backupstartdate,
backupfinishdate,
compatibilitylevel,
position as poisitiononthebackupset,
firstlsn,
lastlsn,
DifferentialBaseLSN
FROM @HeaderInformation