博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
使用PowerShell模块SQL数据库备份– DBATools
阅读量:2519 次
发布时间:2019-05-11

本文共 14953 字,大约阅读时间需要 49 分钟。

This article will be first article of series for SQL database backup and restoration using DBAtools, a powerful open source library of PowerShell automation scripts.

本文将是使用DBAtools(一个强大的PowerShell自动化脚本的开源库)进行SQL数据库备份和还原的系列文章的第一篇。

Every organization should prepare a Disaster Recovery plan to avoid a business loss against any unexpected downtime. It can be in the form of power failure, data centre issues, cyber-attacks, natural disaster etc. We must do a regular disaster drill to be prepared any such incidents. The DBA plays a vital role in these drills.

每个组织都应准备灾难恢复计划,以避免因任何意外停机而造成业务损失。 可能是电源故障,数据中心问题,网络攻击,自然灾害等形式。我们必须定期进行灾难演习,以准备任何此类事件。 DBA在这些演练中起着至关重要的作用。

We used to take regular database backups for production servers to restore later in case of any loss of data. You should do regular database restoration drills to verify that your backup policy is valid and able to recover from any incidents. We need to restore a database backup on the instance on the same version of SQL Server.

我们过去经常为生产服务器进行数据库备份,以备日后在数据丢失的情况下还原。 您应该进行常规的数据库还原演练,以验证您的备份策略有效并且能够从任何事件中恢复。 我们需要在相同版本SQL Server上的实例上还原数据库备份。

If you are maintaining a large number of production instances, it becomes tedious work to test backups on a specified frequency. We also need to perform additional task of consistency check using DBCC CHECKDB command after database restoration. It ensures that there is no allocation or consistency error in the restore database. As a summary, we need to perform two tasks in database restoration drills:

如果要维护大量生产实例,那么以指定频率测试备份将变得很繁琐。 数据库还原后,我们还需要使用DBCC CHECKDB命令执行其他一致性检查任务。 它确保还原数据库中没有分配或一致性错误。 作为总结,我们需要在数据库还原练习中执行两项任务:

  1. Restore database regularly from existing backups regularly

    定期从现有备份中还原数据库
  2. Database Consistency check on the newly restored database

    对新还原的数据库进行数据库一致性检查

In SQL Server, you can write t-SQL code or create an SSIS package to perform the required steps. You need to be good in programming or SSIS package development to do so.

在SQL Server中,您可以编写t-SQL代码或创建SSIS包以执行所需的步骤。 您需要精通编程或SSIS包开发。

As an alternative, we can use DBATools to maintain database backups, restoration and verifications for disaster recovery purpose. In this article, we will discuss database backups using PowerShell SQL Server module DBATools.

或者,我们可以使用DBATools维护数据库备份,还原和验证,以进行灾难恢复。 在本文中,我们将讨论使用PowerShell SQL Server模块DBATools进行数据库备份。

  • Note: We are using Azure Data Studio to run DBATools commands. You can also use Windows PowerShell for this.
  • 注意: 我们正在使用Azure Data Studio运行DBATools命令。 您也可以使用Windows PowerShell。

使用DBAToolsSQL数据库备份 (SQL Database backups using DBATools)

We can get all commands related to keyword Backup using Get-help.

我们可以使用Get-help获取与关键字Backup相关的所有命令。

>Get-Help *Backup*

We will verify last database backups using command Get-DbaLastBackup. In this command, we used Out-GridView to get results in grid view.

我们将使用命令Get-DbaLastBackup验证最后的数据库备份 在此命令中,我们使用Out-GridView在网格视图中获取结果。

>get-help Get-DbaLastBackup

In below screenshot, we can see the syntax, description for this command.

在下面的屏幕截图中,我们可以看到此命令的语法,说明。

Let us run this command in my instance Kashish\SQL2019CTP. In the output, we can see that currently, I do not have any SQL backups for my database instance. It gives timestamp of each database backup in corresponding column such as LastFullBackup, lastDiffbackup, LastLogbackup. It also gives the details of the number of days since last full, differential and log backup.

让我们在实例Kashish \ SQL2019CTP中运行此命令。 在输出中,我们可以看到当前我的数据库实例没有任何SQL备份。 它在相应的列(例如LastFullBackup,lastDiffbackup,LastLogbackup)中提供每个数据库备份的时间戳。 它还提供自上次完整备份,差异备份和日志备份以来的天数的详细信息。

使用DBATools进行数据库备份 (Take a database backup using DBATools)

We can perform database backups using Backup-DBADatabase command using DBATools in PowerShell SQL Server. Check the syntax of Backup-DBADatabase using below command.

我们可以在PowerShell SQL Server中使用DBATools使用Backup-DBADatabase命令执行数据库备份。 使用以下命令检查Backup-DBADatabase的语法。

>get-help Backup-DBADatabase

We can take full SQL database backups, transaction log backups, and database file backups. We need to provide the following parameters to take database backup.

我们可以进行完整SQL数据库备份,事务日志备份和数据库文件备份。 我们需要提供以下参数来进行数据库备份。

  • The server name in -parameter

    -parameter中的服务器名称
  • Database name to the -Database parameter

    -Database参数的数据库名称
  • Type of database backup using -Type parameter. If we do not specify any backup type, it takes Full backup

    使用-Type参数的数据库备份类型。 如果我们不指定任何备份类型,则需要完全备份
  • We can specify a backup directory using -BackupDirectory parameter. If we do not specify any backup directory, it takes backup in the default directory

    我们可以使用-BackupDirectory参数指定备份目录。 如果我们未指定任何备份目录,它将在默认目录中进行备份
    1. Full backup: Databasename_yyyymmddhhss.bak

      完全备份:Databasename_yyyymmddhhss.bak
    2. Log backup: Databasename_ yyyymmddhhss.trn

      日志备份:Databasename_ yyyymmddhhss.trn

Let us perform a database backup using Backup-DBADatabase by only specifying an instance name.

让我们仅通过指定实例名称来使用Backup-DBADatabase执行数据库备份。

>Backup-DBADatabase -ServerInstance Kashish\SQL2019CTP

We did not specify any database in the Backup-DBADatabase command in PowerShell SQL Server. It takes backups of all databases in this case.

我们未在PowerShell SQL Server的Backup-DBADatabase命令中指定任何数据库。 在这种情况下,它需要备份所有数据库。

We can verify the database backup using Get-DbaLastBackup command. We can see here that entry for last full backup and status as well.

我们可以使用Get-DbaLastBackup命令验证数据库备份。 我们在这里也可以看到该条目的最新完整备份和状态。

>Get-DbaLastBackup -SqlServer Kashish\SQL2019CTP | Out-GridView

If the database recovery model is full and log backups are not running, you can see in the status ‘No log backup in the last hour’.

如果数据库恢复模型已满并且日志备份未在运行,则可以在状态“最近一小时没有日志备份”中看到。

In the default backup directory, database backup file is present. We can see the backup file in the format of databasename_yyyymmddhhss.bak as per default full backup format.

在默认备份目录中,存在数据库备份文件。 根据默认的完整备份格式,我们可以看到格式为databasename_yyyymmddhhss.bak的备份文件。

使用DBATools进行差异数据库备份 (Differential database backup using DBATools)

Suppose we want to take a differential backup for SQLShackDemo database only. We can specify the database name using -database parameter. We also need to specify a backup type as Differential.

假设我们只想对SQLShackDemo数据库进行差异备份。 我们可以使用-database参数指定数据库名称。 我们还需要将备份类型指定为“差异”。

>Backup-DBADatabase -SqlInstance  Kashish\SQL2019CTP -Type Differential -Database SQLShackDemo

It takes differential backup for the specified database and returns the details such as type, totalsize, start time, end time and duration for this backup.

它为指定的数据库执行差异备份,并返回此备份的详细信息,例如类型,总计大小,开始时间,结束时间和持续时间。

Now, we want to retake differential SQL database backup for user databases SQLShackDemo and SQLShackDemo_ADR in the directory C:\TEMP\Backup. We can specify multiple databases name in -database parameter separated by a comma.

现在,我们要为目录C:\ TEMP \ Backup中的用户数据库SQLShackDemo和SQLShackDemo_ADR重新获取差异SQL数据库备份。 我们可以在-database参数中用逗号分隔来指定多个数据库名称。

In the following query, we specified the backup directory in the BackupDirectory parameter.

在以下查询中,我们在BackupDirectory参数中指定了备份目录。

>Backup-DBADatabase -SqlInstance  Kashish\SQL2019CTP -Type Differential -Database SQLShackDemo,SQLShackDemo_ADR -BackupDirectory C:\TEMP\Backup\

You can see the differential database backup for both the databases in the specified location.

您可以在指定位置看到两个数据库的差异数据库备份。

使用DBATools的自定义SQL数据库备份文件格式 (Custom SQL database backup file format using DBATools)

You might notice that backup file format is the same for both full and differential backup as database_YYYYMMDDHMM. We want to give a customized name for the backup file. We need to do following changes in command for backup using DBATools.

您可能会注意到,完整备份和差异备份的备份文件格式都与database_YYYYMMDDHMM相同。 我们要为备份文件指定一个自定义名称。 我们需要对命令进行以下更改,以便使用DBATools进行备份。

  • BackupFileName: Specify a format for the database backup file. I want to include database name, backup type (Full, Differential, and Log) along with a timestamp of backup execution. For example, I will define the backup file name as dbname-backuptype-timestamp.bak BackupFileName:指定数据库备份文件的格式。 我想包括数据库名称,备份类型(完整,差异和日志)以及备份执行的时间戳。 例如,我将备份文件名定义为dbname-backuptype-timestamp.bak
  • ReplaceInName: We need to specify this switch in backup command to replace the strings in backupfilename with actual values ReplaceInName:我们需要在backup命令中指定此开关,以将backupfilename中的字符串替换为实际值

In this example, we are specifying BackupFileName as dbname-backuptype-timestamp.bak. Once we set ReplaceInName, command works as follows

在此示例中,我们将BackupFileName指定为dbname-backuptype-timestamp.bak。 设置ReplaceInName之后,命令的工作方式如下

  • dbname – it replaces dbname with the actual database name

    dbname –用实际的数据库名称替换dbname
  • timestamp – It specifies timestamp in BackupFileName

    时间戳–在BackupFileName中指定时间戳
  • backuptype – We get backup type in the BackupFileName

    backuptype –我们在BackupFileName中获取备份类型

Run the following command in PowerShell SQL Server.

在PowerShell SQL Server中运行以下命令。

>Backup-DBADatabase -SqlInstance  Kashish\SQL2019CTP -Type Differential -Database SQLShackDemo  -BackupDirectory C:\TEMP\Backup -BackupFileName dbname-backuptype-timestamp.bak -ReplaceInName

Now look at the database backup directory and notice the backup file name.

现在查看数据库备份目录,并注意备份文件名。

We might also want to add SQL instance name in the backup file name; therefore, you can run the following command with instancename parameter in BackupFileName.

我们可能还想在备份文件名中添加SQL实例名。 因此,您可以在BackupFileName中使用instancename参数运行以下命令。

>Backup-DBADatabase -SqlInstance  Kashish\SQL2019CTP -Type Differential -Database SQLShackDemo  -BackupDirectory C:\TEMP\Backup - BackupFileName instancename-dbname-backuptype-timestamp.bak -ReplaceInName

We can see SQL instance name in a backup file as well.

我们也可以在备份文件中看到SQL实例名称。

Similarly, add a servername in a SQL database backup file name to specify server name as well.

同样,在SQL数据库备份文件名中添加服务器名以也指定服务器名。

>Backup-DBADatabase -SqlInstance  Kashish\SQL2019CTP -Type Differential -Database SQLShackDemo  -BackupDirectory C:\TEMP\Backup -BackupFileName servername-instancename-dbname-backuptype-timestamp.bak -ReplaceInName

使用DBATools将SQL数据库备份到单独的文件夹中 (SQL Database backups into a separate folder using DBATools)

Suppose we want to create a separate folder for each database and all of its SQL database backups should go under it. It allows us to look for all database backups in a specified directory easily. If we have a large number of databases, it is not possible to manually create a folder and specify in backup command. DBATools solves these issues with -CreateFolder string. It automatically creates a folder for each database in a specified backup directory and takes backup of a particular database.

假设我们要为每个数据库创建一个单独的文件夹,并且所有SQL数据库备份都应位于该文件夹下。 它使我们能够轻松地在指定目录中查找所有数据库备份。 如果我们有大量数据库,则无法手动创建文件夹并在backup命令中指定。 DBATools使用-CreateFolder字符串解决了这些问题。 它会自动为指定备份目录中的每个数据库创建一个文件夹,并备份特定数据库。

Run the following command to take all databases backup in SQL instance in a separate folder for each database.

运行以下命令以将SQL实例中的所有数据库备份到每个数据库的单独文件夹中。

>Backup-DBADatabase -SqlInstance  Kashish\SQL2019CTP  -BackupDirectory C:\TEMP\Backup -BackupFileName dbname-backuptype-timestamp.bak -ReplaceInName -CreateFolder

Now look at the SQL database backup directory, and you can see a separate folder for each database. The SQL Database backup is also placed in a particular database folder.

现在查看SQL数据库备份目录,您可以看到每个数据库的单独文件夹。 SQL数据库备份也放置在特定的数据库文件夹中。

Backup SQL database - Database backs into a separate folder using DBATools

If the folder already exists, it does not create a separate folder or overwrites it. It ignores step to create a directory and takes a backup in the existing directory only.

如果该文件夹已经存在,则不会创建单独的文件夹或将其覆盖。 它忽略创建目录的步骤,仅在现有目录中进行备份。

SQL Server supports backup compression. It is good practice to take compressed backup. We might have backup compression enabled at the instance level. We do not want to check compression configuration at instance level each time. We should specify compression in backup command as well to ensure the backup is compressed.

SQL Server支持备份压缩。 最好执行压缩备份。 我们可能在实例级别启用了备份压缩。 我们不想每次都在实例级别检查压缩配置。 我们还应该在backup命令中指定压缩,以确保备份被压缩。

使用DBATools压缩备份 (Compressed backup using DBATools)

We can use -CompressBackup string to take compressed backup using DBATools command.

我们可以使用-Co​​mpressBackup字符串使用DBATools命令进行压缩备份。

For this demo, I have taken a SQL database backup of SQLShackDemo with and without compression to show the difference.

在此演示中,我已进行了SQLShackDemoSQL数据库备份(带压缩和不带压缩)以显示差异。

  • SQL database backup size with compression: 94.7 MB

    带压缩SQL数据库备份大小:94.7 MB
  • SQL database backup size without compression: 187 MB

    不压缩SQL数据库备份大小:187 MB

使用DBATools的仅复制SQL数据库备份 (Copy-Only SQL database backup using DBATools)

We define a backup policy to take regular database backups. Sometimes we get the requirement for an ad-hoc backup. This ad-hoc backup might affect the backup LSN chain and we might need to reinitiate backup chain. SQL Server provides a solution in terms of copy-only backup. By default, DBATools takes a normal full backup of a database. We can specify CopyOnly string in backup command to take copy-only backup. It does not impact the LSN of database backups.

我们定义了备份策略以进行常规数据库备份。 有时我们会收到临时备份的要求。 此临时备份可能会影响备份LSN链,我们可能需要重新启动备份链。 SQL Server提供了仅复制备份方面的解决方案。 默认情况下,DBATools将对数据库进行常规的完整备份。 我们可以在backup命令中指定CopyOnly字符串以执行仅复制备份。 它不会影响数据库备份的LSN。

>Backup-DBADatabase -SqlInstance  Kashish\SQL2019CTP  -BackupDirectory C:\TEMP\Backup -Database SQLShackDemo -BackupFileName dbname-backuptype-timestamp.bak -ReplaceInName  -CompressBackup -CopyOnly

使用DBATools进行SQL数据库备份验证和确认 (SQL database backup verification and validation using DBATools)

We want to perform database backup validation to avoid any corruption while writing backup in the media. It ensures that we have a compatible and verified backup file. SQL Server provides following backup validations.

我们希望执行数据库备份验证,以避免在介质中写入备份时发生任何损坏。 它确保我们具有兼容且经过验证的备份文件。 SQL Server提供以下备份验证。

  • Perform CheckSum before writing to media

    在写入媒体之前执行CheckSum
  • Verify backup when finished

    完成后验证备份

We can specify Checksum and Verify string to do these validations. We should add these strings to have a consistent backup and avoid any issues during database restoration.

我们可以指定Checksum和Verify字符串来进行这些验证。 我们应该添加这些字符串以具有一致的备份,并避免在数据库还原期间出现任何问题。

>Backup-DBADatabase -SqlInstance  Kashish\SQL2019CTP  -BackupDirectory C:\TEMP\Backup -Database SQLShackDemo -BackupFileName dbname-backuptype-timestamp.bak -ReplaceInName  -CompressBackup -CopyOnly -CheckSum –Verify

使用DBATools备份事务日志 (Transaction log backup using DBATools)

We can specify -Type Log in backup command to take a transaction log backup.

我们可以指定-Type Log in backup命令进行事务日志备份。

>Backup-DBADatabase -SqlInstance  Kashish\SQL2019CTP -Type Log -Database msdb -BackupDirectory C:\TEMP\Backup\

结论 (Conclusion)

In this article, we explored about database backups using PowerShell SQL Server module DBA-Tools. We can take backups multiple configurations and format using DBATools. We will cover database restoration with DBATools in my next article.

在本文中,我们探讨了使用PowerShell SQL Server模块DBA-Tools进行数据库备份的方法。 我们可以使用DBATools备份多种配置和格式。 我的下一篇文章将介绍使用DBATools进行数据库还原。

目录 (Table of contents)

SQL Database Backups using PowerShell Module – DBATools
使用PowerShell模块SQL数据库备份– DBATools

翻译自:

转载地址:http://nyiwd.baihongyu.com/

你可能感兴趣的文章
地图平移等地图操作功能
查看>>
ubuntu chmod命令的使用
查看>>
bzoj4554: [Tjoi2016&Heoi2016]游戏
查看>>
Linux解压命令大全
查看>>
c++、java、oc函数的重载及部分代码
查看>>
Python基础之条件表达式、运算符
查看>>
TCP/UDP简易通信框架源码,支持轻松管理多个TCP服务端(客户端)、UDP客户端
查看>>
【UWP开源】图片编辑器,带贴图、滤镜、涂鸦等功能
查看>>
HDU - 1525 博弈 暴力分析
查看>>
pod 安装 Masonry 遇到问题
查看>>
(转)OpenCV中的常用函数
查看>>
poj 3264 Balanced Lineup(线段树、RMQ)
查看>>
CSS实现水平垂直居中
查看>>
使用js实现水波效果
查看>>
Codeforces 215D. Hot Days(贪心)
查看>>
oracle exams
查看>>
简化你的Java代码,让工作更高效
查看>>
Java内存泄漏分析与解决方案
查看>>
【八皇后问题】 回溯算法
查看>>
二叉树的遍历算法
查看>>