一尘不染

Windows批处理脚本,用于备份本地MySQL数据库,并且仅使用备份文件保留N个最新文件夹

mysql

我正在使用adityasatrio的批处理文件来备份本地MySQL数据库,并且希望仅保留30个最新的备份文件。在此示例中使用root:root。

    @echo off

 set dbUser=root
 set dbPassword=root
 set backupDir="D:\MySQLDumps\dbs\"
 set mysqldump="C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump.exe"
 set mysqlDataDir="C:\wamp\bin\mysql\mysql5.6.17\data"
 set zip="C:\Program Files\7-Zip\7z.exe"

 :: get date
 for /F "tokens=2-4 delims=/ " %%i in ('date /t') do (
      set yy=%%i
      set mon=%%j
      set dd=%%k
 )

 :: get time
 for /F "tokens=5-8 delims=:. " %%i in ('echo.^| time ^| find "current" ') do (
      set hh=%%i
      set min=%%j
 )

 echo dirName=%yy%%mon%%dd%_%hh%%min%
 set dirName=%yy%%mon%%dd%_%hh%%min%

 :: switch to the "data" folder
 pushd %mysqlDataDir%

 :: iterate over the folder structure in the "data" folder to get the databases
 for /d %%f in (*) do (

 if not exist %backupDir%\%dirName%\ (
      mkdir %backupDir%\%dirName%
 )

 %mysqldump% --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > %backupDir%\%dirName%\%%f.sql

 %zip% a -tgzip %backupDir%\%dirName%\%%f.sql.gz %backupDir%\%dirName%\%%f.sql

 del %backupDir%\%dirName%\%%f.sql

 )
 popd

现在,我对以下问题进行了很好的了解:

批处理文件以删除超过N天的文件

https://serverfault.com/questions/49614/delete-files-older-than-x-
days

将7个最新文件保存在一个文件夹中的批处理文件

Windows批处理文件仅保留最后30个文件

现在想知道我是否可以简单地添加(https://stackoverflow.com/a/14267137/1010918

for /f "skip=30 delims=" %%A in ('dir /a:-d /b /o:-d /t:c *.sql ^2^>nul') do if exist "%%~fA" echo "%%~fA"

(是的,我稍后将echo更改为del,但首先我想看看会发生什么)

或(https://stackoverflow.com/a/13368077

for /f "skip=30 eol=: delims=" %%F in ('dir /b /o-d *.sql') do @del "%%F"

在批处理文件的末尾,就在

 del %backupDir%\%dirName%\%%f.sql

做到这一点?

我以前从未做过此事,但已经搜索了MySQL数据库的自动本地备份应用程序/ php脚本/ mysqldump命令/
etc,甚至尝试使用Workbench只是发现在社区版中无法设置任何计划(感谢Oracle) 。

所有其他应用程序都需要有人打开该应用程序并单击“立即运行”,或者希望您为设置时间表付费(不用了,谢谢)。

我认为可以使用Windows 7及更高版本计算机上的工具来完成此操作。请帮助我将此功能添加到脚本中,太好了,谢谢。

编辑1:

当添加引号命令时,什么也不会发生。创建的备份目录也仅显示时间,而不显示年,月和日。做进一步的研究以找出原因。有任何想法吗?

此评论删除所有(除X个最新文件夹外)谈论删除5个最新文件夹,尽管当我像这样使用它时

for /f "skip=2 delims=" %%a in ('dir %backupDir%\%dirName% /o-d /b') do rd /S /Q "%backupDir%\%dirName%\%%a"

错误如下。

The system cannot find the file specified.
The system cannot find the path specified.

edit2:
下面是使用@foxidrive帮助设置我喜欢的文件夹名称的代码,但最后一点是,尝试仅保留3个最新文件夹(仅用于测试目的3个),并删除其中的其余文件夹backupDir似乎无法解决。

感谢您的任何帮助。

     @echo off

 set dbUser=root
 set dbPassword=root
 set "backupDir=D:\MySQLDumps\dbs\"
 set "mysqldump=C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump.exe"
 set "mysqlDataDir=C:\wamp\bin\mysql\mysql5.6.17\data"
 set "zip=C:\Program Files\7-Zip\7z.exe"

rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"

 set "dirname=%YY%-%MM%-%DD% %HH%-%Min%-%Sec%"

 echo "dirName"="%dirName%"
 pause

 :: switch to the "data" folder
 pushd "%mysqlDataDir%"

 :: create backup folder if it doesn't exist
 if not exist "%backupDir%\%dirName%\" mkdir "%backupDir%\%dirName%"

 :: iterate over the folder structure in the "data" folder to get the databases




 for /d %%f in (*) do (
 echo processing folder "%%f"

 "%mysqldump%" --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > "%backupDir%\%dirName%\%%~nxf.sql"

 "%zip%" a -tgzip "%backupDir%\%dirName%\%%~nxf.sql.gz" "%backupDir%\%dirName%\%%~nxf.sql"

  del "%backupDir%\%dirName%\%%~nxf.sql"

 )
 popd

 :: delete all but the latest 3 folders

 for /f "skip=3 delims=" %%A in ('dir /b /ad /o-n "%backupDir%\%dirName%\*"')  do @echo rd /s /q "%backupDir%\%dirName%\%%~A"

pause

阅读 403

收藏
2020-05-17

共1个答案

一尘不染

在上面的@foxidrive的帮助下,我设法获得了想要的文件夹日期,它们是YYYY-MM-DD HH-MIN-SEC。

由于adityasatrio的MySQL Backup Batch
Script,
在这些文件夹中存储了gzip压缩的.sql数据库。

借助此答案中的@
Magoo,https://stackoverflow.com/a/17521693/1010918设法删除了所有文件夹(nameDir),
同时保留 了最新的N个文件夹(nameDir),并且也 没有 触摸任何可能在目录(backupDir)中。

这是完整的工作脚本。

随意删除任何出现pause并和echo 上有什么命令提示符里面去。

此外,将其添加到Windows Task Scheduler中,您将获得一个针对使用MySQL数据库的本地开发环境的可靠备份解决方案。

请感谢帮助我完成此任务的人员。没有你们,我不得不只使用昂贵的Windows应用程序来本地保存MySQL数据库。

(..以及下一个技巧,如果备份.sql文件时出现错误,我们将通过电子邮件将错误日志发送给自己。.但这又是另一个问题和故事。.)

 @echo off

 set dbUser=root
 set dbPassword=root
 set "backupDir=D:\MySQLDumps"
 set "mysqldump=C:\wamp\bin\mysql\mysql5.6.17\bin\mysqldump.exe"
 set "mysqlDataDir=C:\wamp\bin\mysql\mysql5.6.17\data"
 set "zip=C:\Program Files\7-Zip\7z.exe"

 :: https://stackoverflow.com/a/31789045/1010918 foxidrive's answer helped me get the folder with the date and time I wanted

rem The four lines below will give you reliable YY DD MM YYYY HH Min Sec MS variables in XP Pro and higher.

for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%" & set "MS=%dt:~15,3%"

 set "dirname=%YYYY%-%MM%-%DD% %HH%-%Min%-%Sec%"

 :: remove echo here if you like
 echo "dirName"="%dirName%"

 :: switch to the "data" folder
 pushd "%mysqlDataDir%"

 :: create backup folder if it doesn't exist
 if not exist "%backupDir%\%dirName%\" mkdir "%backupDir%\%dirName%"

 :: iterate over the folder structure in the "data" folder to get the databases

 for /d %%f in (*) do (
 :: remove echo here if you like
 echo processing folder "%%f"

 "%mysqldump%" --host="localhost" --user=%dbUser% --password=%dbPassword% --single-transaction --add-drop-table --databases %%f > "%backupDir%\%dirName%\%%~nxf.sql"

 "%zip%" a -tgzip "%backupDir%\%dirName%\%%~nxf.sql.gz" "%backupDir%\%dirName%\%%~nxf.sql"

  del "%backupDir%\%dirName%\%%~nxf.sql"

 )
 popd

 :: delete all folders but the latest 2


 :: https://stackoverflow.com/a/17521693/1010918 Magoo's answer helped me get what I wanted to do with the folders
 :: for /f "skip=2 delims=" %G in ('dir /B /ad-h /o-d') DO echo going to delete %G

 :: below following my version with rd (remove dir) command and /s and /q
 :: remove echo before rd to really delete the folders in question!!
 :: attention they will be deleted with content in them!!

 :: change the value after skip= to what you like, this is the amount of latest folders to keep in your backup directory
    for /f "skip=2 delims=" %%a in (' dir "%backupDir%\" /b /ad-h /o-d') do echo rd /s /q "%backupDir%\%%a"

:: remove pause here if you like and add the file to Windows Task Manager
 pause
2020-05-17