Uploaded by joseph kachere

FAQ's on SQL Server DB Backups-SQLSHACKS

advertisement
SQL interview questions on database backups, restores and recovery – Part I
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.
2.
Backup Database
Backup Log
Each of the above types defines the type of data.
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.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
Why are database backups so important?
What is a SQL Server database backup?
What are different types of database backups?
What is a database recovery model?
How can I check the recovery model of the database?
How can I change the recovery model?
What is a full database backup?
How can I create a compressed backup on a newly formatted backup file?
How can I overwrite to an existing backup file?
How can I append backup sets in SQL Server database?
How can I get Files and Filegroup information in SQL Server?
What are system stored procedures that provide database information?
What is a Differential backup?
What are T-Log backups and How can I create T-log (Transaction-log) backups?
What is a Tail-log backup?
What is a Copy-only backup?
What is a mirror backup?
What is a Partial database backup?
What is a Striped (Split) database backup in SQL Server?
What is a file and filegroup backup?
How can I set expiration dates for backups?
How can I retain backup for a specific number of days?
How can I encrypt a database backup using certificate?
What is the difference between FULL, Bulk-Logged and Simple recovery models?
What are system databases?
1. Why are database backups so important?
They is so important because of following reasons:





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.
2. What is a SQL Server database backup?
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.
4. What is a database recovery model?
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
5. How can I check the recovery model of the database?
In the following example, query the sys.databases catalog view the recovery model of the all the databases
SELECT name, recovery_model_desc FROM sys.databases;
6. How can I change the recovery model?
The following example, the recovery model of the model database is set to FULL using the SET RECOVERY option of the ALTER DATABASE statement.
ALTER DATABASE model SET RECOVERY FULL
What permission is required to take a database backup?
By default to members of the sysadmin fixed server role and the db_owner and db_backupoperator fixed database roles.
7. What is a full database backup?
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.
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
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
8. How can I create a compressed backup on a newly formatted backup file?
Use the keyword FORMAT and COMPRESSION to format and compress the database backup.
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
WITH STATS,FORMAT, COMPRESSION;
9. How can I overwrite to an existing backup file?
On specifying the INIT keyword, all the backup sets are overwritten.
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
WITH STATS,INIT
10. How can I append backup sets in SQL Server database?
By default, the NOINIT option is enabled. It means that the backup will append to other backups in the file
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
WITH STATS, NOINIT
OR
BACKUP DATABASE PowerSQL TO DISK='f:\PowerSQL\PowerSQL_FULL.BAK'
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
INNER JOIN sys.filegroups AS dfg
ON
dbf.data_space_id = dfg.data_space_id
12. What are system stored procedures that provide database information?
The following the system stored procedures:



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.
13. What is a Differential backup?
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.
BACKUP DATABASE PowerSQL TO DISK = N'f:\PowerSQL\PowerSQL_Diff.BAK' WITH DIFFERENTIAL
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.
BACKUP DATABASE PowerSQL TO DISK = N'f:\PowerSQL\PowerSQL_Diff.BAK' WITH CHECKSUM,
CONTINUE_AFTER_ERROR,STATS, FORMAT, COMPRESSION
15. What is a Tail-log backup?
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
BACKUP LOG PowerSQL TO DISK=N'f:\PowerSQL\PowerSQL_tlog.trc' WITH CHECKSUM, CONTINUE_AFTER_ERROR,STATS
16. What is a Copy-only backup?
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
BACKUP DATABASE PowerSQL TO DISK = N'f:\PowerSQL\PowerSQL_Diff.BAK' WITH COPY_ONLY,STATS, FORMAT,
COMPRESSION
17. What is a mirror backup?
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.
BACKUP DATABASE PowerSQL
TO DISK = 'F:\PowerSQL\PowerSQL.BAK'
MIRROR TO DISK = 'F:\PowerSQL\PowerSQL_1.BAK'
MIRROR TO DISK = 'F:\PowerSQL\PowerSQL_2.BAK'
WITH FORMAT, COMPRESSION, STATs
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.
BACKUP DATABASE PowerSQL READ_WRITE_FILEGROUPS TO DISK = N'f:\PowerSQL\PowerSQL_Diff.BAK'
WITH COPY_ONLY,STATS, FORMAT, COMPRESSION
19. What is a Striped (Split) database backup in SQL Server?
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.
BACKUP DATABASE PowerSQL
TO DISK = 'F:\PowerSQL\PowerSQL.BAK',
DISK = 'F:\PowerSQL\PowerSQL_1.BAK',
DISK = 'F:\PowerSQL\PowerSQL_2.BAK'
WITH FORMAT, COMPRESSION, STATS=10
20. What is a file and filegroup backup?
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.
Using this method, the desired file\file group backup is possible
File Backup
Syntax: BACKUP DATABASE [DBNAME] FILE = N‘FILENAME‘ TO DISK = N‘Path‘
BACKUP DATABASE PowerSQL FILE = N'PowerSQL_Index' TO DISK = N'F:\PowerSQL\PowerSQL_Index.BAK'
File Group Backup:
Syntax: BACKUP DATABASE [DBNAME] Filegroup = N‘Filegroup Name‘ TO DISK = N‘Path‘
BACKUP DATABASE PowerSQL
Filegroup = N'PRIMARY',
Filegroup = N'SalesInMemFG' TO DISK = N'F:\PowerSQL\PowerSQL_PRIMARYFG.BAK'
WITH FORMAT, STATS
21. How can I set expiration dates for backups?
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:
BACKUP DATABASE PowerSQL TO DISK = N'F:\PowerSQL\PowerSQL_PRIMARYFG.BAK' WITH EXPIREDATE = N'08/28/2018
00:00:00'
22. How can I retain backup for a specific number of days?
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.
BACKUP DATABASE PowerSQL TO DISK = N'F:\PowerSQL\PowerSQL.BAK' WITH RETAINDAYS = 3 , FORMAT, STATS=10
23. How can I encrypt a database backup using certificate?



Create master Key
Create certificate B
Backup the database
USE MASTER
GO
CREATE MASTER KEY ENCRYPTION BY PASSWORD = 'PowerSQL$2018'
CREATE CERTIFICATE BackupServerCert WITH SUBJECT = 'Backup certificate'
BACKUP DATABASE PowerSQL TO DISK = N'F:\PowerSQL\PowerSQL.BAK'
WITH FORMAT,
ENCRYPTION
(
ALGORITHM = AES_256,
SERVER CERTIFICATE = BackupServerCert
),
STATS = 10
GO
See Also,
Understanding Database Backup Encryption in SQL Server
24. What is the difference between FULL, Bulk-Logged and Simple recovery models?
FULL
BULK LOGGED
SIMPLE
All, but minimal log for few operations such as bulk
operations(bcp, BULK INSERT) Select into, Create index, alter
Log level
All
index, drop index, updatetext, and writetext
Minimal
During
Every time Checkpoint
Log Truncation
backup
background run against the
Process (LTP) Life
process
During backup process
database
Yes
Depends – Yes/No
Depends – Yes/No
Yes
No
No
Can it use for
Production
Server?
Point-in-time
recovery
Log backup
support
Yes
No
Yes
Piecemeal Restore Yes
Yes
Yes
Yes
Yes
No
Mirroring Support Yes
No
No
Yes
Yes
Log Shipping
Support
Database
Database
Replication
Support
Yes
25. What are system databases?
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 metadata 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.
System database
Description
Backup?
Recovery
model
master
The database is used to record all of the system level information
Yes
Simple
User
model
Yes
configurable*
Yes
Simple (default)
It‘s a template for all databases.
It is used by SQL Server Agent for job management and it also stores a history
msdb
of every backup and restore operations
A read-only database that contains copies of all system objects that ship with
Resource
SQL Server
No
—
tempdb
A workspace for SQL Server
No
Simple
Configuration database for replication
Yes
Simple
Configure
Distribution
SQL interview questions on database backups, restores and recovery – Part II
https://www.sqlshack.com/sql-interview-questions-on-database-backups-restores-and-recovery-part-ii/
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.
In this article, we‘ll discuss the following topics:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
What are database backups?
What are factors to consider while planning for backup, restore, and recovery strategy?
What are different stages of data life-cycle management?
How to do plan or setup SQL Server Backup and Restore strategy in a multi-server environment using native tools?
How does the database recovery model impact database backups?
Explain the different types of database backup in SQL Server?/a>
How can I verify that backups are occurring on a daily basis?
What is the difference between Database backup and Database snapshots?
What are the system tables that store backup and restore related information?
What are the new enhancements added for Backup and Restore SQL Server 2017?
What is a smart T-Log backup in SQL Server 2017?
Assume that the database recovery model is full. The full database backup runs every week at 9 PM. Sunday, differential backup runs daily at 9 PM.
Monday to Saturday, and hourly transaction log backups. Now, database crashed on Friday 5:10 PM. How to do a point-in-time recovery of the
database?
How to do database refresh automation?
Explain the process of database backup and restore operations using the Cloud?
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?
Explain piecemeal restore operation?
What are database recovery phases and how it is different for in-memory optimized objects?
How to perform database backup and restore operation on SQL Server Docker containers?
What are the native toolset that are available to perform database backup and restore operation?
What are the top 10 trace flags can be used with database backup?
1. What are database backups?
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.
An overview of the process of SQL Server backup-and-restore
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:






Rate or frequency at which data changes
Measure the volume of online transaction processing
Measure the frequency of the schema or structural design changes
Measure the frequency of database configuration change
Understand data generation and data loading patterns
Know the nature of the data
For additional information: refer to the below link
Backup and Restore (or Recovery) strategies for SQL Server database
3. What are different stages of data life-cycle management?
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.








Data capture or generation
Data usage
Data corruption
Data corruption prevention
Data protection
Data archival
Data purging
Data retention
For additional information: refer to the below link
Understanding the SQL Server Data Management Life Cycle
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?

If only a small part of a large database changes frequently, partial and/or file backups can be used.
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.

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.
For additional information: refer to the below link
Planning a SQL Server Backup and Restore strategy in a multi-server environment using PowerShell and T-SQL
5. How does the database recovery model impact database backups?
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.
SQL Server database recovery models:



SIMPLE
FULL
Differential
For additional information: refer to the below link.
Understanding SQL Server database recovery models
6. Explain the different types of database backup in SQL Server?
The most common types of backups available in SQL Server:




Full
Differential
Transaction log
Tail Log backup
There are other backup types available as well:



Copy-only backup
File backups
Partial backups.
For additional information: refer to the below link
Understanding SQL Server Backup Types
7. How can I verify that backups are occurring on a daily basis?
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.
The reports can be generated in many ways

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
For additional information: refer to the below link
SQL Server Database Backup and Restore reports
8. What is the difference between Database backup and Database snapshots?
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.
For additional information, refer to the below link
Understanding Database snapshots vs Database backups in SQL Server
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
For additional information; refer to the below link
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.
For more information, refer to the below link
Smart database backups in SQL Server 2017
11. What is a smart T-Log backup in SQL Server 2017?
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.
For more information, refer to the below link
Smart database backups in SQL Server 2017
12. Assume that the database recovery model is full. The full database backup runs
every week at 9 PM. Sunday, differential backup runs daily at 9 PM. Monday to
Saturday, and hourly transaction log backups. Now, database crashed on Friday 5:10
PM. How to do a point-in-time recovery of the database?
Yes, the point-in-time recovery is possible.





First, initiate a tail log backup
Now, restore recent Full database backup with NORECOVERY, i.e.is. Sunday 9 PM backup
Apply the Thursday night differential backup with NORECOVERY, i.e.is Thursday 9 PM. Differential backup
Next, Apply all the T-Log backups since Thursday Differential backup with NORECOVERY option
Apply tail-log backup WITH RECOVERY and STOPAT options
For additional information, refer to the below link
Tail-Log Backup and Restore in SQL Server
13. How to do database refresh automation?
This can be done using several of the following available techniques:

Sqlcmd, Robocopy utility, and T-SQL
For more information, see the below article
Discussing Backup and Restore Automation using SQLCMD and SQL Server agent

PowerShell
For more information, see the below article
Backup Linux SQL Server databases using PowerShell and Windows task scheduler

Sqlpackage.exe tool
For more information, see the below article
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.
For more information, refer the below link
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.
For more information, refer to the below link
Understanding SQL Server Backup Types
16. Explain piecemeal restore operation?
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.
Database Filegroup(s) and Piecemeal restores in SQL Server
17. What are database recovery phases and how it is different for in-memory
optimized objects?
Database Recovery Phases
When SQL Server instances restart, each database goes through different recovery stages.
The different phases are:



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.
Here are the points to ponder:


For disk-based tables, the database is moved to the current point-in-time and acquires locks taken by uncommitted transactions.

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.
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.
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.
For additional information, refer to the below link
SQL Server Database Recovery Process Internals – database STARTUP Command
For memory-optimized databases, refer the below link
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.
For additional information, refer the below link
Backup and Restore using SQL Server Docker Containers TBA
19. What are the native toolset that are available to perform database backup and
restore operation?
a.
b.
c.
d.
e.
SSMS
Sqlcmd – Discussing Backup and Restore Automation using SQLCMD and SQL Server agent
Sqlpackage.exe – SqlPackage.exe – Automate SQL Server Database Restoration using bacpac with PowerShell or Batch techniques
SQL Ops Studio – Backup and Restore operations using SQL Ops Studio TBA
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.










1806 – Turn off Instant-file-initialization
3004 – Logs every step of backup/restore internal operation.
3213 – Display the buffer and maxtransfersize information
3023 – Enables CHECKSUM option for the BACKUP command
3226 – Prevent log backup entries written into the errorlog
3001 – Prevent log backup entries written into the MSDB tables.
3014 – Log more internal information for every backup operation
3042 – Bypasses the default backup compression pre-allocation algorithm
3051 – Enables database backup to URL logging
3608 – Prevents SQL Server from automatically starting and recovering any database except the master database
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.
In this article, we‘ll answer the following topics.
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
Define database restore
Define the database recovery process
Check the state of the backup file
Check the number of file(s) in a backup file
Identify the database version from a backup file
Check the backup software tools used for backup
Perform simple and multiple ways to restore a database
Explain WITH OVERWRITE option
Explain WITH NORECOVERY option
Restore differential or t-log backup file
Understand Other restore types STANDBY/READONLY
UExplain WITH REPLACE option
Explain WITH MOVE option
Restore the database using a Split files
Detail Piecemeal restore process
Explain Point-in-time recovery
Describe the Page-Level-Restore process
Explain Recovery-Only database restore
Explain WITH STOPAT option
Generate restore data commands using dynamic T-SQL
Questions
In this section, let us deep-dive into the concepts of the database restore options.
1. What is a database restore?
It is the process of reconstructing the data to a usable state from database backup files in order to facilitate database operations.
2. What do you mean by database recovery?
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.
3. How do you check the backup file is a usable state?
The RESTORE VERIFYONLY command is used to check or validate the backup and it will ensure that the backup file is readable form.
RESTORE VERIFYONLY FROM DISK = N'F:\PowerSQL\PowerSQL.BAK'
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.
RESTORE FILELISTONLY FROM DISK = N'F:\PowerSQL\PowerSQL.BAK'
5. How do you find the database version of SQL Server by using the backup file?
The RESTORE HEADERONLY command output the header information of the backup.
RESTORE HEADERONLY FROM DISK = N'F:\PowerSQL\PowerSQL.BAK'
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.
RESTORE LABELONLY FROM DISK = N'F:\PowerSQL\PowerSQL.BAK'
7. How do you do a simple database restore in SQL Server?
To restore a simple database from a backup file, run the following command
RESTORE DATABASE PowerSQL FROM DISK =N'F:\PowerSQL\PowerSQL_FULL_20171012_1.BAK'
The different ways to perform database restore operations:

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]
IF EXISTS (SELECT * FROM sys.databases WHERE name = '$dbname')
ALTER DATABASE [$dbname] SET SINGLE_USER WITH ROLLBACK IMMEDIATE
RESTORE DATABASE [$dbname]
FROM DISK = N'$backupFile'
WITH FILE = 1,
MOVE N'SQLShackDemo' TO N'$dataFile',
MOVE N'SQLShackDemo_log' TO N'$logFile',
NOUNLOAD, REPLACE, STATS = 5
ALTER DATABASE [$dbname]
SET MULTI_USER
"@
Invoke-Sqlcmd -ServerInstance hqdbt01 -Query $backupSql
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.
RESTORE DATABASE SQLShack FROM DISK N'F:\PowerSQL\SQLSHACK_FULL_20171012_1.BAK'
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
RESTORE DATABASE SQLShack FROM DISK N'F:\PowerSQL\SQLSHACK_FULL_20171012_1.BAK'
WITH NORECOVERY
10. How do you do a differential backup file restore?
To restore a differential backup, the options are exactly the same.



You need do a full database restore with the NORECOVERY option
Initiate differential restore using the following command
Bring the database online, if you‘ve no other file to restore.
RESTORE DATABASE SQLShack FROM DISK =N'F:\PowerSQL\SQLSHACK_DIFF_20171012_1.BAK' WITH NORECOVERY
GO
RESTORE DATABASE SQLShack WITH RECOVERY
GO
11. What are other types of database restore that you can perform in SQL Server?
You can think of Standby / Read-Only Restore.
This mode is also known as STANDBY mode and can be used for reading operations.
RESTORE LOG PowerSQL FROM DISK='F:\PowerSQL\PowerSQL_log.trn'
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.
RESTORE DATABASE [SQLShackDemoTest] FROM DISK = 'F:\PowerSQL\SQLShackDemo_FULL.BAK'
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:
RESTORE DATABASE PowerSQL FROM
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 ,
MOVE 'PowerSQL' TO 'F:\PowerSQL\PowerSQL_data.mdf',
MOVE 'PowerSQL_Log' TO 'G:\PowerSQL\PowerSQL_data.mdf_log.ldf'
14. What is Piecemeal restore and explain the internals?
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
Restore database SQLShack FILEGROUP='Primary' from disk=N'F:\PowerSQL\PowerSQL_FULL_20171012.BAK' WITH
NORECOVERY, PARTIAL
See Also,
Database Filegroup(s) and Piecemeal restores in SQL Server
15. What is Point-in-time Recovery?
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.
16. Explain Point-in-time restore with STOPAT option
To perform the point-in-time database restores action, the database recovery model to be in either the Full or Bulk-Logged.



First, identify the valid FULL or Bulk-logged backup file
Restore the database with NORECOVERY
Restore the log
This will restore the database to a point ―July 16, 2018, 01:38:00 PM‖.
RESTORE DATABASE SQLShack FROM DISK = N'F:\PowerSQL\PowerSQL_FULL_20171012.BAK'
WITH NORECOVERY
GO
RESTORE LOG SQLShack FROM DISK = N'F:\PowerSQL\PowerSQL_LOG_20171012.TRN'
WITH RECOVERY,
STOPAT = 'July 16, 2018 01:38:00 PM'
GO
17. What is Page-level restore in SQL Server?
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.
Restore database SQLShack PAGE ='1:153,1:202' from disk=N'F:\PowerSQL\PowerSQL_20171012.BAK' WITH RECOVERY
The entire process is very well explained in the following article
How to perform a page level restore in SQL Server
18. What is WITH REPLACE option in Restore command?
The ―RESTORE database…WITH REPLACE‖ option allows overwriting an existing database while doing a database restore process.
The following restore database command overwrite the database files
RESTORE DATABASE [ApexSQLCrd] FROM DISK = N'F:\PowerSQL\ApexSQLCrd_20171012.BAK' WITH REPLACE
19. How do you perform the Recovery-Only database restore?
The scope of recovery-only restore is to the database or file or filegroup(s) level.
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.
RESTORE DATABASE SQLShack WITH RECOVERY
For filegroup, issue the following command
RESTORE DATABASE SQLShack FILEGROUP='SQLShackFG' WITH RECOVERY
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
N'+''''+sfm.Name+''''+ ' TO N'+''''+sfm.Physical_Name+''''+','+' MOVE N'+''''+sfl.Name+''''+' TO N'+''''+sfl.physical_name+''''+','+' REPLACE,
STATS = 5 , NORECOVERY'
from
sys.master_files sfm
Join sysdatabases sd On sfm.database_id=sd.dbid and sfm.file_id=1
Join sys.master_files sfl on sfl.database_id=sd.dbid and sfl.file_id=2 and sd.Name in(@dbname)
DECLARE
@path varchar(50)='F:\PowerSQL'
Select
'RESTORE DATABASE '+ '['+sd.Name+']'+ ' FROM DISK = N'+''''+@path+'\'+sd.Name+'_20171012.BAK'+''''+' WITH MOVE
N'+''''+sfm.Name+''''+ ' TO N'+''''+sfm.Physical_Name+''''+','+' MOVE N'+''''+sfl.Name+''''+' TO N'+''''+sfl.physical_name+''''+','+' REPLACE,
STATS = 5 , NORECOVERY'
from
sys.master_files sfm
Join sysdatabases sd On sfm.database_id=sd.dbid and sfm.file_id=1
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
N'+''''+sfm.Name+''''+ ' TO N'+''''+sfm.Physical_Name+''''+','+' MOVE N'+''''+sfl.Name+''''+' TO N'+''''+sfl.physical_name+''''+','+' REPLACE,
STATS = 5 , NORECOVERY'
from
sys.master_files sfm
Join sysdatabases sd On sfm.database_id=sd.dbid and sfm.file_id=1
Join sys.master_files sfl on sfl.database_id=sd.dbid and sfl.file_id=2 and sd.Name in('SQLShackDemo','SQLShackInMemDB')
That‘s all for now…stay tuned for more updates.
SQL Interview questions on database backups, restores and recovery – Part IV
https://www.sqlshack.com/sql-interview-questions-on-database-backups-restores-and-recovery-part-iv/
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.
In this article, we‘ll discuss the following topics:
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
How do you delete six months old data to reduce the size of the backup and restore history tables?
How do you get the Backup History for a specific database including the size, location, and LSN?
How do you create and restore a marked transaction?
How do you find the RESTORE HISTORY of the database?
How do you list the last 30 days restore history at the instance level?
How do you measure the database backup or database restore operation progress?
How do you measure the database growth using backup size?
How do you define or estimate the storage required for database backup?
How do you get most recent database backup time for each database?
How do you get recent database backup time for each database using PowerShell?
How do you get recent database backup time for each database across multiple servers using PowerShell?
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:
System tables
Description
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
backupfile
configuration information.
The table in-house the filegroup configuration of the database. It stores one row for each filegroup in a
backupfilegroup
database.
backupmediafamily It stores a row for each media family.
backupmediaset
It stores a row for each backup media set.
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.
restorefile
It stores one row for each restored file.
restorefilegroup
It stores one row for each restored filegroup.
restorehistory
It stores one row for each restore operation
It stores one row per page that failed with the error 823 or 824 See the link for more information How to
suspect_pages
perform a page level restore in SQL Server
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
Declare @date Datetime = dateadd(mm,-6,getdate())
EXEC sp_delete_backuphistory @date
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
WHEN bs.[type] = 'D'
THEN 'Full Backup'
WHEN bs.[type] = 'I'
THEN 'Differential Database'
WHEN bs.[type] = 'L'
THEN 'Log'
WHEN bs.[type] = 'F'
THEN 'File/Filegroup'
WHEN bs.[type] = 'G'
THEN 'Differential File'
WHEN bs.[type] = 'P'
THEN 'Partial'
WHEN bs.[type] = 'Q'
THEN 'Differential partial'
END
,cast((bs.backup_size/1024)/1024 as numeric(8,0)) 'backup_size MB',
bmf.physical_device_name
from msdb.dbo.backupset bs
inner join msdb.dbo.backupmediafamily bmf
on bs.media_set_id=bmf.media_set_id
where
bs.database_name='SQLShackDemo'
order by
bs.backup_start_date desc
3. How do you create and restore a marked transaction?
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:
1.
Create full/differential database backup
BACKUP DATABASE [SQLShackDemo] TO DISK='F:\PowerSQL\SQLShackDemo_FULL_07172018.bak' WITH
COMPRESSION,STATS=10
2.
Use ―BEGIN TRANSACTION WITH MARK‖ clause to mark the transaction and perform the DML operations
USE [SQLShackDemo]
GO
BEGIN TRANSACTION UpdateBackupInfoDemo
WITH MARK 'UPDATE Compatibility level';
GO
UPDATE dbo.BackupInfo
SET compatibilitylevel=120
WHERE servername='hqdbsp18';
GO
COMMIT TRANSACTION UpdateBackupInfo;
GO
3.
Back up the SQLShackDemo transaction-log
BACKUP LOG [SQLShackDemo] TO DISK='F:\PowerSQL\SQLShackDemo_LOG_07172018.TRN' WITH
COMPRESSION,STATS=10
4.
To verify the marked transaction, run the following command
SELECT * FROM MSDB.dbo.logmarkhistory
5.
6.
7.
Restore full database backup WITH NORECOVERY option.
USE MASTER
GO
DROP DATABASE IF EXISTS [SQLShackDemo]
RESTORE DATABASE [SQLShackDemo]
FROM DISK='F:\PowerSQL\SQLShackDemo_FULL_07172018.bak'
WITH NORECOVERY;
GO
8.
Restore log WITH STOPATMARK option
RESTORE LOG [SQLShackDemo]
FROM DISK='F:\PowerSQL\SQLShackDemo_LOG_07172018.TRN'
WITH RECOVERY,
STOPATMARK = 'UpdateBackupInfo';
9.
Verify the data
SELECT * FROM dbo.BackupInfo WHERE servername='aqdbsp18';
10.
11.
4. How do you find the RESTORE HISTORY of the database?
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
WHEN bs.[type] = 'D'
THEN 'Full Backup'
WHEN bs.[type] = 'I'
THEN 'Differential Database'
WHEN bs.[type] = 'L'
THEN 'Log'
WHEN bs.[type] = 'F'
THEN 'File/Filegroup'
WHEN bs.[type] = 'G'
THEN 'Differential File'
WHEN bs.[type] = 'P'
THEN 'Partial'
WHEN bs.[type] = 'Q'
THEN 'Differential partial'
END
,bmf.physical_device_name,
rh.restore_date
,rh.stop_at_mark_name
from backupset bs
inner join backupmediafamily bmf on bs.media_set_id=bmf.media_set_id
inner join restorehistory rh on bs.backup_set_id=rh.backup_set_id
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,
rh.user_name AS [Restored By],
CASE WHEN rh.restore_type = 'D' THEN 'Database Restore'
WHEN rh.restore_type = 'F' THEN 'File Restore'
WHEN rh.restore_type = 'G' THEN 'Filegroup Restore'
WHEN rh.restore_type = 'I' THEN 'Differential Restore'
WHEN rh.restore_type = 'L' THEN 'Log Restore'
ELSE rh.restore_type
END AS [Restore Type],
rh.restore_date,
bm.physical_device_name SourceDevice
,rf.destination_phys_name destDevice
FROM msdb.dbo.restorehistory rh
INNER JOIN msdb.dbo.backupset b ON rh.backup_set_id = b.backup_set_id
INNER JOIN msdb.dbo.restorefile r ON rh.restore_history_id = r.restore_history_id
INNER JOIN msdb.dbo.backupmediafamily bm ON bm.media_set_id = b.media_set_id
WHERE
rh.restore_date >= DATEADD(dd, -30,getdate())
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,
CAST(((DATEDIFF(s,start_time,GetDate()))/3600) as varchar) + ' hr(s), '
+ CAST((DATEDIFF(s,start_time,GetDate())%3600)/60 as varchar) + 'min(s), '
+ CAST((DATEDIFF(s,start_time,GetDate())%60) as varchar) + ' ss' as running_time,
CAST((estimated_completion_time/3600000) as varchar) + ' hr(s), '
+ CAST((estimated_completion_time %3600000)/60000 as varchar) + 'min(s), '
+ CAST((estimated_completion_time %60000)/1000 as varchar) + ' ss' as est_time_to_go,
dateadd(second,estimated_completion_time/1000, getdate()) as est_completion_time
FROM sys.dm_exec_requests er
CROSS APPLY sys.dm_exec_sql_text(er.sql_handle) s
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 belowmentioned 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
SELECT sd.Name AS DatabaseName,
COALESCE(CONVERT(VARCHAR(12), MAX(bs.backup_finish_date), 101),'-') AS LastBackUpTime
FROM sys.sysdatabases sd
LEFT OUTER JOIN msdb.dbo.backupset bs ON bs.database_name = sd.name
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
1.
2.
3.
Load the SQLServer Module
Instantiate the SMO class library
Invoke the database property
TRY {
If (Get-Module SQLServer -ListAvailable)
{
Write-Verbose "Preferred SQLServer module found"
}
else
{
Install-Module -Name SqlServer
}
} CATCH {
Write-Host "Check the Module and version"
}
$srv = New-Object ('Microsoft.SqlServer.Management.Smo.Server') 'hqdbt01'
$databases=$srv.Databases
$databases | SELECT Name,LastBackupDate, LastLogBackupDate | Format-Table -autosize
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.
1.
2.
3.
Load the SQLServer Module
List the SQL Server instances
Use looping construct to Instantiate the SMO class library and list the database properties
Install-Module -Name SqlServer
$List = @('aqdbsp18','aqdbs19','adbs201')
$list|%{(New-Object -TypeName Microsoft.SQLServer.Management.Smo.Server($ServerName)).Databases| SELECT
Name,LastBackupDate, LastLogBackupDate | Format-Table -autosize}
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,
DATEDIFF(MINUTE, bs.backup_start_date, bs.backup_finish_date) Duration_Mins,
bmf.physical_device_name Backup_File_Location,
CASE
WHEN bs.[type] = 'D'
THEN 'Full_Backup'
WHEN bs.[type] = 'I'
THEN 'Differential_Backup'
WHEN bs.[type] = 'L'
THEN 'Log_Backup'
WHEN bs.[type] = 'F'
THEN 'File/Filegroup backup'
WHEN bs.[type] = 'G'
THEN 'DifferentialFile_Backup'
WHEN bs.[type] = 'P'
THEN 'Partial_Backup'
WHEN bs.[type] = 'Q'
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
FROM msdb.dbo.backupmediafamily bmf
INNER JOIN msdb.dbo.backupset bs
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.
Download