栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/383731.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号