I am using adityasatrio's batch file to backup local MySQL dbs and like to be able to only keep the 30 latest backup files. Using root:root for this example.
@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
Now I have had a good look at the following questions:
Batch file to delete files older than N days
https://serverfault.com/questions/49614/delete-files-older-than-x-days
Batch file that keeps the 7 latest files in a folder
Windows batch file only keeping the last 30 files
and am now wondering if I can simply add (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"
(yes I will later change echo to del but first I like to see what will happen)
or (https://stackoverflow.com/a/13368077)
for /f "skip=30 eol=: delims=" %%F in ('dir /b /o-d *.sql') do @del "%%F"
at the end of the batch file, right under
del %backupDir%\%dirName%\%%f.sql
to make this happen?
I have never done this before but have searched for automated local backup apps/php scripts/mysqldump commands/etc for MySQL dbs, even had a go with Workbench only to discover that no scheduling can be set in the community edition (thank you Oracle).
All the other apps either need to have someone open the app and hit "run now" or want you to pay for setting up a schedule (no thanks).
I think this can be done with the tools at hand on a Windows 7 and later versions machine. Please help me add this functionality to the script, that would be great, thank you.
edit1:
When adding the quote commands nothing happens. Also the created backup directory only displays the time but not the year, month and day. Doing further research to find out why. Any ideas?
This comment delete all but X most recent folders talks about deleting the 5 latest folders, though when I use it like this
for /f "skip=2 delims=" %%a in ('dir %backupDir%\%dirName% /o-d /b') do rd /S /Q "%backupDir%\%dirName%\%%a"
the error is the following.
The system cannot find the file specified.
The system cannot find the path specified.
edit2: Below is the code that with @foxidrive help sets the folder name as I like to have it, but the last bit, trying to only keep the 3 latest folders (for testing purposes only 3) and delete the rest of the folders in the backupDir does not seem to work out.
Thank you for any help.
@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