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数据库备份也放置在特定的数据库文件夹中。

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)

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/sql-database-backups-using-powershell-module-dbatools/

使用PowerShell模块SQL数据库备份– DBATools相关推荐

  1. ASP中怎么实现SQL数据库备份、恢复!

    选择自 hanghwp 的 Blog 1.ASP中怎么实现SQL数据库备份.恢复! 答:asp在线备份sql server数据库: 1.备份 <% SQL="backup databa ...

  2. 系统损坏sql数据库备份_如何识别损坏SQL备份文件

    系统损坏sql数据库备份 A Database administrator's key task is to keep the database healthy and available for t ...

  3. sql数据库备份默认路径_在Linux上SQL Server中更改默认数据库文件和备份路径

    sql数据库备份默认路径 In a previous article, we explored the process to change default SQL dump file location ...

  4. SQL数据库备份及还原

    在对数据库的实际操作中,数据库的备份及还原十分重要.他不仅能很好的做到服务器容灾,又能有效的解决因为误操作带来的数据问题,还能为开发团队测试团队提供最新的用户动态,及时响应和调整用户最新软件情况.是I ...

  5. C#+SQL数据库备份和还原

    使用前要导入SQLDMO.dll(在com组件中导入Microsoft SQLDMO Object Library即可)/// /// DbOper类,主要应用SQLDMO实现对Microsoft S ...

  6. C#+Sql数据库备份

    1.在项目中添加SQLDmo dll文件引用(SQLDMO(SQL Distributed Management Objects,SQL分布式管理对象)) 2在相应页面加using SQLDMO引用 ...

  7. MS SQL数据库备份和恢复

    数据库备份和恢复存储过程(和大家交流一下) if exists( select * from sysobjects where name = ' pr_backup_db ' and xtype = ...

  8. SQL数据库备份与删除

    下面将介绍SQL Server自带的数据库备份方法,希望可以帮助大家解决日常备份. 第一步:登录数据库->管理->维护计划->维护计划向导 第二步:输入维护计划"名称&qu ...

  9. MS SQL数据库备份和恢复存储过程

    /**//*备份数据库*/ create proc pr_backup_db @flag varchar(20) out, @backup_db_name varchar(128), @filenam ...

最新文章

  1. python numpy创建数组方法汇总array() zeros() zeros_like() ones() ones_like() empty() empty_like() arange eyes
  2. 北斗导航 | 卫星导航基础知识(卫星通信调制技术:信号调制、解调:ASK、FSK、PSK:FDMA、TDMA、CDMA、SDMA)
  3. DOM-7 【兼容】事件处理函数、冒泡捕获、阻止冒泡默认事件
  4. LeetCode 876 Middle of the Linked List 解题报告
  5. 使用app测试Modelarts在线服务
  6. React学习笔记:入门案例
  7. wr703n 4m固件 带打印机服务器_我的WIFI打印机
  8. 计算机无法安装应用,编程软件无法安装或报错
  9. vscode禁用 json 添加注释时的报错
  10. 045 Android Studio 常用应用
  11. 通过定制控制在屏幕上显示图片
  12. 松下电视切换html,松下等离子电视如何用HDMI连接电脑?
  13. 14种鼻型图解_十种鼻型分类图详细解析
  14. Python编程定义函数参数的小tips
  15. 【阅】天才在左 疯子在右
  16. PS_1_认识主界面_新建文档(分辨率)_打开保存(序列动画)
  17. datedif函数mysql_datedif函数怎么用
  18. 【Opencv综合应用】自制训练集的人脸识别2——制作csv文件
  19. 地址总线、字长和寻址空间的关系
  20. echart 广州3d_echarts绘制3D城市地图

热门文章

  1. 漏洞扫描工具有哪些_5.4k Star!简单又全面的容器漏洞扫描工具:Trivy
  2. nopcommerce 二次开发
  3. lua本学习笔记功能
  4. 在net.tcp模式下,由SvcUtil.exe生成代理类文件和配置文件(转)
  5. 关于iframe的一点发现与思考
  6. 【Vue】npm run serve 和 npm run dev 有什么区别
  7. python time模块计时_python中计时模块timeit的使用方法
  8. 怎么让无线网络共享打印机设置
  9. 有时我都非常讨厌自己,特别容易相信人
  10. 理财产品利息可每天提取吗?