简体   繁体   English

如何使用PowerShell从产品备份中刷新开发数据库

[英]How to use PowerShell to refresh a dev database from backup of prod

We are using DDBoost to backup and restore SQL Server databases. 我们正在使用DDBoost备份和还原SQL Server数据库。

Now we want to create a script or a job, so developers can kick off the script or the job to refresh their dev databases without asking a DBA. 现在,我们要创建一个脚本或作业,以便开发人员可以启动脚本或作业来刷新其开发数据库,​​而无需询问DBA。

I know in SQL Server Management Studio we can't take input in Job so I want to create a script with T-SQL or CLI or Powershell to take inputs like SourceDB, TargetDB and then refresh a dev database using with the parameters. 我知道在SQL Server Management Studio中我们无法在Job中接受输入,因此我想使用T-SQL或CLI或Powershell创建脚本来接受SourceDB,TargetDB之类的输入,然后使用参数刷新开发数据库。

I know how to take input in PowerShell so if someone can tell how to do it either by: 我知道如何在PowerShell中进行输入,因此,如果有人可以通过以下方法告诉输入方法:

  • using powershell to restore from DDBoost 使用Powershell从DDBoost还原
  • passing the values from PowerShell to T-SQL or CLI 将值从PowerShell传递到T-SQL或CLI
  • any other option in SQL Server management Studio. SQL Server Management Studio中的任何其他选项。

Please help me setting it up. 请帮助我进行设置。

Thanks and regards in advance. 谢谢和提前问候。

Shoaib Shoaib

I'm going to ignore the fact about DDBoost, because I don't know it. 我将忽略有关DDBoost的事实,因为我不知道。

But I know about dbatools.io , which is very solid powershell module that does exactly what you are looking for. 但是我知道dbatools.io ,它是一个非常坚固的powershell模块,可以完全满足您的需求。 It can with one line of powershell restore an entire database. 它可以用一行Powershell还原整个数据库。 You can make it as simple or advanced as you want. 您可以根据需要将其设置为简单或高级。

Prerequisites: 先决条件:

  1. A working SQL Server backup file. 一个有效的SQL Server备份文件。 This can be created from SQL Server Management Studio or dbatools.io 可以从SQL Server Management Studio或dbatools.io创建
  2. A SQL Server SQL Server
  3. The executing user needs privileges to restore the database. 执行用户需要特权来还原数据库。
  4. Install dbatools 安装dbatools

Installation 安装

Start PowerShell (Run As Administrator) 启动PowerShell(以管理员身份运行)

Install-Module dbatools

Backup database 备份资料库

Here I'll show how you can backup a database using dbatools. 在这里,我将展示如何使用dbatools备份数据库。

Backup-DbaDatabase -SqlInstance servername -Database databasename -BackupDirectory C:\Temp -CompressBackup

Restore database 恢复数据库

Here I'll show how you can restore a database using dbatools. 在这里,我将展示如何使用dbatools还原数据库。

Restore-DbaDatabase -SqlInstance servername -DatabaseName databasename -Path C:\Temp\filename.bak -WithReplace -useDestinationDefaultDirectories -ReplaceDbNameInFile

The restore script instructs the SQL Server to overwrite any existing database. 还原脚本指示SQL Server覆盖任何现有数据库。 It will restore the database files into the folders that are configured inside the SQL Server configuration and it will replace the database name in the physical files. 它将数据库文件还原到SQL Server配置中配置的文件夹中,并将替换物理文件中的数据库名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM