Created
January 29, 2018 12:00
-
-
Save lord-alfred/49f53c06eb94d9cc41a29f52fd18197d to your computer and use it in GitHub Desktop.
Windows batch script for backup mysql databases in cloud storage (dropbox / yandex.disk / google drive)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
echo [-] Task 'mysql backup' start | |
rem backups directory (with slash at end!) | |
set backup_dir=C:\YandexDisk\mysql\daily\ | |
rem mysql & mysqldump path | |
set mysql_path="C:\Program Files\MySQL\MySQL Server 5.7\bin\mysql.exe" | |
set mysqldump_path="C:\Program Files\MySQL\MySQL Server 5.7\bin\mysqldump.exe" | |
rem archive name | |
set backup_filename=database | |
rem tmp backup directory (without slash at end!) | |
set tmp_backup_dirname=C:\backup\mysql | |
rem argumets for rar.exe | |
set rar_argum=-m3 -dh -r -y -rr -t -mt1 -ag_yyyy_mm_dd_hh_mm -inul | |
rem path to rar.exe | |
set rar_path= | |
rem check rar.exe availability | |
if "%rar_path%" == "" ( | |
if exist "C:\Program Files\WinRAR\rar.exe" ( | |
set rar_path="C:\Program Files\WinRAR\rar.exe" | |
) else ( | |
if exist "C:\Program Files (x86)\WinRAR\rar.exe" ( | |
set rar_path="C:\Program Files (x86)\WinRAR\rar.exe" | |
) else ( | |
goto :rar_not_found | |
) | |
) | |
) | |
rem check forfiles.exe availability | |
if not exist "C:\Windows\System32\forfiles.exe" ( | |
goto :forfiles_not_found | |
) | |
rem remove backups older 14 days | |
echo [-] Start removing old backups | |
"C:\Windows\System32\forfiles.exe" /p %backup_dir% /s /m *.rar /d -14 /c "cmd /c del /q /f @file" | |
echo [+] Finish removing old backups | |
rem create dump & archive | |
echo [-] Start backup | |
rem delete old tmp sql files | |
del /s /f /q %tmp_backup_dirname%\*.* 1>nul | |
for /f %%f in ('dir /ad /b %tmp_backup_dirname%') do rd /s /q %tmp_backup_dirname%\%%f 1>nul | |
rem create tmp dir | |
if not exist %tmp_backup_dirname% ( | |
mkdir %tmp_backup_dirname% | |
) | |
rem dump databases into separate sql files | |
%mysql_path% --defaults-extra-file=%cd%mysql_config.cnf -s -N -e "SHOW DATABASES" | for /F "usebackq" %%D in (`findstr /V "information_schema performance_schema"`) do %mysqldump_path% --defaults-extra-file=%cd%mysql_config.cnf %%D 1> %tmp_backup_dirname%\%backup_filename%_%%D.sql | |
rem make archive with sql files | |
%rar_path% a %rar_argum% %backup_dir%%backup_filename% %tmp_backup_dirname% | |
rem delete old tmp sql files | |
del /s /f /q %tmp_backup_dirname%\*.* 1>nul | |
for /f %%f in ('dir /ad /b %tmp_backup_dirname%') do rd /s /q %tmp_backup_dirname%\%%f 1>nul | |
echo [+] Finish backup | |
goto :done | |
:rar_not_found | |
echo [!] C:\Program Files\WinRAR\rar.exe - not found! WinRAR installed? | |
echo [!] Set path to rar.exe in rar_path variable | |
pause | |
exit /b 1 | |
:forfiles_not_found | |
echo [!] C:\Windows\System32\forfiles.exe - not found! | |
echo [!] Download this from: ( https://www.microsoft.com/en-us/download/details.aspx?id=17657 ??? ) | |
exit /b 1 | |
:done | |
echo [+] Task 'mysql backup' end | |
exit /b 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Config for mysql:
C:\mysql_config.cnf