DBATools PowerShell SQL Server数据库备份命令

In my earlier PowerShell SQL Server article, SQL Database Backups using PowerShell Module – DBATools, we explored the importance of a disaster recovery solution for an organization. Microsoft offers various disaster recovery solutions in SQL Server.

在我以前的PowerShell SQL Server文章“ 使用PowerShell模块SQL数据库备份– DBATools”中 ,我们探讨了灾难恢复解决方案对组织的重要性。 Microsoft在SQL Server中提供了各种灾难恢复解决方案。

  • Database Backup and Restore

    数据库备份和还原
  • Always on availability groups

    始终在可用性组中
  • Failover Clustering

    故障转移群集
  • Log Shipping

    日志传送

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相关的所有命令。

PowerShell SQL Server - SQL Database Backups commands in DBATools

Let us explore a few useful DBATools command before we start with SQL database restoration.

在开始SQL数据库还原之前,让我们探索一些有用的DBATools命令。

查找数据库备份 (Find-DbaBackup)

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

PowerShell SQL Server - Find-DbaBackup PowerShell Command

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数据库备份的列表。 我们需要在此命令中传递以下参数。

  • -Path: Provide path of the directory in which we have placed SQL database backups -Path:提供我们放置SQL数据库备份的目录的路径
  • -RetentionPeriod: It searches backup files older than the retention period -RetentionPeriod:搜索早于保留期限的备份文件
  • -BackupFileExtension: We can search for files for a specific extension in a specified directory. We normally use .bak (full database backups) and *.trn (transaction log backup) extensions in SQL database backups -BackupFileExtension:我们可以在指定目录中搜​​索特定扩展名的文件。 我们通常在SQL数据库备份中使用.bak(完整数据库备份)和* .trn(事务日志备份)扩展名

Before we execute Find-DbaBackup command, I have following backup files in default directory.

在执行Find-DbaBackup命令之前我在默认目录中有以下备份文件。

PowerShell SQL Server - Find-DbaBackup PowerShell Command

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小时的备份文件。

PowerShell SQL Server - Find-DbaBackup PowerShell Command configurations

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

PowerShell SQL Server - Find-DbaBackup PowerShell Command configurations

测量-DbaBackupThroughput (Measure-DbaBackupThroughput)

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来提供以下有用的信息。

  • Minimum and Maximum Throughput

    最小和最大吞吐量
  • Average backup duration

    平均备份时间
  • First and last backup date

    第一个和最后一个备份日期
  • Database backups count

    数据库备份计数
  • Average backup size

    平均备份大小

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

PowerShell SQL Server - Measure-DbaBackupThroughput command in PowerShell

We can have the following important parameters for this command.

对于此命令,我们可以具有以下重要参数。

  • -SqlInstance: We need to give SQL instance name for which we want information about backup performance -SqlInstance:我们需要提供我们想要有关备份性能的信息SQL实例名称
  • -Database: By default, it checks for all databases present in SQL instance. If we want details about a specific database, pass the database name to filter results -数据库:默认情况下,它检查SQL实例中存在的所有数据库。 如果我们需要有关特定数据库的详细信息,请传递数据库名称以过滤结果
  • -Last: We can use this parameter to get information about the last full, diff and log backups performance -Last:我们可以使用此参数来获取有关上次完整备份,差异备份和日志备份性能的信息
  • -Type: We can filter results for a particular backup type using this parameter -Type:我们可以使用此参数过滤特定备份类型的结果
  • -Since: We can use this filter to retrieve backup information from MSDB as per time filter using -Since parameter -Since:我们可以使用-Since参数,按照时间过滤器使用此过滤器从MSDB检索备份信息

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命令获得以下输出。

PowerShell SQL Server - Measure-DbaBackupThroughput command in PowerShell

Let us retrieve throughput information for a single database using -database parameter.

让我们使用-database参数检索单个数据库的吞吐量信息。

>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP -Database SQLShackdemo | Out-GridView

PowerShell SQL Server - Measure-DbaBackupThroughput command examples  in PowerShell

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

PowerShell SQL Server - Measure-DbaBackupThroughput command examples  in PowerShell

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

PowerShell SQL Server - Measure-DbaBackupThroughput command examples  in PowerShell

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-DateAddDays PowerShell函数筛选结果,以筛选最近7天的完整备份。

>Measure-DbaBackupThroughput -SqlInstance Kashish\SQL2019CTP -Since (Get-Date).AddDays(-7)  | Out-GridView

PowerShell SQL Server - Measure-DbaBackupThroughput command examples  in PowerShell

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

PowerShell SQL Server - Measure-DbaBackupThroughput command examples  in PowerShell

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

PowerShell SQL Server - Measure-DbaBackupThroughput command examples  in PowerShell

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

假设我们要检查以下实例的备份吞吐量

  1. Kashish\SQL2019

    Kashish \ SQL2019
  2. Kashish\SQL2019CTP

    Kashish \ SQL2019CTP

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.

在下面的屏幕快照中,我们可以看到两个备份吞吐量实例。 同样,您可以在单个命令中指定多个实例以获取合并的输出。

PowerShell SQL Server - Measure-DbaBackupThroughput command examples for multiple instances  in PowerShell

Get-DbaBackupHistory (Get-DbaBackupHistory)

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的信息

PowerShell SQL Server - Get-DbaBackupHistory PowerShell Command SQL Server

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.

在输出中,我们获得备份类型,大小,备份设备类型的持续时间,开始和结束时间。

PowerShell SQL Server - Get-DbaBackupHistory PowerShell Command Examples SQL Server

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数据库的完整备份和日志备份,因此,在输出中,我们可以看到完整备份和日志备份。

PowerShell SQL Server - Get-DbaBackupHistory PowerShell Command Examples SQL Server

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

PowerShell SQL Server - Get-DbaBackupHistory PowerShell Command Examples SQL Server

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

PowerShell SQL Server - Get-DbaBackupHistory PowerShell Command Examples SQL Server

结论 (Conclusion)

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中的数据库恢复命令。

目录 (Table of contents)

DBATools PowerShell Module for SQL Server
PowerShell SQL Server Validation Utility – DBAChecks
SQL Database Backups using PowerShell Module – DBATools
IDENTITY columns threshold using PowerShell SQL Server DBATools
DBATools PowerShell SQL Server Database Backups commands
SQL Restore Database using DBATools
Validate backups with SQL restore database operations using DBATools
Fix Orphan users in SQL Server using DBATools PowerShell
Creating a SQL Server Database using DBATools
Get SQL Database details using DBATools
Get-DbaHelpIndex command in DBATools
适用于SQL Server的DBATools PowerShell模块
PowerShell SQL Server验证实用程序– DBAChecks
使用PowerShell模块SQL数据库备份– DBATools
使用PowerShell SQL Server DBATools的IDENTITY列阈值
DBATools PowerShell SQL Server数据库备份命令
使用DBAToolsSQL Restore Database
使用DBATools通过SQL恢复数据库操作验证备份
使用DBATools PowerShell修复SQL Server中的孤立用户
使用DBATools创建SQL Server数据库
使用DBATools获取SQL数据库详细信息
DBATools中的Get-DbaHelpIndex命令

翻译自: https://www.sqlshack.com/dbatools-powershell-sql-server-database-backups-commands/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值