标题: [其他] 批处理版mysql数据备份 [打印本页]
作者: 小勇12 时间: 2011-9-18 17:21 标题: 批处理版mysql数据备份
- @echo off&setlocal enabledelayedexpansion
- title code by xiaoyong12
- mode con cols=36 lines=14
- net start mysql >nul 2>nul
- set username=root
- set password=root
- echo ***********************************
- echo * *
- echo * *
- echo * 1、数据备份 *
- echo * *
- echo * *
- echo * *
- echo * 2、数据恢复 *
- echo * *
- echo * *
- echo ***********************************
- set/p item= 请选择(1-2)
- if exist database.sql del database.sql>nul
- set count=0
- set len=0
- set DataBaseName=all
- set OnlyName=
- set DataBaseRecoverList=数据库列表 ALL
- set DataBaseBakList=数据库列表 `ALL`
- if !item! equ 1 goto bak
- if !item! equ 2 goto recovery
- :bak
- cls
- mode con cols=60 lines=18
- if exist database.sql del database.sql>nul
- mysqldump -u!username! -p!password! -A -d -t -N -n>database.sql
- for /f "skip=2 tokens=4" %%i in ('find database.sql "Current Database"') do (
- set DataBaseBakList=!DataBaseBakList!%%i
- set tmp=%%i
- set DataBaseName=!DataBaseName!!tmp:~1,-1!
- set OnlyName=!OnlyName!!tmp:~1,-1!
- )
- del database.sql>nul
- for /l %%i in (1,1,5) do echo=
- for /l %%i in (1,1,20) do set/p= <nul
- for /l %%i in (1,1,12) do set/p=■<nul
-
- for %%j in (%DataBaseBakList%) do (
- echo=
- for /l %%i in (1,1,20) do set/p= <nul
- set/p=■<nul
- set /p= <nul
- if !count! neq 0 set/p=!count! <nul
- call :length %%j
- set/p=%%j<nul
- if !count! neq 0 (set/a sp=12-!len) else (set/a sp=12-!len-2)
- for /l %%i in (1,1,!sp!) do set/p= <nul
- set/p=■<nul
- set /a count=!count!+1
- )
- echo=
- for /l %%i in (1,1,20) do set/p= <nul
- for /l %%i in (1,1,12) do set/p=■<nul
- echo=
- for /l %%i in (1,1,26) do set/p= <nul
- set /a count=!count!-1
- set /p choice=请选择(1-!count!)
- set cnt=1
- for %%i in (!DataBaseName!) do (
- if !choice! equ 1 (
- for %%j in (!OnlyName!) do (
- mysqldump -u!username! -p!password! %%j>%%j.sql
- )
- ) else (
- if !cnt! equ !choice! mysqldump -u!username! -p!password! %%i>%%i.sql&goto break
- )
- set /a cnt=!cnt!+1
- )
- :break
- echo 数据库备份完成。。&ping /n 3 127.0.0.1>nul&exit
-
-
-
- :recovery
- cls
- mode con cols=60 lines=18
- for /f %%i in ('dir /b /a-d *.sql') do (
- if not "%%~ni" == "mysql" (
- set DataBaseRecoverList=!DataBaseRecoverList!%%~ni
- set DataBaseName=!DataBaseName!%%~ni
- set OnlyName=!OnlyName!%%~ni )
- )
- for /l %%i in (1,1,5) do echo=
- for /l %%i in (1,1,20) do set/p= <nul
- for /l %%i in (1,1,12) do set/p=■<nul
-
- for %%j in (%DataBaseRecoverList%) do (
- echo=
- for /l %%i in (1,1,20) do set/p= <nul
- set/p=■<nul
- set /p= <nul
- if !count! neq 0 set/p=!count! <nul
- call :length %%j
- set/p=%%j<nul
- if !count! neq 0 (set/a sp=12-!len) else (set/a sp=12-!len-2)
- for /l %%i in (1,1,!sp!) do set/p= <nul
- set/p=■<nul
- set /a count=!count!+1
- )
- echo=
- for /l %%i in (1,1,20) do set/p= <nul
- for /l %%i in (1,1,12) do set/p=■<nul
- echo=
- for /l %%i in (1,1,26) do set/p= <nul
- set /a count=!count!-1
- set /p choice=请选择(1-!count!)
- set cnt=1
- for %%i in (!DataBaseName!) do (
- if !choice! equ 1 (
- for %%j in (!OnlyName!) do (
- echo Y|mysqladmin -u%username% -p%password% drop %%j>nul 2>nul
- mysqladmin -u%username% -p%password% create %%j
- mysql -u%username% -p%password% %%j<%%j.sql
- )
- ) else (
- if !cnt! equ !choice! (
- echo Y|mysqladmin -u%username% -p%password% drop %%i>nul 2>nul
- mysqladmin -u%username% -p%password% create %%i
- mysql -u%username% -p%password% %%i<%%i.sql
- )&goto out
- )
- set /a cnt=!cnt!+1
- )
- :out
- echo 数据库恢复完成。。&ping /n 3 127.0.0.1>nul&exit
-
-
-
- :length
- set str=%1
- set b=0
- :b
- if "!str!"=="" set /a len=0 &goto :eof
- if "!str:~%b%,1!"=="" (set len=!b!&goto :eof) else set /a b+=1&goto b
复制代码
作者: honmung 时间: 2012-4-7 14:16
这个可以等下来试试效果
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |