本文共 13508 字,大约阅读时间需要 45 分钟。
In my earlier PowerShell SQL Server article, , we explored the importance of a disaster recovery solution for an organization. Microsoft offers various disaster recovery solutions in SQL Server.
在我以前的PowerShell SQL Server文章“ ,我们探讨了灾难恢复解决方案对组织的重要性。 Microsoft在SQL Server中提供了各种灾难恢复解决方案。
We will explore SQL database backups in this series. We learned to take database backups for SQL Server using PowerShell Module – DBATools. It is DBA responsibility to restore backups on source database instance or different instance as per requirement. We usually use SSMS restore database wizard or t-SQL scripts to restore the database. We need to know about backup related information before planning for database restore. We have various PowerShell commands in DBATools to get useful backup information such as backup history, back threshold.
我们将在本系列中探索SQL数据库备份。 我们学会了使用PowerShell模块– DBATools为SQL Server进行数据库备份。 DBA负责根据要求在源数据库实例或其他实例上还原备份。 我们通常使用SSMS还原数据库向导或t-SQL脚本来还原数据库。 在计划数据库还原之前,我们需要了解有关备份的信息。 DBATools中具有各种PowerShell命令,以获取有用的备份信息,例如备份历史记录,后备阈值。
In this article, we will discuss SQL database backup commands in PowerShell SQL Server module DBATools.
在本文中,我们将讨论PowerShell SQL Server模块DBATools中的 SQL数据库备份命令。
We can get information about commands related to any particular keyword using Get-Help command. In the following screenshot, you can all commands related to keyword backup.
我们可以使用Get-Help命令获取有关与任何特定关键字相关的命令的信息。 在以下屏幕截图中,您可以使用与关键字backup相关的所有命令。
Let us explore a few useful DBATools command before we start with SQL database restoration.
在开始SQL数据库还原之前,让我们探索一些有用的DBATools命令。
We can get a list of all database backup files placed in a directory using Find-DbaBackup command. It is good practice to get information about particular command before we start exploring it.
我们可以使用Find-DbaBackup命令获得放置在目录中的所有数据库备份文件的列表。 在开始探索特定命令之前,最好先获取有关该命令的信息。
>get-help Find-DbaBackup
Suppose I want to get a list of all SQL database backups in the default directory. We need to pass the following parameters in this command.
假设我想获取默认目录中所有SQL数据库备份的列表。 我们需要在此命令中传递以下参数。
Before we execute Find-DbaBackup command, I have following backup files in default directory.
在执行Find-DbaBackup命令之前,我在默认目录中有以下备份文件。
Run following command in PowerShell.
在PowerShell中运行以下命令。
>Find-DbaBackup -Path 'C:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019CTP\MSSQL\Backup\' -BackupFileExtension bak -RetentionPeriod 48h
It returns all backup files having .bak extension and older than 48 hours.
它返回所有扩展名为.bak且时间超过48小时的备份文件。
Similarly, we can use following command to get backup in specified directory for .bak extension older than 3 days.
同样,我们可以使用以下命令在指定的目录中获取超过3天的.bak扩展名的备份。
>Find-DbaBackup -Path 'C:\Program Files\Microsoft SQL Server\MSSQL15.SQL2019CTP\MSSQL\Backup\' -BackupFileExtension bak -RetentionPeriod 3d
It is an exciting and useful DBATools command to know SQL database backup performance. SQL Server stores backup information in the MSDB database. We can use DBATools function Measure-DbaBackupThroughput to give the following useful information.
了解SQL数据库备份性能是令人兴奋且有用的DBATools命令。 SQL Server将备份信息存储在MSDB数据库中。 我们可以使用DBATools函数Measure-DbaBackupThroughput来提供以下有用的信息。
It is essential to understand database backup performance in SQL Server. We do not have any direct mechanism to calculate the details mentioned above. You have to get data from MSDB and perform calculations over it to get the required values.
必须了解SQL Server中的数据库备份性能。 我们没有任何直接的机制可以计算上述细节。 您必须从MSDB获取数据并对其进行计算才能获得所需的值。
DBATools provide Measure-DbaBackupThroughput to calculate these values for us without any additional configurations.
DBATools提供Measure-DbaBackupThroughput来为我们计算这些值,而无需任何其他配置。
As usual, we need to check syntax for this command in PowerShell.
与往常一样,我们需要在PowerShell中检查此命令的语法。
>Get-help Measure-DbaBackupThroughput
We can have the following important parameters for this command.
对于此命令,我们可以具有以下重要参数。
Let us explore this function using an example.
让我们使用一个示例来探索此功能。
In the following command, we want to check the SQL database backup throughput for SQL instance. I have used Out-GridView to display the result in a user-friendly format.
在以下命令中,我们要检查SQL实例SQL数据库备份吞吐量。 我使用Out-GridView以用户友好的格式显示结果。
>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP | Out-GridView
We have not used any filter in this command; therefore; it retrieves all database backup information from MSDB and calculates required information as shown in the following image. We get throughput information for our instance since initial database backup. It gives excellent information to check if we are not getting the desired throughput that might be the reason for a delay in backup completion. We have the following output using Measure-DbaBackupThroughput PowerShell command.
我们在此命令中未使用任何过滤器; 因此; 它从MSDB检索所有数据库备份信息,并计算所需的信息,如下图所示。 自从初始数据库备份以来,我们获得了实例的吞吐量信息。 它提供了极好的信息,可以检查我们是否没有获得所需的吞吐量,这可能是备份完成延迟的原因。 我们使用Measure-DbaBackupThroughput PowerShell命令获得以下输出。
Let us retrieve throughput information for a single database using -database parameter.
让我们使用-database参数检索单个数据库的吞吐量信息。
>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP -Database SQLShackdemo | Out-GridView
Suppose we do not want cumulative throughput information since first database backup. We only want to check throughput information for the last full backup of the specified database. We need to run the command with -Last and -Type (backup type) parameters.
假设自第一次数据库备份以来,我们不希望累积吞吐量信息。 我们只想检查指定数据库的上次完整备份的吞吐量信息。 我们需要使用-Last和-Type(备份类型)参数运行命令。
>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP -Database SQLShackdemo -Last -Type Full | Out-GridView
Similarly, we can use filter throughput information about last log backup for SQLDB database using the following command. I performed transaction log backup before running this command to get details in this demo.
同样,我们可以使用以下命令使用有关SQLDB数据库上次日志备份的筛选器吞吐量信息。 在运行此命令以获取本演示中的详细信息之前,我执行了事务日志备份。
>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP -Database SQLDB -Last -Type Log | Out-GridView
We have not added any date or time filter until now. Suppose we want to analyze last 7 days of full database backups for SQLShackDemo database. We can add -Since parameter in the command.
到目前为止,我们尚未添加任何日期或时间过滤器。 假设我们要分析最近7天SQLShackDemo数据库的完整数据库备份。 我们可以在命令中添加-Since参数。
In the following command, we filter last 7 days full backups using Get-Date and AddDays PowerShell function to filter results.
在以下命令中,我们使用Get-Date和AddDays PowerShell函数筛选结果,以筛选最近7天的完整备份。
>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP -Since (Get-Date).AddDays(-7) | Out-GridView
We can further filter results based on a threshold value. For example, we want to get backup details having a minimum throughput of less than 10 MB.
我们可以基于阈值进一步过滤结果。 例如,我们要获取最小吞吐量小于10 MB的备份详细信息。
>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP -Since (Get-Date).AddDays(-7) | Where-Object { $_.MinThroughput.Megabyte -lt 10 } | Out-GridView
Similarly, we can use the following query to get backup details having an average throughput of less than 5 MB in the last 7 days.
同样,我们可以使用以下查询来获取最近7天内平均吞吐量小于5 MB的备份详细信息。
>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP -Since (Get-Date).AddDays(-7) | Where-Object { $_.AvgThroughput.Megabyte -lt 5 } | Out-GridView
Normally, we do not care throughput of backups for system databases Master, Model and MSDB. We cannot have a backup for tempdb system database. We can exclude databases using – ExcludeDatabase parameter.
通常,我们不关心系统数据库Master,Model和MSDB的备份吞吐量。 我们无法为tempdb系统数据库提供备份。 我们可以使用– ExcludeDatabase参数排除数据库。
In the following query, we excluded system databases Master, Model and MSDB database from last 7 days backups having minimum throughput greater than 5 MB.
在以下查询中,我们从最近7天的备份中排除了具有最小吞吐量大于5 MB的系统数据库Master,Model和MSDB数据库。
>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP -Since (Get-Date).AddDays(-7) -ExcludeDatabase master,model,msdb | Where-Object { $_.MinThroughput.Megabyte -gt 5 } | Out-GridView
We can check SQL database Backups throughput for multiple instances altogether using Measure-DbaBackupThroughput.
我们可以使用Measure-DbaBackupThroughput来检查多个实例SQL数据库备份吞吐量。
Suppose we want to check backup throughput for following instances
假设我们要检查以下实例的备份吞吐量
In the -SqlInstance parameter specify both instance name separated by a comma. Execute the following command.
在-SqlInstance参数中,指定两个实例名称,并用逗号分隔。 执行以下命令。
>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP,Kashish\SQL2019 | Out-GridView
We can see both instances of backup throughput in the following screenshot. Similarly, you can specify multiple instances in a single command to get consolidated output.
在下面的屏幕快照中,我们可以看到两个备份吞吐量实例。 同样,您可以在单个命令中指定多个实例以获取合并的输出。
We need to get backup history for SQL Server databases using Get-DbaBackuphistory PowerShell Module of DBATools. We get details about all backups belonging to a particular database or instance in SQL Server. You can use various customizations to retrieve the result set as per our requirement.
我们需要使用DBATools的Get-DbaBackuphistory PowerShell模块获取 SQL Server数据库的备份历史记录。 我们获得有关属于SQL Server中特定数据库或实例的所有备份的详细信息。 您可以根据我们的要求使用各种自定义来检索结果集。
First, let us get information about Get-DbaBackuphistory using Get-Help
首先,让我们使用Get-Help获取有关Get-DbaBackuphistory的信息
We usually use t-SQL to fetch details from MSDB to get the backup history of the SQL Server database. It requires you to join multiple tables and retrieve information with customization in the SQL script. DBATools helps us to view database history with minimum effort.
我们通常使用t-SQL从MSDB获取详细信息,以获取SQL Server数据库的备份历史记录。 它要求您连接多个表并使用SQL脚本中的自定义检索信息。 DBATools帮助我们以最小的努力查看数据库历史记录。
We can pass information about -SqlInstance parameter to get all backups information in the specified instance.
我们可以传递有关-SqlInstance参数的信息,以获取指定实例中的所有备份信息。
>Get-DbaBackupHistory -SqlInstance Kashish\SQL2019CTP | Out-GridView
In the output, we get backup type, size, backup device type duration, start and end time.
在输出中,我们获得备份类型,大小,备份设备类型的持续时间,开始和结束时间。
Sometimes, we want latest backups (Full, Differential and Log backup) information in SQL instance. We can use –Last parameter for getting the latest backup information.
有时,我们需要SQL实例中的最新备份(完整,差异和日志备份)信息。 我们可以使用– Last参数来获取最新的备份信息。
>Get-DbaBackupHistory -SqlInstance Kashish\SQL2019CTP -Last | Out-GridView
If we have only full backup configured for a database, it just shows that entry. We have Full and Log backups for SQLDB database, therefore, in the output we can see both full and log backup.
如果我们仅为数据库配置了完整备份,则仅显示该条目。 我们具有SQLDB数据库的完整备份和日志备份,因此,在输出中,我们可以看到完整备份和日志备份。
We might have backups configured on disk, tape devices. We can filter results for a particular device type using -DeviceType parameter. I do not have any database backups in the Tape device. We can filter disk device backups for a particular database using the following command.
我们可能在磁盘,磁带设备上配置了备份。 我们可以使用-DeviceType参数过滤特定设备类型的结果。 磁带设备中没有任何数据库备份。 我们可以使用以下命令来过滤特定数据库的磁盘设备备份。
>Get-DbaBackupHistory -SqlInstance Kashish\SQL2019CTP -DeviceType Disk -Database SQLDB| Out-GridView
Suppose, we want backup history after a specific time for SQLDB database in SQL instance. We can run the following command.
假设我们要在特定时间后在SQL实例中为SQLDB数据库备份历史记录。 我们可以运行以下命令。
>Get-DbaBackupHistory -SqlInstance Kashish\SQL2019CTP -Database SQLDB -Since '2019-03-20 00:00:00' | Out-GridView
In this article, we explored various SQL database backup options using DBATools PowerShell Module. It is an interesting module to explore for backups in PowerShell. In the next article, we will cover database restoration commands in DBATools.
在本文中,我们使用DBATools PowerShell模块探索了各种SQL数据库备份选项。 这是探索PowerShell中备份的有趣模块。 在下一篇文章中,我们将介绍DBATools中的数据库恢复命令。
DBATools PowerShell SQL Server Database Backups commands |
DBATools PowerShell SQL Server数据库备份命令 |
翻译自:
转载地址:http://zbiwd.baihongyu.com/