Board logo

标题: [其他] 批处理版mysql数据备份 [打印本页]

作者: 小勇12    时间: 2011-9-18 17:21     标题: 批处理版mysql数据备份

  1. @echo off&setlocal enabledelayedexpansion
  2. title code by xiaoyong12
  3. mode con cols=36 lines=14
  4. net start mysql >nul 2>nul
  5. set username=root
  6. set password=root
  7. echo ***********************************
  8. echo *                                 *
  9. echo *                                 *
  10. echo *        1、数据备份              *
  11. echo *                                 *
  12. echo *                                 *
  13. echo *                                 *
  14. echo *        2、数据恢复             *
  15. echo *                                 *
  16. echo *                                 *
  17. echo ***********************************
  18. set/p item=    请选择(1-2)
  19. if exist database.sql del database.sql>nul
  20. set count=0
  21. set len=0
  22. set DataBaseName=all
  23. set OnlyName=
  24. set DataBaseRecoverList=数据库列表 ALL
  25. set DataBaseBakList=数据库列表 `ALL`
  26. if !item! equ 1 goto bak
  27. if !item! equ 2 goto recovery
  28. :bak
  29. cls
  30. mode con cols=60 lines=18
  31. if exist database.sql del database.sql>nul
  32. mysqldump -u!username! -p!password! -A -d -t -N -n>database.sql
  33. for /f "skip=2 tokens=4" %%i in ('find database.sql "Current Database"') do  (
  34. set DataBaseBakList=!DataBaseBakList!%%i
  35. set tmp=%%i
  36. set DataBaseName=!DataBaseName!!tmp:~1,-1!
  37. set OnlyName=!OnlyName!!tmp:~1,-1!
  38. )
  39. del database.sql>nul
  40. for /l %%i in (1,1,5) do echo=
  41. for /l %%i in (1,1,20) do set/p= <nul
  42. for /l %%i in (1,1,12) do set/p=■<nul
  43. for %%j in (%DataBaseBakList%) do (
  44. echo=
  45. for /l %%i in (1,1,20) do set/p= <nul
  46. set/p=■<nul
  47. set /p=     <nul
  48. if !count! neq 0 set/p=!count! <nul
  49. call :length %%j
  50. set/p=%%j<nul
  51. if !count! neq 0 (set/a sp=12-!len) else (set/a sp=12-!len-2)
  52. for /l %%i in (1,1,!sp!) do set/p= <nul
  53. set/p=■<nul
  54. set /a count=!count!+1
  55. )
  56. echo=
  57. for /l %%i in (1,1,20) do set/p= <nul
  58. for /l %%i in (1,1,12) do set/p=■<nul
  59. echo=
  60. for /l %%i in (1,1,26) do set/p= <nul
  61. set /a count=!count!-1
  62. set /p choice=请选择(1-!count!)
  63. set cnt=1
  64. for %%i in (!DataBaseName!) do (
  65. if !choice! equ 1 (
  66.      for %%j in (!OnlyName!) do (
  67.             mysqldump -u!username! -p!password! %%j>%%j.sql
  68.       )
  69.     ) else (
  70.   if !cnt! equ !choice! mysqldump -u!username! -p!password! %%i>%%i.sql&goto break
  71. )
  72. set /a cnt=!cnt!+1
  73. )
  74. :break
  75. echo 数据库备份完成。。&ping /n 3 127.0.0.1>nul&exit
  76. :recovery
  77. cls
  78. mode con cols=60 lines=18
  79. for /f %%i in ('dir /b /a-d *.sql') do (
  80. if not "%%~ni" == "mysql" (
  81. set DataBaseRecoverList=!DataBaseRecoverList!%%~ni
  82. set DataBaseName=!DataBaseName!%%~ni
  83. set OnlyName=!OnlyName!%%~ni )
  84. )
  85. for /l %%i in (1,1,5) do echo=
  86. for /l %%i in (1,1,20) do set/p= <nul
  87. for /l %%i in (1,1,12) do set/p=■<nul
  88. for %%j in (%DataBaseRecoverList%) do (
  89. echo=
  90. for /l %%i in (1,1,20) do set/p= <nul
  91. set/p=■<nul
  92. set /p=     <nul
  93. if !count! neq 0 set/p=!count! <nul
  94. call :length %%j
  95. set/p=%%j<nul
  96. if !count! neq 0 (set/a sp=12-!len) else (set/a sp=12-!len-2)
  97. for /l %%i in (1,1,!sp!) do set/p= <nul
  98. set/p=■<nul
  99. set /a count=!count!+1
  100. )
  101. echo=
  102. for /l %%i in (1,1,20) do set/p= <nul
  103. for /l %%i in (1,1,12) do set/p=■<nul
  104. echo=
  105. for /l %%i in (1,1,26) do set/p= <nul
  106. set /a count=!count!-1
  107. set /p choice=请选择(1-!count!)
  108. set cnt=1
  109. for %%i in (!DataBaseName!) do (
  110. if !choice! equ 1 (
  111.      for %%j in (!OnlyName!) do (
  112.             echo Y|mysqladmin -u%username% -p%password% drop %%j>nul 2>nul
  113.             mysqladmin -u%username% -p%password%  create %%j
  114.             mysql -u%username% -p%password%  %%j<%%j.sql
  115.       )
  116.     ) else (
  117.   if !cnt! equ !choice! (
  118.     echo Y|mysqladmin -u%username% -p%password% drop %%i>nul 2>nul
  119.     mysqladmin -u%username% -p%password%  create %%i
  120.     mysql -u%username% -p%password%  %%i<%%i.sql
  121.    )&goto out
  122. )
  123. set /a cnt=!cnt!+1
  124. )
  125. :out
  126. echo 数据库恢复完成。。&ping /n 3 127.0.0.1>nul&exit
  127. :length
  128. set str=%1
  129. set b=0
  130. :b
  131. if "!str!"=="" set /a len=0 &goto :eof
  132. if "!str:~%b%,1!"=="" (set len=!b!&goto :eof) else set /a b+=1&goto b
复制代码

作者: honmung    时间: 2012-4-7 14:16

这个可以等下来试试效果




欢迎光临 批处理之家 (http://bathome.net./) Powered by Discuz! 7.2