如何创建批处理文件来备份Mysql数据库?

时间:2014-05-22 05:36:18

标签: c# mysql database winforms batch-file

我一直在尝试创建一个备份MySQL数据库的批处理文件。一切正常,但备份只是空的。我不知道哪里出了问题。这是我的代码

set year=%DATE:~10,4%
set day=%DATE:~7,2%
set mnt=%DATE:~4,2%
set hr=%TIME:~0,2%
set min=%TIME:~3,2%

IF %day% LSS 10 SET day=0%day:~1,1%
IF %mnt% LSS 10 SET mnt=0%mnt:~1,1%
IF %hr% LSS 10 SET hr=0%hr:~1,1%
IF %min% LSS 10 SET min=0%min:~1,1%

set backuptime=%year%-%day%-%mnt%-%hr%-%min%
echo %backuptime%
set dbuser=root
set dbpass=vsplabs
set mysqldumpexe="C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe"
set backupfldr="C:\Users\sowmya\Desktop\Mysqlbackups"
set datafldr="C:\ProgramData\MySQL\MySQL Server 5.6\data"
set zipper="c:\MySQLBackups\zip\7za.exe"
pushd %datafldr%
echo "Pass each name to mysqldump.exe and output an individual .sql file for each"
@echo off
FOR /D %%F IN (*) DO (
IF NOT [%%F]==[performance_schema] (
SET %%F=!%%F:@002d=-!
%mysqldumpexe% --user=%dbuser% --password=%dbpass% --databases --routines --log-error=%errorLogPath% %%F > "%backupfldr%%%F.%backuptime%.sql"
) ELSE (
echo Skipping DB backup for performance_schema
)
)
 echo "Zipping all files ending in .sql in the folder"
 %zipper% a -tzip "%backupfldr%FullBackup.%backuptime%.zip" "%backupfldr%*.sql
 echo "done"
 popd

另一个问题我只想要一个数据库从数据库中检索所有数据库。如何修改它并且我已经得到一个名为" mysql"

的额外数据库

1 个答案:

答案 0 :(得分:1)

这可以直接在cmd中运行(我包裹了行但不应该包裹它): 您应该授予root访问权限。

mysql.exe -uroot -p1234 -s -N -e "SHOW DATABASES" |
for /F "usebackq" %D in (`findstr /V "information_schema performance_schema"`)
do mysqldump %D -uroot -p1234 > S:\Backup\MySQL\%D.sql

在批处理文件中,您需要使用额外的%转义%,即使用%% D。