3

The scenario is that we have bunch of .sql dump files and new ones are created before every deployment.

If anything goes wrong with migration scripts someone has to manually drop/create schema and use command line to restore dump from the latest backup.

I am writing a PS script to automate this process.

  1. find latest dump from given path
  2. drop schema
  3. create schema
  4. restore dump.

I have accomplished first 3 steps but have wasted a lot of time on the 4th one:

Write-Host "Restoring: " $path
$command = '"mysql.exe -uUsername -pPassword ' + $dbname + ' < ' + $path + '"'
Write-Host $command
cmd /C $command

It says "The system cannot find the file specified."

If i use cmd $command without /C it starts cmd in powershell but doesn't execute $command.

I have tried different variations to execute the command in cmd but it doesn't seem to work, and the reason i will have to use cmd is because powershell doesn't play well with '<'.

I tried Invoke-Item and Invoke-Expression but can't guarantee i used correct syntax.

Any suggestions would be greatly appreciated.

Thanks

Adil H. Raza
  • 1,649
  • 20
  • 25
  • https://stackoverflow.com/questions/6763086/why-is-input-redirect-not-implemented-in-powershell explains the < operator, and how to accomplish something similar. – Ryan Leach Apr 27 '18 at 00:47
  • Wouldn't it be simpler to create database backups directly before the deployment? Maybe copy off the log and data files as well, just for added safety? – DBADon Apr 26 '19 at 16:26
  • @DBADon that's what the first line of my question says. But i manged to complete the script, i was fairly new with PS back then. – Adil H. Raza May 02 '19 at 20:59

0 Answers0