SQL Interview Questions On Database Backups
SQL Interview Questions On Database Backups
https://www.sqlshack.com/sql-interview-questions-on-database-backups-restores-and-recovery-part-i/
So far, we’ve discussed a lot about database backup-and-restore process. The backup database command is an online database copy of the SQL Server
database and restore database command gives an option to test the consistency and integrity of the backup file.
As we all know, the backup database command bound with many database options. Indeed, it facilitates the execution of specific backup database command
that meets the business requirement.
The backup database is further classified into two preliminary backup types
1. Backup Database
2. Backup Log
Let’s deep dive and review each of the database backup command topics to get a better understanding of what it is all about. In this article, we will find an
answer for FAQs about the SQL Server database backup. We will learn more about database backup.
Data is always the target and remain vulnerable for various threats
Un-reliable on hardware and software programs
Importance of the data
Critical downtime
It all depends on the amount of time it takes to redo the work. If it’s minor; then it can be ignored. If this is a major data loss, it may take several
business days and it could feasibly end up in a heavy loss for the organization.
A Backup is a process to create a copy of data and it can be used to reconstruct the data in case of any failures.
Backing up your SQL Server databases, running test restores procedures on your backups, and storing copies of backups in a safe, on-site, off-site, and cloud
location protects and safeguard the data from potentially catastrophic or various types of data loss.
3. What are different types of database backups?
The following are the different types of SQL Server database backups.
Full
Differential
Transactional Log(T-Log)
Copy-Only
File
FileGroup
Partial
Mirror
The type of a database backup depends on the database recovery model of the database.
The Recovery Model is a property of a database that controls how the transactions are logged.
The design of entire database and recovery procedures based on the context of the recovery model of the database.
It controls and manages each transaction scope into its entirety. You can refer to it here
In the following example, query the sys.databases catalog view the recovery model of the all the databases
The following example, the recovery model of the model database is set to FULL using the SET RECOVERY option of the ALTER DATABASE statement.
By default to members of the sysadmin fixed server role and the db_owner and db_backupoperator fixed database roles.
In this backup type, the whole database is backed up. This is the base for any type of backups. In order to have further differential or transaction log backups,
you must create the full database backup.
How can I find the progress of percentage completion of the database backup?
Use the keyword STATS in the T-SQL to monitor backup progress status. This can also be used with restore command to measure the progress.
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK' WITH STATS
Use the keyword FORMAT and COMPRESSION to format and compress the database backup.
On specifying the INIT keyword, all the backup sets are overwritten.
WITH STATS,INIT
By default, the NOINIT option is enabled. It means that the backup will append to other backups in the file
OR
11. How can I get Files and Filegroup information in SQL Server?
You can query the sys,filegroups joined with sys. databases_files to get the filegroup related information.
SELECT
dbf.name AS filename,
dbf.size/128 AS FileSizeMB,
dfg.name AS FGName,
dfg.type_desc,
dbf.physical_name AS Physicalpath
FROM
sys.database_files AS dbf
ON
dbf.data_space_id = dfg.data_space_id
12. What are system stored procedures that provide database information?
sp_helpdb
sp_helpfile
sp_helpfilegroup
sp_helpdb ‘PowerSQL’
On specifying the database name, it displays the database information along with the list all the files and related filegroup of the database
sp_helpfile
sp_helpfile is a subset of sp_helpdb and it returns files, filegroup, and their properties.
sp_helpfile PowerSQL_1
On specifying the parameter, the file, it will list the details of that specified file and associated details.
sp_helpfilegroup
sp_helpfilegroup lists all the filegroups and number of associated data files in each filegroup.
On specifying the filegroup, the output lists the specific filegroup and associated data file details.
A Differential backup is also a type of SQL Server database backup where it copies all the data that has changed since the last full backup.
14. What are T-Log backups and How can I create T-log (Transaction-log) backups?
The transaction log records every modification made to the databases. The records are maintained in such a way that the system can be brought to a
consistent state at any point of time with minimal or no data loss.
For T-Log backup, the full backup is the base. It takes complete log file data to write to a backup file.
The transaction log backup depends on the database recovery model and it’s relevant for the databases that are using the full or bulk-logged recovery models.
To execute the BACKUP LOG statement to back up the transaction log, specify the database name and backup device
BACKUP LOG PowerSQL TO DISK=N'f:\PowerSQL\PowerSQL_tlog.trc'
How can I continue the database backup processes despite they encounter an error?
It’s basically overriding the default behavior of database backup process using CONTINUE_AFTER_ERROR. On error, the backup process will stop the process.
In case of failure, it is required to start the recovery process, the first and foremost important step is intended to ensure take care of tail part of the transaction
before starting the restoration process is called tail-log backup.
WITH CONTINUE_AFTER_ERROR keyword, the SQL Server will override the default behavior even though it’s generating an error to complete the backup
process.
USE MASTER
GO
It’s a special type of backup. It is independent of the conventional backups and it will not have an impact on the overall backup process.
In simple words, it is used to create a full database or transaction log backup without breaking the log chain
COMPRESSION
In some cases, it is required to create multiple copies of data into different files. You can create a maximum of three mirror copies of the data at a time.
TO DISK = 'F:\PowerSQL\PowerSQL.BAK'
GO
18. What is a Partial database backup?
Partial database backup is one of the rarely used backup methods. All though it works with all recovery models, it is basically designed for simple database
recovery model database. This provides flexibility for backing up only READ_WRITE_FILEGROUPS.
This type of backup is mainly used where there is an issue with storage space.
In this type of database backup, the data will be split into parts and can be very useful during space constraints. Striped backup is a process of taking backup
to different locations.
TO DISK = 'F:\PowerSQL\PowerSQL.BAK',
DISK = 'F:\PowerSQL\PowerSQL_1.BAK',
DISK = 'F:\PowerSQL\PowerSQL_2.BAK'
Every SQL Server database must have a minimum of a data file and a log file. We can also create more than one files and it can be grouped together in
filegroups for easier file management and administration purpose.
File Backup
Filegroup = N'PRIMARY',
If you want the backup to expire, use WITH EXPIREDATE clause option in the backup database T-SQL. The following example shows How can I back up with an
expiration date on Jun 28, 2018:
00:00:00'
If you want to retain the backup for the only specific number of days then use the WITH RETAINDAYS clause with the database backup command.
USE MASTER
GO
WITH FORMAT,
ENCRYPTION
ALGORITHM = AES_256,
),
STATS = 10
GO
See Also,
Log level All index, drop index, updatetext, and writetext Minimal
Production
Point-in-time
recovery Yes No No
Log backup
support Yes No
Yes
Log Shipping
Database
Database
Replication
System databases are an essential component of SQL Server engine for the functioning of a server instance. The system databases are critical as it stores meta-
data of the user-defined databases. It must be backed up after every significant update as we do it for user-defined databases. The system databases that you
must always back up include msdb, master, model and configuration databases.
master The database is used to record all of the system level information Yes Simple
User
It is used by SQL Server Agent for job management and it also stores a history
A read-only database that contains copies of all system objects that ship with
Configure
In this article, we’ll walk through, some of the refined list of SQL Server backup-and-restore, (or recovery) interview Q&A. Also, it includes the high-level
overview and links of the “stairway to backup-and-restore series” articles for detailed information. As you read through the lists of Q&A, you’ll learn most of
the features and concepts of Backup-and-Restore operations.
The word “backup” refers to copying and archiving the data so that it may be used to restore in case of an event of data loss. In general, you should back up
any work or data that can’t be replaced easily.
A database backup is a copy of data from the database, which can be used to reconstruct the data. A database backup is a process to protect the data from
various disasters. Disasters cannot be completely prevented; the least we can do is to ensure we have everything that’s needed to get up and running as soon
as we can.
2. What are factors to consider while planning for backup, restore, and recovery
strategy?
It’s a broad topic to discuss but, some of the high-level points to be considered while defining good backup strategy include the following topics:
Backup and Restore (or Recovery) strategies for SQL Server database
It’s important for any database administrator to understand the data lifecycle and the nature of the particular business, in order to have the ability to recover
artifacts of business value from any sort of data disruptions.
4. How to do plan or setup SQL Server Backup and Restore strategy in a multi-server
environment using native tools?
In some cases, as part of company acquisition process, we may lead to revisit the review the existing the plans or we may also need to run through the steps to
measure the current process. It is always advised to understand the importance and implication of business requirements. As each database may have different
requirements based on the application it serves. The requirements may be based on:
How frequently does the application access the database? Is there a specific off-peak period when the backups can be scheduled?
How frequently does the data get changed? If the changes are too frequent, you may want to schedule incremental backups in between full
backups. Differential backups also reduce the restoration time.
If only a small part of a large database changes frequently, partial and/or file backups can be used.
Estimate the size of a full backup. Usually, the backup is smaller than the database itself, because it does not record the unused space in a database.
PowerShell comes into the rescue for most of the DBAs. It is really that simple enough to design to gather backup information with few lines of code.
Planning a SQL Server Backup and Restore strategy in a multi-server environment using PowerShell and T-SQL
Database recovery model is the database configuration determines the type of database backup one could initiate on the database. It defines the state of the
entries in the in transaction log files. Also, it provides the ways to restore or recover the database based on the configuration from varieties of failure.
SIMPLE
FULL
Differential
Full
Differential
Transaction log
Tail Log backup
Copy-only backup
File backups
Partial backups.
You could rely on database backup reports. In general, database administrators are very much concerned with getting the database backup report on a daily
basis and also alerts as per the Service Level Agreement. It’s considered as a prime role of DBAs to rely on the backup report to understand and how the
backups are running.
PowerShell scripts can be used to review the SQL Server error logs for backup failure events for the specific event id 18204,18210,3009,3017,3033,
and 3021
Use T-SQL to query msdb.dbo.backupset for the backup information
PowerShell SMO library to pull the related backup information
The database snapshots are directly dependent on the source structure of the database. Therefore, snapshots can never be a substitute for backup-and-restore
strategy. For instance, if an entire database is lost or corrupted, it means, the source files become inconsistent. If the source files are unavailable, snapshots
cannot refer to them, and so, snapshot restoration would be impossible.
9. What are the system tables that store backup and restore related information?
The MSDB database in-house several system tables that stores all the information related to the backup-and-restore operation. You can query the following
system tables for finding backup-and-restore information.
backupfile – Store information about all the backup of the data file or log file
backupfilegroup -This gives an information about all the backed up filegroups
backupmediafamily – Stores information about each media family
backupmediaset – Stores information about each backup media set
backupset – Stores information about each backup set
restorefile – Stores information about each restored file
restorefilegroup – Stores information about each restored filegroup
restorehistory – Stores information about each restore operation
Backup and Restore (or Recovery) strategies for SQL Server database
10. What are the new enhancements added for Backup and Restore SQL Server 2017?
SQL Server 2017, the enhanced the DMV’s and DMF’s which facilitates an extra column to measure the modified extents in a simpler way.
DCM (Differential Changed Map) tracks and tells what pages are changed since the last full backup. The values could be leveraged to determine if you need to
initiate a full or differential backup.
In SQL Server 2017, enhancement has been made to the sys.dm_db_log_stats dynamic management function. This function returns a
column log_since_last_log_backup_mb. Now, you have a better control the transactional log backup based on the amount of the data that has changed since
last transactional log backup.
Discussing Backup and Restore Automation using SQLCMD and SQL Server agent
PowerShell
Backup Linux SQL Server databases using PowerShell and Windows task scheduler
Sqlpackage.exe tool
SqlPackage.exe – Automate SQL Server Database Restoration using bacpac with PowerShell or Batch techniques
14. Explain the process of database backup and restore operations using the Cloud?
The backup-to-cloud functionality was added in SQL Server 2012. In general, backup and restore functionality to and from the cloud are similar to using disk or
tape, with very few differences. SQL Server database Backup to Azure Blob Storage is a process designed to perform almost like a backup device, such as disk
or tape. During the backup or restore process, a URL is selected as a “device type” which in turn triggers a VDI (Virtual Backup Device Interface) client process.
The process acts as an intermediary agent to send the database backup to the Azure Blob Storage.
SQL Server Database Backup and Restore operations using the Cloud
15. In a given situation, assume that the system has 3 drives with 30 GB of free space
on each drive. Now, how can you perform a database backup for 80 GB database?. Is
it possible?
Yes, it is possible.
In some instances, we’re limited by the amount of space on the drives. What if we wanted to backup an entire database that is huge? Or what if we have to
copy the backup files over the network? It might be a good idea in these cases to split the backup into smaller chunks—each being a separate file.
Piecemeal restore helps with databases that contain multiple file-groups to be restored and recovered at multiple stages. This would give an option to
customize the backup and restore (or recovery) solution.
Consider a scenario; where we have a database with 3 file-groups, Primary, read-only and read-write file-groups. We actually need not perform backup for
read-only file groups, here we can perform partial backups. We need to have a single backup copy of the read-only file-groups. Using the piecemeal process,
we do have the option to restore required file groups but not all of the file groups are required to make the database online at a specific instance. It is always
required to restore the primary filegroup but any secondary user-defined file groups are optional, at that point, while doing the restore. After the restore, one
could get partial data and it’s available online and for the rest of the data, the users can wait, for a period of time, to recover other file-groups.
17. What are database recovery phases and how it is different for in-memory
optimized objects?
When SQL Server instances restart, each database goes through different recovery stages.
Analysis
Redo
Undo
Analysis: In this phase, the transaction log is analyzed to track the information about the last checkpoint and create the Dirty Page Table (DPT); this captures
all the dirty-page details. In In-Memory OLTP engine, the analysis phase identifies the checkpoint inventory and prepares the system table with all the log
entries and also its processes the associated file allocation log records
Redo: It’s a roll-forward phase. When this phase completes, the database comes online.
For disk-based tables, the database is moved to the current point-in-time and acquires locks taken by uncommitted transactions.
For memory-optimized tables, data from the data and delta file pairs are loaded into the memory and then update the data with the active
transaction-log based on the last durable checkpoint. During this phase, disk and memory-optimized based object recovery run concurrently.
In SQL Server 2017, the redo phase of the memory-optimized tables (the associated data and delta files) is done in parallel. This injects faster times
for database recovery process.
When the above operations are completed for both disk-based and memory-optimized tables, the database becomes online and available for
access.
Undo: It’s a rollback phase. It holds the list of the active transaction from the analysis phase basically undoing the transactions. This phase is not needed for
memory-optimized tables since In-Memory OLTP doesn’t record any uncommitted transactions for memory-optimized tables.
Backup and Restore of a SQL Server database with Memory-Optimized objects TBA
18. How to perform database backup and restore operation on SQL Server Docker
containers?
As long as the containers remain intact with the host, the data will remain safe even if the container is stopped or restarted. However, if you remove the
container your databases get removed with it and it’ll be gone forever.
Let’s discuss the Ducker’s solution that keeps the data safe across containers. Using Docker data volume (-v) option, it is that simple to share the data. During
the SQL Server container creation process, map to the SQL Server database file directory using –v parameter.
19. What are the native toolset that are available to perform database backup and
restore operation?
a. SSMS
b. Sqlcmd – Discussing Backup and Restore Automation using SQLCMD and SQL Server agent
c. Sqlpackage.exe – SqlPackage.exe – Automate SQL Server Database Restoration using bacpac with PowerShell or Batch techniques
d. SQL Ops Studio – Backup and Restore operations using SQL Ops Studio TBA
e. PowerShell – Backup Linux SQL Server databases using PowerShell and Windows task scheduler
20. What are the top 10 trace flags can be used with database backup?
When used the following traceflags with 3605 traceflag, the output message to the errorlog.
SQL interview questions on database backups, restores and recovery – Part III
https://www.sqlshack.com/sql-interview-questions-on-database-backups-restores-and-recovery-part-iii/
So far, we’ve discussed a lot about database backup commands. In this article, we’ll discuss more on database restore or database recovery processes. When
you initiate a restore process, it undergoes a series of internal stages to restore or recover the data to the specific point of time.
Introduction
In this article, you’ll see the FAQs and answers about the database restore and recovery internals. To learn a lot about SQL Server database backup-and-restore
(recovery) internals, you can refer the full list of topics at the bottom.
Questions
In this section, let us deep-dive into the concepts of the database restore options.
It is the process of reconstructing the data to a usable state from database backup files in order to facilitate database operations.
Database recovery is the process of reconstructing the data that has been lost or it may be due to human errors (accidentally deletion) or hardware corruption
or catastrophic failure made the data inaccessible. The data recovery typically refers to the restoration of data to a point where there is no or minimal data loss.
The RESTORE VERIFYONLY command is used to check or validate the backup and it will ensure that the backup file is readable form.
4. How do you find the how many files are there .bak\trn files?
The RESTORE FILELISTONLY command is used to list all the files related to the specified backup file.
The RESTORE HEADERONLY command output the header information of the backup.
6. How do you find software that was used to create the database backup?
The RESTORE LABELONLY command output the backup media information. In the output, we can see a software column.
To restore a simple database from a backup file, run the following command
Using T-SQL, the database restore commands are almost similar to backup database commands. To perform database restore, you just need to
change the word “backup” to “restore” and “to” to “from” followed by restore and recovery options
Using SQL Server Management Studio, run through the following steps:
Browse Object Explorer
Right-click Databases
Click Restore Database
Select Source in the restore section
Select From Device, and choose the browse
Click Add to select the backup file
Select the Backup set to restore
Click the options tab, Enable the checkbox to “overwrite the existing database”
Select the recovery state (RECOVERY or NORECOVERY)
Click Ok
Using PowerShell, this is one of the ways to restore a database in PowerShell
Open the PowerShell ISE
Load the SQLServer Module
Define the variables
Database name
Backup file location
Data file location
Log file location
Prepare the restore command
Invoke the SQL string using Invoke-SQLCmd
Import-Module sqlServer
$dbname = "SQLShackDemo"
$backupFile = "\\aqdbt01\f$\PowerSQL\SQLShackDemo_07132018.bak"
$dataFile = "\\aqdbt01\f$\PowerSQL\SQLShackNewDB.mdf"
$logFile = "\\aqdbt01\f$\PowerSQL\SQLShackNewDB_log.ldf"
$backupSql = @"
USE [master]
WITH FILE = 1,
SET MULTI_USER
"@
8. How do you restore a database from full backup with overwrite option?
The following command is used to restore the database using the specified backup file. If the database already exists, it will overwrite the database files. If the
database doesn’t exist, it will create the database and restore the files to the specified location in the backup command.
WITH OVERWRITE
9. How does a full backup restore allow additional restores such as a differential or
transaction log backup?
Using NORECOVERY option, this option leaves the database in a restoring state after the full database restore has completed. This will allow you to restore
additional database backup files in order to get the database more current
WITH NORECOVERY
10. How do you do a differential backup file restore?
GO
GO
11. What are other types of database restore that you can perform in SQL Server?
This mode is also known as STANDBY mode and can be used for reading operations.
WITH STANDBY=N'F:\PowerSQL\PowerSQL_STANDBY_20171012_1.BAK'
The standby option is for High-availability setup such as Log Shipping. In this setup, you have configured a warm standby server for disaster recovery. SQL
Server engine designed to offer the ability to have the secondary database in a restoring state or in a standby state which allows read-only activity on the
secondary database.
12. How does Restore with move option different from normal restore operation?
The RESTORE … WITH MOVE option allows you to specify a new file location for the newly created database. Also, if you are restoring a database from another
instance with different file locations, then you need to use this move option.
WITH MOVE N'SQLShackDemo' TO 'F: \PowerSQLTest\ SQLShackDemoTest.mdf', MOVE N' N'SQLShackDemo' _log' TO 'F:
\PowerSQLTest\ SQLShackDemo_log.ldf'
13. How do you do a restore full database from multiple backup files?
SQL Server supports an option of write the backup data to multiple files. In some cases, the database backup file splitting is to manage the workloads of the
system. In this case, it is assumed to be full backup file is available and split into multiple files. The process is similar and we will perform database restore using
the following T-SQL:
DISK =N'F:\PowerSQL\PowerSQL_FULL_20171012_1.BAK'
,DISK = N'F:\PowerSQL\PowerSQL_FULL_20171012_2.BAK'
,DISK = N'F:\PowerSQL\PowerSQL_FULL_20171012_3.BAK'
WITH REPLACE ,
Piecemeal restore is a process to help database restore that contains multiple filegroups (s) and it is recovered in multiple stages.
Piecemeal restore process involves a series of the database restore, starting with the Primary filegroup followed by one or more secondary
filegroup(s).
Piecemeal restore process works with all recovery models
The restore process maintains a sequence called partial-restore sequence
NORECOVERY, PARTIAL
See Also,
The RESTORE … WITH STOPAT option allows you to restore your database to a point in time. This gives you the ability to restore a database prior to an event
that occurred that was detrimental to your database. In order for this option to work, the database needs to be either in the FULL or Bulk-Logged recovery
model and you need to be doing transaction log backups.
To perform the point-in-time database restores action, the database recovery model to be in either the Full or Bulk-Logged.
This will restore the database to a point “July 16, 2018, 01:38:00 PM”.
WITH NORECOVERY
GO
WITH RECOVERY,
STOPAT = 'July 16, 2018 01:38:00 PM'
GO
Page-level restore is a process or technique can be used to replace corrupted pages in a database with an uncorrupted data from the database backup file. If
you have a corrupt page(s) in SQL Server database, instead of restoring a complete database, you can restore only those pages that are corrupted from the
available valid database backup file set. The process can be performed via SSMS or T-SQL. You can query msdb.suspect_pages’ to identify corrupted pages and
track the pages that are marked as “suspect” in the table as well.
The “RESTORE database…WITH REPLACE” option allows overwriting an existing database while doing a database restore process.
Before performing database recovery, restore the entire database make sure it is consistent. However, it is also possible that the recovery can be applied
without restoring an entire database. For example, read-only file.
20. How do you generate restore one/one or more/all database script dynamically?
In the first T-SQL, the database names and backup file location are passed using a variable.
In the following steps, you can customize T-SQL to directly feed all the database names by querying the sys.databases object or you can hard code
the database names in the sub-query.
DECLARE
@path varchar(50)='F:\PowerSQL',
@dbname varchar(100)='SQLShackDemo'
Select
'RESTORE DATABASE '+ '['+sd.Name+']'+ ' FROM DISK = N'+''''+@path+'\'+sd.Name+'_20171012.BAK'+''''+' WITH MOVE
STATS = 5 , NORECOVERY'
from
sys.master_files sfm
DECLARE
@path varchar(50)='F:\PowerSQL'
Select
'RESTORE DATABASE '+ '['+sd.Name+']'+ ' FROM DISK = N'+''''+@path+'\'+sd.Name+'_20171012.BAK'+''''+' WITH MOVE
STATS = 5 , NORECOVERY'
from
sys.master_files sfm
Join sys.master_files sfl on sfl.database_id=sd.dbid and sfl.file_id=2 and sd.Name in(select name from sys.databases where
database_id>4)
DECLARE
@path varchar(50)='F:\PowerSQL'
Select
'RESTORE DATABASE '+ '['+sd.Name+']'+ ' FROM DISK = N'+''''+@path+'\'+sd.Name+'_20171012.BAK'+''''+' WITH MOVE
STATS = 5 , NORECOVERY'
from
sys.master_files sfm
Join sysdatabases sd On sfm.database_id=sd.dbid and sfm.file_id=1
In this article, we’ll see the how the backup-and-restore meta-data tables store the information in the MSDB database. Also, discuss several T-SQL statements
to derive most useful information with reference to data purge, database growth, backup report, restore history and more.
1. How do you delete six months old data to reduce the size of the backup and restore history tables?
2. How do you get the Backup History for a specific database including the size, location, and LSN?
3. How do you create and restore a marked transaction?
4. How do you find the RESTORE HISTORY of the database?
5. How do you list the last 30 days restore history at the instance level?
6. How do you measure the database backup or database restore operation progress?
7. How do you measure the database growth using backup size?
8. How do you define or estimate the storage required for database backup?
9. How do you get most recent database backup time for each database?
10. How do you get recent database backup time for each database using PowerShell?
11. How do you get recent database backup time for each database across multiple servers using PowerShell?
12. How do you find the backup history with duration and compressed backup size columns?
Questions
MSDB database is a log-store and it stores a complete history of all SQL Server backup-and-restore operations.
The following table highlights the high-level detail about the backup-and-restore operation:
The system table provides the most granular details of the backup file. It stores one row for each data file
or log file of a database. The columns describe the file type, file group name, page size and file
The table in-house the filegroup configuration of the database. It stores one row for each filegroup in a
backupfilegroup database.
backupset It contains a row for each backup set for successful backup.
logmarkhistory
Contains one row for each marked transaction that has been committed. It is applicable to only those
databases where the recovery model is set to full or bulk-logged.
It stores one row per page that failed with the error 823 or 824 See the link for more information How to
1. How do you delete six months old data to reduce the size of the backup and
restore history tables?
To reduce the size of the backup and restore history tables, delete the entries of backup sets that are older than the specified date-time. It is recommended to
run sp_delete_backuphistory frequently to clean-up the entries from the MSDB database.
USE msdb
GO
If you want to remove all the entries of backup history for a specific database, run sp_delete_database_backuphistory <database name>
USE msdb
Go
sp_delete_database_backuphistory 'SQLShackDemo'
2. How do you get the Backup History for a specific database including the size,
location, and LSN?
The following T-SQL provides you the backup information and LSN details of a given database.
The LSN is viewable using the following system-tables and using the restore command:
backupset
backupfile
sys.database_files;
sys.master_files
RESTORE HEADERONLY
In the following example, you can see that the LSN is ordered in a Zig-Zag fashion.
SELECT
bs.server_name,
bs.database_name,
bs.backup_start_date,
bs.backup_finish_date,
bs.user_name,
bs.first_LSN,
bs.last_LSN,
CASE
THEN 'Log'
THEN 'File/Filegroup'
THEN 'Partial'
END
bmf.physical_device_name
from msdb.dbo.backupset bs
where
bs.database_name='SQLShackDemo'
order by
bs.backup_start_date desc
Marked transactions are very useful to recover the database to a logically consistent point.
To create a marked transaction and restore the marked transaction follow the steps:
COMPRESSION,STATS=10
2. Use “BEGIN TRANSACTION WITH MARK” clause to mark the transaction and perform the DML operations
USE [SQLShackDemo]
GO
GO
UPDATE dbo.BackupInfo
SET compatibilitylevel=120
WHERE servername='hqdbsp18';
GO
GO
5.
6.
7. Restore full database backup WITH NORECOVERY option.
USE MASTER
GO
FROM DISK='F:\PowerSQL\SQLShackDemo_FULL_07172018.bak'
WITH NORECOVERY;
GO
FROM DISK='F:\PowerSQL\SQLShackDemo_LOG_07172018.TRN'
WITH RECOVERY,
STOPATMARK = 'UpdateBackupInfo';
10.
11.
This following T-SQL provides you with information about a particular database with the restore history and source, destination, start, end time and type of the
restore operation.
USE msdb
GO
SELECT
bs.server_name,
bs.database_name Source_database_name,
rh.destination_database_name,
bs.backup_set_id,
bs.backup_start_date,
bs.backup_start_date,
bs.backup_finish_date,
bs.user_name,
bs.first_LSN,
bs.last_LSN,
CASE
THEN 'Log'
THEN 'File/Filegroup'
THEN 'Partial'
,bmf.physical_device_name,
rh.restore_date
,rh.stop_at_mark_name
from backupset bs
where
bs.database_name='SQLShackDemo'
order by
rh.restore_date desc
5. How do you list the last 30 days restore history at the instance level?
The following T-SQL provides you a list of last 30 days data of database restore history.
SELECT
b.database_name source_database_name,
rh.destination_database_name,
ELSE rh.restore_type
rh.restore_date,
bm.physical_device_name SourceDevice
,rf.destination_phys_name destDevice
FROM msdb.dbo.restorehistory rh
WHERE
ORDER BY
rh.restore_history_id desc
Output:
6. How do you measure the database backup or database restore operation progress?
To measure the backup operation progress or estimate the time and percentage completed, you can query the DMV—sys.dm_exec_requests.
This script provides the output with backup estimation time and percentage completed.
SELECT command,
s.text,
er.start_time,
er.percent_complete,
FROM sys.dm_exec_requests er
WHERE er.command in ('BACKUP DATABASE', 'BACKUP LOG', 'RESTORE DATABASE', 'RESTORE LOG')
7. How do you measure the database growth using backup size?
You can construct the T-SQL using backup system table to analyze the growth of the database over a given timeframe. The T-SQL script used in the below-
mentioned link is used to calculate the capacity planning of the databases. The metrics are useful for capacity planning and forecasting.
Backup and Restore (or Recovery) strategies for SQL Server database
8. How do you define or estimate the storage required for database backup?
You can refer the T-SQL to get very detailed information about the database backup history. It also talks about capturing the baseline database growth
metrics.
Planning a SQL Server Backup and Restore strategy in a multi-server environment using PowerShell and T-SQL
9. How do you get most recent database backup time for each database?
The following T-SQL provides the most recent backup completion time of all databases along with the database name
FROM sys.sysdatabases sd
GROUP BY sd.Name
10. How do you get recent database backup time for each database using
PowerShell?
The following PowerShell script provides the most recent backup completion time of all databases with the database name
TRY {
else
{
Install-Module -Name SqlServer
} CATCH {
$databases=$srv.Databases
11. How do you get recent database backup time for each database across multiple
servers using PowerShell?
This can be done using looping construct in PowerShell with very few lines of code.
$List = @('aqdbsp18','aqdbs19','adbs201')
12. How do you find the backup history with duration and compressed backup size
columns?
The following T-SQL get the latest successful backups and it includes columns such as database name, backup start time, backup end time, a derived column
named duration (mins), backup file location, type of the backup, backup size, and compressed backup size (if used)
SELECT
bs.database_name DatabaseName,
bs.backup_start_date Backup_Start_Date,
bs.backup_finish_date Backup_Finished_Date,
bmf.physical_device_name Backup_File_Location,
CASE
WHEN bs.[type] = 'D'
THEN 'Full_Backup'
THEN 'Differential_Backup'
THEN 'Log_Backup'
THEN 'DifferentialFile_Backup'
THEN 'Partial_Backup'
THEN 'Differentialpartial_Backup'
END 'Backup_Type',
ROUND(((bs.backup_size/1024)/1024),2) Backup_SizeMB,
ROUND(((bs.compressed_backup_size/1024)/1024),2) CompressedBackup_SizeMB
ON bmf.media_set_id = bs.media_set_id
ORDER BY
bs.backup_start_date DESC
Output:
That’s all for now…
Wrapping up
Thus far, we’ve covered most of the concepts of “database backup-and-restore” operations. Please refer the TOC for more information.
Database Filegroup(s) and Piecemeal restores in SQL Server
https://www.sqlshack.com/database-filegroups-and-piecemeal-restores-in-sql-server/
So far, we discussed many de-facto details about SQL Server database backup and restore. In this 15 th article of the series, we
are going to discuss, the file-group and piecemeal database backup and restore process.
Database “Backup and Restore” strategies are vital to every organization for smoother functioning of the business. Database
design concepts are also important in defining the backup and restore strategy. A good database design structure and
proper planning would give us an ample time to speed up the recovery process.
1. Introduction
2. Explain file-group(s) level database backup and restore operations
3. Discuss piecemeal database restore process
4. Demo
5. And more…
In some cases, taking full database backup sis not a big deal, whereas, for VLDB databases or large OLTP databases, it may
not be a feasible solution to initiate frequent full database backups in-and-out. In such scenarios, the file(s) and filegroup(s)
backup and restore options play a vital role.
If you are operating VLDB database, in some cases, it becomes a daunting task to perform full database backup and restore
as it may take several hours to complete the backup and restore operation.
Piecemeal restore helps with databases that contain multiple filegroups to be restored and recovered at multiple stages. This
would give an option to customize the backup and restore (or recovery) solution.
Based on recommended practices and database design principles; if the database is designed to leverage data and segments
to different file groups and store them on a different drive this provides a great advantage when doing backups of the
database, and restoring the database in case of any database corruption or failure. Let’s say that one of the non-primary data
files may become corrupt or otherwise it can go offline due to some hardware failure then there is no need to perform the
full database restores, instead, only restore the filegroup that are needed. This operation will suffice or speed-up the entire
restoration process.
Getting started
Let us jump into the demo to see how to perform the backup and restore operation.
In most of the cases, a single data file and log file works best for the database design requirement. If you’re planning to
leverage data across multiple data files, create secondary file groups for the data and indexes, and make the secondary
filegroup a default one for the storage. In this way, the primary-file will contain only the system objects. Then it’s possible
that a single file group’s data file may become corrupted or otherwise go offline due to hardware failure or I/O subsystem
failure. When this happens, there’s no need to perform a full database restore. After all, the rest of the file groups are all still
safe and sound. By only restoring the file groups that need it, this way you can speed up the entire restoration process.
Let’s go ahead and complete the prep work by executing the following the T-SQL:
USE SQLShackFGDB;
3. Next, add additional file archiveData.ndf to the filegroup SecondarySQLShackFGDB to a SQLShackFGDB database
GO
ADD FILE
NAME = archiveData,
FILENAME = 'f:\powerSQL\archiveData.ndf',
SIZE = 5MB,
MAXSIZE = 100MB,
FILEGROWTH = 10MB
TO FILEGROUP SecondarySQLShackFGDB;
sf.name FileName,
size/128 SizeMB,
fg.name FGName,sf.physical_name
FROM sys.database_files sf
INNER JOIN
sys.filegroups fg ON sf.data_space_id=fg.data_space_id
5.
6.
7. Verify the location and it’s status of all the respective data and log files
FROM sys.master_files
8.
9.
10. Now, let’s use these two file groups, and create two tables. First table called ActiveSQLShackAuthor , and we’ll store it
on the primary file group. Then, a second table called InactiveSQLShackAuthor, and this one on
the SecondarySQLShackFGDB file group.
GO
ON SecondarySQLShackFGDB;
GO
values('Active1'),('Active2'),('Active3'),('Active4'),('Active5')
GO
values('Inactive1'),('Inactive2'),('Inactive3'),('Inactive4'),('Inactive5')
12. Verify the existence of inserted data into the SQL table
13.
14.
15. The following query list all the objects that are create on all the filegroups in the database
SELECT
OBJECT_NAME(st.object_id) AS ObjectName,
sds.name AS FileGroup
GO
16.
17.
First, initiate a backup of the entire database using full database backup command. The WITH format option is used to
override the already existing backups in the f:/PowerSQL/ folder.
TO DISK = 'f:\PowerSQL\SQLShackFGDB.bak'
WITH FORMAT;
values('Inactive6'),
('Inactive7'),
('Inactive8'),
('Inactive9'),
('Inactive10')
I’ll execute the following statement to create the backup of the filegroup.
FILEGROUP = 'SecondarySQLShackFGDB'
TO DISK = 'f:\PowerSQL\SecondarySQLShackFGDB.bak'
GO
Let’s simulate the hardware failure event by deleting the files. Now, the SQL Server won’t be able to access the secondary
filegroup.
USE MASTER;
GO
2.
3.
4. Locate the the secondary file and delete
USE MASTER;
GO
6.
7.
8. Check the error-log to isolate the issue. As we’ve deleted the file, the error-log report about the missing file.
9. Initiate a tail-log backup to recover the newly added data entries from the transaction log file.
USE MASTER
GO
WITH NO_TRUNCATE;
GO
10. Now, restore the secondary filegroup from the backup with NORECOVERY option.
FILE = 'archiveData',
FILEGROUP = 'SecondarySQLShackFGDB'
WITH NORECOVERY
GO
11.
12.
13. Apply the tail log to the database to bring it online
WITH RECOVERY
GO
14.
15.
16. Let’s go ahead validate the recovery process by querying the SQL table.
17.
Piecemeal restore
https://www.sqlshack.com/database-filegroups-and-piecemeal-restores-in-sql-server/
Piecemeal restore process involves a series of restore step sequences, starting with the primary and, one or more secondary
read-write filegroups followed by read-only filegroups.
In some scenarios, we need to do a database restore from the backup. As we know, we do have the option to restore
required file groups but not all of the file groups are requirered to make the database online at a specific instance. It is always
required to restore the primary file group but any secondary user defined file groups are optional, at that point, while doing
the restore. After the restore, one could get partial data and it’s available online and for the rest of the data, the users can
wait, for a period of time,to recover other filegroups.
The RESTORE DATABASE command with PARTIAL clause starts a new piecemeal restore operation. The keyword PARTIAL
indicates that the restore process involves a partial restore. The partial keyword defines and initiates the partial-restore
sequence. This will be validated during the recovery stages. The state of the database restores remains to be recovery
pending because their database recovery has been postponed.
Let us follow the below steps to prove the concept of the piecemeal process
GO
ADD FILE
NAME = readonlyData,
FILENAME = 'f:\powerSQL\readonlydata.ndf',
SIZE = 5MB,
MAXSIZE = 100MB,
FILEGROWTH = 10MB
TO FILEGROUP ReadOnlySQLShackFGDB;
GO
2. Add a table to the filegroup ReadOnlySQLShackFGDB and insert few records to the table
ON ReadOnlySQLShackFGDB;
GO
values('Report1'),
('Report2'),
('Report3'),
('Report4'),
('Report5')
4.
5.
6. To change the filegroup state to read_only use the following alter database command
use master
GO
7.
8. Backup the SQLShackFGDB database
BACKUP DATABASE SQLShackFGDB
TO DISK = 'f:\PowerSQL\SQLShackFGDB.bak'
WITH FORMAT;
GO
9. The database has three filegroup, One is read-only and other two are in read-write mode. Verify the filegroup status
by executing the following T-SQL
sf.name FileName,
size/128 SizeMB,
fg.name FGName,sf.physical_name,
sf.state_desc,
sf.is_read_only
FROM sys.database_files sf
INNER JOIN
sys.filegroups fg
ON sf.data_space_id=fg.data_space_id
10.
11. Backup the read-only database
FILEGROUP = 'ReadOnlySQLShackFGDB'
TO DISK = 'f:\PowerSQL\ReadOnlySQLShackFGDB.bak'
WITH FORMAT
GO
12.
13.
14. Now, drop the database to simulate the piecemeal recovery process of the database
USE MASTER;
GO
15. Let’s perform the database restore operation. Before you start, change the session context to master database. Now,
we’re going to do the read-write filegroups restore using READ_WRITE_FILEGROUPS clause.
USE MASTER
GO
GO
,[AUthorName]
FROM [SQLShackFGDB].[dbo].[InactiveSQLShackAuthor]
GO
,[AUthorName]
FROM [SQLShackFGDB].[dbo].ReportSQLShackAuthor
16.
17.
18. Next, restore the read-only filegroups
RESTORE DATABASE SQLShackFGDB
FILE = 'readonlyData',
FILEGROUP = 'ReadOnlySQLShackFGDB'
WITH RECOVERY
19.
20.
21. Verify the output by querying the read-only table data
,[AUthorName]
FROM [SQLShackFGDB].[dbo].[InactiveSQLShackAuthor]
GO
,[AUthorName]
FROM [SQLShackFGDB].[dbo].ReportSQLShackAuthor
22.
23.
Wrapping up
This article walkthrough the database backup and restore (or recovery) of a SQL Server that contain multiple files or
filegroups.
We also talked about file and filegroup level backup and the available options to restore partial databases with the concept
of a piecemeal restore. We saw how to perform the database recovery process by enabling filegroup backups. There is an
option available, that we reviewed, to speed up the recovery process without having to restore the entire database.