简体   繁体   中英

MySQL Dump with PowerShell

$mysqlpath = "C:\Program Files\MySQL\MySQL Server 5.6\bin"
$backuppath = "C:\Users\Tiffany\Downloads"
$username = "user"
$password = "123123"
$database = "db"
$errorLog = "error_dump.log"

$date = Get-Date
$timestamp = "" + $date.day + $date.month + $date.year + "_" + $date.hour + $date.minute

$backupfile = $backuppath + $database + "_" + $timestamp +".sql"

CD $mysqlpath
.\mysqldump.exe --user=$username --password=$password --log-error=$errorLog --result-file=$backupfile --databases $database

CD $backuppath
$oldbackups = gci *.sql*

for($i=0; $i -lt $oldbackups.count; $i++){
    if ($oldbackups[$i].CreationTime -lt $date.AddMonths(-1)){
        $oldbackups[$i] | Remove-Item -Confirm:$false
    }
}

However, I keep getting the following:

mysqldump.exe : Warning: Using a password on the command line interface can be insecure.
At C:\Users\Tiffany\Desktop\mysqldump.ps1:14 char:16
+ .\mysqldump.exe <<<<  --user=$username --password=$password --log-error=$errorLog --result-file=$backupfile --databases $database
    + CategoryInfo          : NotSpecified: (Warning: Using ...an be insecure.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Do I need to set a flag to allow this commandline?

I would sidestep that question and use a my.cnf to store credentials instead of storing such data in the powershell script, see How to perform a mysqldump without a password prompt? :

[mysqldump]
user=mysqluser
password=secret

http://dev.mysql.com/doc/refman/5.5/en/option-files.html http://dev.mysql.com/doc/refman/5.5/en/password-security-user.html

Handing over the credentials via command line options will make them show up in the process listing (depending a bit on who looks onto the process list, but still).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM