mysqldump的批处理文件,用于将每个数据库备份到单独的文件中

时间:2012-02-27 21:46:29

标签: mysql windows batch-file windows-server-2008 mysqldump

尝试创建批处理(cmd)文件,以将每个数据库备份到单独的文件中。 经常创建/删除数据库,因此批处理文件需要在每次运行时获取当前的数据库名称并备份每个数据库。

以下是我希望的方式:

mysql -e "show databases" -u root --password=1234
mysqldump %dbname% -u root --password=1234 > S:\Backup\MySQL\%dbname%.sql

是否可以在批处理文件中执行?

请帮忙。感谢。

8 个答案:

答案 0 :(得分:53)

这可以直接在cmd中运行(我包裹了行但不应该包裹它):

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

批处理文件

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

答案 1 :(得分:14)

你会爱上这个

让information_schema数据库构建一个DOS批处理文件以并行执行mysqldump

set MYSQLUSER=root
set MYSQLPASS=1234
set BATCHFILE=S:\Backup\MySQL\Batch_mysqldump.bat 
set DUMPPATH=S:\Backup\MySQL
echo @echo off > %BATCHFILE% 
echo cd %DUMPPATH% >> %BATCHFILE% 
mysql -u%MYSQLUSER% -p%MYSQLPASS% -AN -e"SELECT CONCAT('start mysqldump -u%MYSQLUSER% -p%MYSQLPASS% --routines --triggers ',schema_name,' > ',schema_name,'.sql') FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema','performance_schema')" >> %BATCHFILE% 
type %BATCHFILE% 

就像任何DOS批处理文件一样运行

确保您拥有正确的用户名和密码才能连接到mysql

我刚试了一下以确保

C:\>set MYSQLUSER=lwdba

C:\>set MYSQLPASS=<hidden>

C:\>set BATCHFILE=C:\LWDBA\Batch_mysqldump.bat

C:\>set DUMPPATH=C:\LWDBA

C:\>echo @echo off > %BATCHFILE%

C:\>echo cd %DUMPPATH% >> %BATCHFILE%

C:\>mysql -u%MYSQLUSER% -p%MYSQLPASS% -AN -Bse"SELECT CONCAT('start mysqldump -u%MYSQLUSER% -p%MYSQLPASS% --routines --triggers ',schema_nam
e,' > ',schema_name,'.sql') FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema','performance_schema')" >> %BATCH
FILE%

C:\>type %BATCHFILE%
@echo off
cd C:\LWDBA
start mysqldump -ulwdba -phidden  --routines --triggers a1ex07 > a1ex07.sql
start mysqldump -ulwdba -phidden  --routines --triggers annarbor > annarbor.sql
start mysqldump -ulwdba -phidden  --routines --triggers dilyan_kn > dilyan_kn.sql
start mysqldump -ulwdba -phidden  --routines --triggers dtest > dtest.sql
start mysqldump -ulwdba -phidden  --routines --triggers dude > dude.sql
start mysqldump -ulwdba -phidden  --routines --triggers example > example.sql
start mysqldump -ulwdba -phidden  --routines --triggers fed > fed.sql
start mysqldump -ulwdba -phidden  --routines --triggers friends > friends.sql
start mysqldump -ulwdba -phidden  --routines --triggers giannosfor > giannosfor.sql
start mysqldump -ulwdba -phidden  --routines --triggers javier > javier.sql
start mysqldump -ulwdba -phidden  --routines --triggers johnlocke > johnlocke.sql
start mysqldump -ulwdba -phidden  --routines --triggers junk > junk.sql
start mysqldump -ulwdba -phidden  --routines --triggers lovesh > lovesh.sql
start mysqldump -ulwdba -phidden  --routines --triggers mysql > mysql.sql
start mysqldump -ulwdba -phidden  --routines --triggers nwwatson > nwwatson.sql
start mysqldump -ulwdba -phidden  --routines --triggers part > part.sql
start mysqldump -ulwdba -phidden  --routines --triggers preeti > preeti.sql
start mysqldump -ulwdba -phidden  --routines --triggers prefixdb > prefixdb.sql
start mysqldump -ulwdba -phidden  --routines --triggers replagdb > replagdb.sql
start mysqldump -ulwdba -phidden  --routines --triggers rollup_test > rollup_test.sql
start mysqldump -ulwdba -phidden  --routines --triggers sample > sample.sql
start mysqldump -ulwdba -phidden  --routines --triggers stuff > stuff.sql
start mysqldump -ulwdba -phidden  --routines --triggers table_test > table_test.sql
start mysqldump -ulwdba -phidden  --routines --triggers tagmediatest > tagmediatest.sql
start mysqldump -ulwdba -phidden  --routines --triggers targetdb > targetdb.sql
start mysqldump -ulwdba -phidden  --routines --triggers test > test.sql
start mysqldump -ulwdba -phidden  --routines --triggers test_mysqldb > test_mysqldb.sql
start mysqldump -ulwdba -phidden  --routines --triggers tostinni > tostinni.sql
start mysqldump -ulwdba -phidden  --routines --triggers user1267617 > user1267617.sql
start mysqldump -ulwdba -phidden  --routines --triggers user391986 > user391986.sql
start mysqldump -ulwdba -phidden  --routines --triggers utility > utility.sql
start mysqldump -ulwdba -phidden  --routines --triggers veto > veto.sql
start mysqldump -ulwdba -phidden  --routines --triggers vito > vito.sql
start mysqldump -ulwdba -phidden  --routines --triggers zipcodes > zipcodes.sql

答案 2 :(得分:2)

hey rolando我将您的代码与来自互联网的其他代码结合起来将所有数据库转储到不同的文件,并将其压缩到一个带有日期时间戳的文件中,最后删除超过60天的文件 欢呼声

@echo off
CLS
cd c:\temp
set MYSQLUSER=root
set MYSQLPASS=PassWord
set BATCHFILE=c:\temp\Batch_mysqldump.bat 
set DUMPPATH=c:\temp
SET backuptime=%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4%-%TIME:~0,2%-%TIME:~3,2%
SET backuptimelog=%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2%
echo starting MySqlDump at %backuptime%
echo ------ starting MySqlDump at %backuptimelog% ------   >> "Z:\-=macine backup=-\sqldump\sqldump.log"
echo Running dump...   
set 7zip_path=
mkdir "%backuptime%" >> "Z:\-=macine backup=-\sqldump\sqldump.log"
cd "c:\Program Files\MySQL\MySQL Server 5.6\bin"
echo @echo off > %BATCHFILE% 
echo cd %DUMPPATH% >> %BATCHFILE% 
echo copy "C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe" "c:\temp\%backuptime%" >> %BATCHFILE% 
echo cd "%backuptime%" >> %BATCHFILE% 
mysql -u%MYSQLUSER% -p%MYSQLPASS% -AN -e"SELECT CONCAT('mysqldump -u%MYSQLUSER% -p%MYSQLPASS% ' ,schema_name,' --result-file=',schema_name,'.sql') FROM information_schema.schemata WHERE schema_name NOT IN ('information_schema','performance_schema')" >> %BATCHFILE% 
echo exit >> %BATCHFILE%
start /wait %BATCHFILE% 
echo Compressing bk_%backuptime%.sql...
SET ziptime=%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2%
echo starting 7zip compression at %ziptime%
echo starting 7zip compression at %ziptime% >> "Z:\-=macine backup=-\sqldump\sqldump.log"
"C:\Program Files\7-Zip\7z.exe" a -t7z -m0=PPMd "Z:\-=macine backup=-\sqldump\bk_%backuptime%.7z" "c:\temp\%backuptime%" >> "Z:\-=macine backup=-\sqldump\sqldump.log"
 echo Deleting the SQL file ...   
 rmdir /s /q "c:\temp\%backuptime%" >> "Z:\-=macine backup=-\sqldump\sqldump.log"
 echo deleting files older than 60 days
 echo deleting files older than 60 days >> "Z:\-=macine backup=-\sqldump\sqldump.log"
 forfiles -p "Z:\-=macine backup=-\sqldump" -s -m *.* /D -60 /C "cmd /c del @path" >> "Z:\-=macine backup=-\sqldump\sqldump.log"
 SET finishtime=%DATE:~4,2%-%DATE:~7,2%-%DATE:~10,4% %TIME:~0,2%:%TIME:~3,2%:%TIME:~6,2%
 echo ------ Done at %finishtime%! ------ >> "Z:\-=macine backup=-\sqldump\sqldump.log"
 echo Done at %finishtime%!

答案 3 :(得分:0)

好的,首先......我可能使用的是不同版本的SQL,而且很抱歉,如果是这样的话,你没有在你的问题中列出你的版本所以我只是要给你能与我合作。

我完成了第一部分,但我仍在努力备份数据库。

sqlcmd -U %USER% -P %PASSWORD% -Q"SELECT name FROM sys.databases" > c:\JHA\Synergy\SQL_db_list.txt

还有其他可以使用的触发器,但听起来你会在安装了SQL的实际机器上,对吗?如果是这种情况,它应该默认IP连接到SQL到127.0.0.1或localhost等。

我在想我要为此做的是在批处理脚本中创建一个文件,我将进一步向下调用,将逐行发送命令,类似于在FTP进程中执行的操作批处理脚本。

我拿到它后会更新。

答案 4 :(得分:0)

我没有DOS黑客,但我在我的批处理文件副本中添加了一个更正,以便在时间早于上午10点之前考虑该时间变量中的空白字符。 我在for循环之后将这一行添加到我的批处理文件中:

if "%curtime:~0,1%"==" " set curtime=0%curtime:~1,3%

答案 5 :(得分:0)

我已经尝试了答案,但没有一个能按预期工作,这是我的备份解决方案,但它会为mysql和其他用户数据库创建一个文件。

set USERNAME=root
set PASSWORD=1234
set TIMESTAMP=%DATE:~10,4%.%DATE:~4,2%.%DATE:~7,2%-%TIME:~0,2%.%TIME:~3,2%.%TIME:~6,2%
set BACKUPPATH=D:\Backup\MySql\

if not exist %BACKUPPATH% md %BACKUPPATH%

mysqldump --all-databases --result-file="%BACKUPPATH%%TIMESTAMP%.sql" --user=%USERNAME% --password=%PASSWORD%

答案 6 :(得分:0)

这个脚本更专业&#34;专业&#34;从某种意义上说,当一个数据库转储失败并且哪个失败时,它会通知某人。虽然,我没有备份所有数据库,只有备份数据库。通过获取所有数据库的命令更改SET DBS=内容,可以轻松解决此问题。

编辑:新版本删除了警告消息

@ECHO OFF

:: Configuration part
SET BACKUP_PATH=Backup-MySQL8
SET PHP=C:\Program Files (x86)\PHP\v5.6\php.exe
SET MAIL_TO=to_an_email@domain.com
SET MAIL_FROM=from_an_email@domain.com

SET MYSQL_PATH=C:\Program Files\MySQL\MySQL Server 5.6\bin
SET MYSQL_USER=root
SET MYSQL_PASS=mypassword
SET DBS=database_name1 database_name2


:: Software part
if not exist %BACKUP_PATH% md %BACKUP_PATH%

setlocal EnableDelayedExpansion
SET hasError=0
SET dbsInError=
SET CONFIG_FILE=backup-mysql.cnf

DEL /F /Q %BACKUP_PATH%\* 

echo [mysqldump] > %CONFIG_FILE%
echo user=%MYSQL_USER% >> %CONFIG_FILE%
echo password=%MYSQL_PASS% >> %CONFIG_FILE%

(for %%a in (%DBS%) do (
    "%MYSQL_PATH%\mysqldump.exe" --defaults-extra-file=%CONFIG_FILE% --routines --triggers %%a > %BACKUP_PATH%\%%a.sql
    IF NOT !ERRORLEVEL! == 0 (
        SET hasError=1
        DEL %BACKUP_PATH%\%%a.sql
        SET dbsInError=!dbsInError! %%a
    )
))

DEL %CONFIG_FILE%

IF !hasError! == 1 (
    echo Error... sending email
    "%PHP%" -r "echo (mail('%MAIL_TO%', 'Backup MySQL failed', 'The following database dump failed:!dbsInError!', 'From: %MAIL_FROM%') ? 'Sent' : 'Failed:' . print_r(error_get_last()));"
    echo.
)

echo Backup ended

答案 7 :(得分:-1)

您可以尝试这种直接的方法:

mysqldump databaseName -u root --password=rootPass >  "path\myDBbackup.sql"

<强>步骤:
1.在文本编辑器中键入以上代码并将其另存为批处理文件,例如mybatch.bat
2.从命令提示符中将目录(cd)更改为保存批处理文件的位置 3.键入批处理文件的名称,然后按Enter键,例如mybatch.bat
4.检查数据库架构的位置,即路径