简体   繁体   English

导入SQLPS模块并使用它来将数据库备份和还原到其他服务器

[英]Import SQLPS module and use it to backup and restore DB to different server

I am running PS script from the client machine (Win7) that does not have SQLPS (Ms SQL Server Database Engine) installed. 我正在从未安装SQLPS(MS SQL Server数据库引擎)的客户端计算机(Win7)运行PS脚本。

Due to this, I do it through importing a remote ps-session: 因此,我通过导入远程ps会话来实现:

  1. Create remote PS-Session to the SQL Server 创建到SQL Server的远程PS会话

$sourceSQLRemoteSession = New-PSSession -ComputerName $SQLServerHostname -Authentication negotiate -Credential $PSOCredentialObject $ sourceSQLRemoteSession = New-PSSession -ComputerName $ SQLServerHostname-身份验证-Credential $ PSOCredentialObject

  1. Import the SQLPS module 导入SQLPS模块

Import-PSSession -Session $sourceSQLRemoteSession -Module SQLPS -DisableNameChecking Import-PSSession-会话$ sourceSQLRemoteSession-模块SQLPS -DisableNameChecking

  1. Run SQL Backup 运行SQL备份

Backup-SqlDatabase -ServerInstance "destinationSQL\\SQLInstance" -Database "blabla_db" -BackupFile "c:\\blabla.bak" -Credential $PSOCredentialObject Backup-SqlDatabase -ServerInstance“ destinationSQL \\ SQLInstance”-数据库“ blabla_db” -BackupFile“ c:\\ blabla.bak”-凭据$ PSOCredentialObject

The thing is, if I need to restore the DB to a different SQL database server, require me to use "RelocateFile" objects, for example: 问题是,如果需要将数据库还原到其他SQL数据库服务器,则要求我使用“ RelocateFile”对象,例如:

$RelocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("uat_project1_db", "c:\SQLDATA\blabla.mdf")
$RelocateLog = New-Object Microsoft.SqlServer.Management.Smo.RelocateFile("uat_project1_db_Log", "c:\SQLDATA\blabla.ldf")
Restore-SqlDatabase -ServerInstance "Computer\Instance" -Database "MainDB" -BackupFile "\\mainserver\databasebackup\MainDB.trn" -RelocateFile @($RelocateData,$RelocateLog)

And when I run the new object, it does not seem to like it: 当我运行新对象时,它似乎不喜欢它:

Error: 错误:

New-Object : Cannot find type [Microsoft.SqlServer.Management.Smo.RelocateFile]: verify that the assembly containing this type is loaded.At line:1 char:17
+ ... ocateData = New-Object Microsoft.SqlServer.Management.Smo.RelocateFil ...
+               ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo         : InvalidType: (:) [New-Object], PSArgumentException
    + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

What's the best method to approach this? 解决此问题的最佳方法是什么?

Thanks 谢谢

Your best approach would be to use dbaTools . 最好的方法是使用dbaTools Check it out! 看看这个! You can install from the PowerShell Gallery, though if you're running Windows 7 you may have to upgrade PowerShell to v3 or above (go for v5! ) to make use of the PowerShell Gallery. 您可以从PowerShell库中进行安装,尽管如果您正在运行Windows 7,则可能必须将PowerShell升级到v3或更高版本(适用于v5! )才能使用PowerShell库。

dbaTools has a single command for backing up and restoring DBs. dbaTools有一个用于备份和还原数据库的命令。

Here's an example from their documentation : 这是他们的文档中的一个示例:

Copy-DbaDatabase -Source sqlserver2014a -Destination sqlcluster -Exclude Northwind, pubs, AdventureWorks -BackupRestore -NetworkShare \\\\fileshare\sql\migration

Hope that helps 希望能有所帮助

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

相关问题 无法将SQLPS CmdLet Backup-SQLDatabase与-InputObject参数一起使用 - Unable to use SQLPS CmdLet Backup-SQLDatabase with -InputObject parameter Powershell 导入模块 SqlServer 和 SQLPS 不相处 - Powershell Import-Module SqlServer and SQLPS dont get along SQL Server 2012 SQLPs 模块 - 自动更改当前位置? - SQL Server 2012 SQLPs Module - Changing current location automatically? 使用SQLPS创建差异备份 - Create a Differential backup using SQLPS PowerShell 4 - Import-Module:未加载指定的模块“SQLPS”,因为在任何模块目录中都找不到有效的模块文件 - PowerShell 4 - Import-Module : The specified module 'SQLPS' was not loaded because no valid module file was found in any module directory Powershell SqlPs模块无法正确导入 - Powershell SqlPs module not importing properly #sqlps-连接到服务器进行查询时出现问题 - #sqlps - Problems connecting to the server to query 如何使用PowerShell SQLPS模块浏览SQL Server 2012 LocalDB对象的层次结构? - How to navigate through the hierarchy of SQL Server 2012 LocalDB objects using the PowerShell SQLPS module? 指定的模块“SQLPS”未在azure VM中加载 - The specified module 'SQLPS' was not loaded in azure VM sql server cmdlet(sqlps)和远程服务器 - sql server cmdlet (sqlps) and remote server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM