问题描述
我有一个目录到我备份mysql DBS的文件夹中当前日期。
现在经过30天或60天,我喜欢只保留n个最新的文件夹备份目录,并删除这些文件夹的其余部分,每个丹麦人gzip压缩DBS。
有关备份我使用MySQL DBS 并创建一个与当前文件夹日期也进行排序thanks这个答案。
但是,只有保持,在这个例子中,最新的3个文件夹的缘故,并删除所有其他文件夹中的备份目录似乎是一个斗争。
这是我都试过了。
::删除的BACKUPDIR的所有文件和文件夹
:: FOR / F跳过= 3 delims =%%一个在('DIR%BACKUPDIR%\\/ B / O-D')做回声删除%BACKUPDIR%\\ %%一个
::只删除BACKUPDIR文件
:: FOR / F跳过= 3 delims =%%一个在('DIR%BACKUPDIR%\\/ B / A-D')做回声删除%BACKUPDIR%\\ %%一个
::删除所有文件和文件夹,但保持3最新的文件夹BACKUPDIR
:: FOR / F跳过= 3 delims =%%一个在('DIR%BACKUPDIR%\\/ B')做回声删除%BACKUPDIR%\\ %%一个
::删除所有文件和文件夹,但保持3最新的文件夹BACKUPDIR
:: FOR / F跳过= 3 delims =%%一个在('DIR%BACKUPDIR%\\/ O-D / B')做回声RD / S / Q%BACKUPDIR%\\ %%一个
请注意我不是在寻找删除,可能是在BACKUPDIR任何文件。我删除所有,但在BACKUPDIR最新的3个文件夹后,所以基本上删除与gzip压缩的.sql文件夹老在其中。
这些问题帮助,但仍然我不这样做是正确弄好了。
都将x最近的文件夹
How我从目录中删除旧文件,同时保持在Windows (文件夹不)是最近的
(良好的措施,又不文件的文件夹。)
本评论delete所有将x最近的文件夹中最接近涉及到什么,我试图做的,但也删除,可能是在BACKUPDIR的任何文件。
** EDIT1:**刚刚发现的 2介绍最新的文件夹和所有文件夹都看在任何方面有所帮助。
我在 POPD
绵薄之力完整的脚本是在这里。我希望能得到这个排序与您的帮助或输入进一步阅读。请注意我完全新批脚本。对任何帮助,这实在是太多AP preciated。帮我把所有未删除,请这N个最新的文件夹。 :)
关闭@echo 设置DBUSER =根
设置DBPASSWORD =根
设置BACKUPDIR = D:\\ MySQLDumps
设置的mysqldump = C:\\ WAMP \\ BIN \\ mysql的\\ mysql5.6.17 \\ BIN \\ mysqldump.exe
设置mysqlDataDir = C:\\ WAMP \\ BIN \\ mysql的\\ mysql5.6.17 \\ DATA
设置ZIP = C:\\ Program Files文件\\的7-Zip \\ 7z.exeREM下面的四行会给你可靠的YY DD在XP专业版及更高版本MM YYYY HH分秒MS变量。FOR / F令牌= 2 delims ==%%一个在('WMIC OS获取localdatetime /值)并设置DT = %%一个
设置YY =%DT:〜2,2%与&设置YYYY =%DT:〜0.4%,与&设置MM =%DT:〜4.2%与&设置DD =%DT:6,2〜%
设置HH =%DT:〜8.2%&放大器;设置最小=%DT:10,2〜%&放大器;设置SEC =%DT:〜12.2%&放大器;集MS =%DT:〜15.3%。 设置目录名=%YYYY% - %MM% - %DD%HH%% - %最小% - %二段% 回声目录名=%DIRNAME%
::暂停 ::切换到数据文件夹中
PUSHD%mysqlDataDir% ::创建备份文件夹,如果它不存在
如果不存在%BACKUPDIR%\\%DIRNAME%\\的mkdir%BACKUPDIR%\\%DIRNAME% ::迭代中的数据的文件夹结构文件夹来获取数据库 针对/ D %% f由于(*)做(
回波处理文件夹%% F %的mysqldump%主机=localhost的用户=%DBUSER%--password =%DBPASSWORD%--single事务--add-drop-table在--databases %% F> %BACKUPDIR%\\%DIRNAME%\\ %%〜nxf.sql 拉链%%一-tgzip%BACKUPDIR%\\%DIRNAME%\\ %%〜nxf.sql.gz%BACKUPDIR%\\%DIRNAME%\\ %%〜nxf.sql 德尔%BACKUPDIR%\\%DIRNAME%\\ %%〜nxf.sql )
POPD ::删除所有文件夹,但最近3
::删除的BACKUPDIR的所有文件和文件夹
:: FOR / F跳过= 3 delims =%%一个在('DIR%BACKUPDIR%\\/ B / O-D')做回声删除%BACKUPDIR%\\ %%一个
::只删除BACKUPDIR文件
:: FOR / F跳过= 3 delims =%%一个在('DIR%BACKUPDIR%\\/ B / A-D')做回声删除%BACKUPDIR%\\ %%一个
::删除所有文件和文件夹,但保持3最新的文件夹BACKUPDIR
:: FOR / F跳过= 3 delims =%%一个在('DIR%BACKUPDIR%\\/ B')做回声删除%BACKUPDIR%\\ %%一个
::删除所有文件和文件夹,但保持3最新的文件夹BACKUPDIR
:: FOR / F跳过= 3 delims =%%一个在('DIR%BACKUPDIR%\\/ O-D / B')做回声RD / S / Q%BACKUPDIR%\\ %%一个暂停
这两种不同的线工作。保持所有的文件有可能是在BACKUPDIR,而只删除所选择的N最新nameDir文件夹。
这是从@ JosefZ的评论Windows批处理脚本只能保持在一个目录N个最新FOLDERS
FOR / F跳过= 2 delims =%%一个在('DIR%BACKUPDIR%\\/ B / O:-N / A:D')不回应RD RD / S / q%BACKUPDIR%\\ %%一个
这也适用摘自@从这里http://stackoverflow.com/a/17521693/1010918
FOR / F跳过= 2 delims =%%一个在('DIR%BACKUPDIR%\\/ B / AD-H / O-D)做次/ S / q%BACKUPDIR%\\ %%一个
在这里Windows批处理脚本备份本地MySQL数据库和放大器;只保留最新ñ资料夹,备份文件如果你喜欢。
感谢大家的帮助!
I have a directory into which I backup MySQL dbs in a folder with the current date.
Now after 30 or 60 days I like to only keep the n latest folders in that backup directory and delete the rest of the folders, each having gzipped dbs in them.
For backing up the MySQL dbs I use adityasatrio's batch script and creating the folder with the current date is also sorted thanks to this answer.
However only keeping the, for sake of this example, latest 3 folders and deleting all other folders in the backup directory seems a struggle.
This is what I have tried.
:: deletes all files and folders in the backupDir
:: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b /o-d') do echo del "%backupDir%\%%a"
:: deletes only files in backupDir
:: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b /a-d') do echo del "%backupDir%\%%a"
:: deletes all files and folders BUT keeps 3 latest FOLDERS in backupDir
:: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b') do echo del "%backupDir%\%%a"
:: deletes all files and folders BUT keeps 3 latest FOLDERS in backupDir
:: for /f "skip=3 delims=" %%a in (' dir "%backupDir%\" /o-d /b') do echo rd /S /Q "%backupDir%\%%a"
Please note I am not looking to delete any files that might be in that backupDir. I am after deleting all but the latest 3 folders in the backupDir, so basically remove old folders with gzipped .sql files in them.
These questions helped but still I am not doing it right somehow.
delete all but X most recent folders
How do I delete old files from a directory while keeping the most recent ones on Windows (files not folder)
Batch file to delete files older than N days (again, files not folder)
https://serverfault.com/questions/49614/delete-files-older-than-x-days (for good measure, again files not folders..)
This comment delete all but X most recent folders comes closest to what I am trying to do, but that also deletes any files that might be in the backupDir.
**edit1:**Just found Delete all folders except 2 lastest folders in Windows and will see if that helps in any ways.
The complete script with my humble efforts below popd
is here. I hope to get this sorted with your help or input for further reading. Note I am totally new to Batch Scripting. Any help towards this is really much appreciated. Help me get all but those N latest folders deleted please. :)
@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"
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%"
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 folders but the latest 3
:: deletes all files and folders in the backupDir
:: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b /o-d') do echo del "%backupDir%\%%a"
:: deletes only files in backupDir
:: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b /a-d') do echo del "%backupDir%\%%a"
:: deletes all files and folders BUT keeps 3 latest FOLDERS in backupDir
:: for /f "skip=3 delims=" %%a in ('dir "%backupDir%\" /b') do echo del "%backupDir%\%%a"
:: deletes all files and folders BUT keeps 3 latest FOLDERS in backupDir
:: for /f "skip=3 delims=" %%a in (' dir "%backupDir%\" /o-d /b') do echo rd /S /Q "%backupDir%\%%a"
pause
These two different lines work. Keeping all files there might be in the backupDir while deleting only the selected N latest nameDir folders.
This is adjusted from @JosefZ's comment Windows Batch Script to only keep the N latest FOLDERS in a directory
for /f "skip=2 delims=" %%a in ('dir "%backupDir%\" /B /O:-N /A:D') do echo rd rd /s /q "%backupDir%\%%a"
This also works taken from @Magoo's answer from here http://stackoverflow.com/a/17521693/1010918
for /f "skip=2 delims=" %%a in (' dir "%backupDir%\" /b /ad-h /o-d') do rd /s /q "%backupDir%\%%a"
Find the complete working Batch Script here Windows Batch Script to backup local MySQL databases & only keep N latest FOLDERS with backup files if you like.
Thank you all for your help!
这篇关于Windows批处理脚本只保留N个最新的文件夹目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!