Avatar of the404guy
the404guy

asked on 

PS Script to restore latest .sql backup file in to database

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

Open in new window

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
PowershellMySQL Server

Avatar of undefined
Last Comment
the404guy

8/22/2022 - Mon