0

As another post (Script for MySQL backup to multiple files), I received help to create a Powershell script that creates backup of MySQL databases and generates multiple files, one for each database. As can be seen, the script makes a pipeline between a command mysql and mysqldump.

My intention now is to eliminate the user information and password directly in the script. As another link (How to perform a mysqldump without a password prompt?), I created the my.cnf configuration file MYSQL_HOME, passing the information on [mysqldump], and used the flag --defaults-extra-file. The problem is that this flag does not work for mysql.exe, so could not use this solution.

To avoid leaving the user and password information directly in the script, I used another post (How to handle command-line arguments in PowerShell), which shows how to configure parameters input into Powershell scripts. With that, my script looked like this:

param (
    [string]$username = $(throw "-username is required."),
    [string]$password = $(Read-Host "Input password, please" )
)

$BACKUPDATE = Get-Date -UFormat "%Y-%m-%d_%H%M%S"
$BKPFOLDER='E:\bkp'
$MYSQL_HOME="C:\MYSQL"

Set-Location "$MYSQL_HOME\bin"
  & .\mysql.exe -N -s -r -u $username -p$password -e 'show databases' | % {
    & .\mysqldump.exe -u $username -p$password --single-transaction $_ |
      Out-File "$BKPFOLDER\${_}_$BACKUPDATE.sql" -Encoding Ascii
 }  

When I run the following command:

test.ps1 -username bkpuser -password mypass

I get the following message:

ERROR 1045 (28000): Access denied for user 'bkpuser'@'localhost' (using password: YES)

But there is no access permission problem, because if I replace the values ​​of $usename and $password to call the mysql and mysqldump by correct values ​​(excluding the parameter), the command works.

What should I change?

Community
  • 1
  • 1
Rogério Arantes
  • 712
  • 1
  • 8
  • 29

1 Answers1

2

PowerShell's parser can't determine where commandline argument ends and vairable name starts. You can see this clearly in ISE, because $password should be red, but it's blue:

PS ISE

Just add space between -p and $password or use "=": --user=$username --password=$password

beatcracker
  • 6,714
  • 1
  • 18
  • 41