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

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

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

在上面的@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:wampbinmysqlmysql5.6.17binmysqldump.exe" set "mysqlDataDir=C:wampbinmysqlmysql5.6.17data" set "zip=C:Program Files7-Zip7z.exe" :: https://stackoverflow.com/a/31789045/1010918 foxidrive's answer helped me get the folder with the date and time I wantedrem 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


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4946466.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-13
下一篇2022-11-13

发表评论

登录后才能评论

评论列表(0条)

    保存