Sql-server – SQL Server 2014 SP2, backup and restore with powershell

backupsql serversql server 2014

I have a task where i have to take the backup with encryption and restore it to destination using the powershell.

I used the invoke-sql command and use the tsql in it but this command is not reliable for us rather we want to use the SQL powershell i.e backup-sqldatabase & restore sqldatabase.

my questions are what are the process to perform it (please i dont want to use invoke-sqlcmd)

  • create master using powershell ( i use "create master key encryption
    by password = 'Abcd1234!'" in invoke-sqlcommand)
  • Create certificate using powershell
  • backup-certificate
  • create backup using encryption

Best Answer

You can use dbatools which is powershell based.

For your case you need to have a 2 step approach.

  1. use the cmdlets in Backup and Restore – Certificates

Backup-DbaDbCertificate

Backup-DbaDbMasterKey

Restore-DbaDbCertificate

then use Copy-DbaDatabase with -BackupRestore switch. Alternately, you can use Backup-DbaDatabase and then Restore-DbaDatabase as per your needs.