ronisaha
4/5/2017 - 6:33 PM

Backup restore MySQL(Windows)

Backup restore MySQL(Windows)

@ECHO OFF
TITLE MYSql dump file importer

:: Change the values that match your environment
SET mysqlpath=mysql
:: You will be asked for password if not set here
SET mysqlpassword=
SET mysqluser=root

:: Nothing to change bellow this
::_____________________________________

COLOR 2f
ECHO.
ECHO   ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
ECHO   º                                                                          º
ECHO   º                           IMPORT SQL DUMP SO EASY                        º 
ECHO   º                                      BY                                  º 
ECHO   º                               RONI KUMAR SAHA                            º 
ECHO   º                                                                          º
ECHO   ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ 
ECHO.
pause

where /q %mysqlpath%
IF ERRORLEVEL 1 (
	SET MSG=Enter Full Path Of your mysql.exe
    GOTO GETMYSQLPATH
) ELSE (
    GOTO EVERYTHINGFNE
)

IF EXIST %mysqlpath% GOTO EVERYTHINGFNE
SET MSG=Enter Full Path Of your mysql.exe
:GETMYSQLPATH
SET /P mysqlpath=%MSG% : 
SET MSG=Path incorrect!. Enter valid file Path 
IF NOT EXIST %mysqlpath% GOTO GETMYSQLPATH

:GETPASSWORD
IF NOT [%mysqlpassword%] == [] GOTO EVERYTHINGFNE
SET /P mysqlpassword=Enter Password : 
GOTO GETPASSWORD


:EVERYTHINGFNE

SET mysqldump=%1
IF EXIST %mysqldump% GOTO GETDBNAME
SET MSG=Enter Full Path Of your mysql dump file
:GETMYSQLDUMPPATH
SET /P mysqldump=%MSG% : 
SET MSG=Path incorrect!. Enter valid file Path of your mysql dump file 
IF NOT EXIST %mysqldump% GOTO GETMYSQLDUMPPATH


:GETDBNAME

SET mysqldb=zxy_mis
ECHO.
ECHO.
ECHO Importing "%mysqldump%" into %mysqldb%
ECHO.
ECHO Please wait for a while............
%mysqlpath% -u %mysqluser% -p%mysqlpassword% %mysqldb%<"%mysqldump%"
ECHO.
IF ERRORLEVEL 1 (
	ECHO Operation failed..............
) ELSE (
  ECHO Operation completed..............
)
ECHO.
pause
@echo off

TITLE MySQL Backup Executor

:: Change the values that match your environment
SET backup_dir=D:\mysql_daily_backups\
SET mysqldb=zxy_mis
SET mysqlpassword=123456
SET mysqluser=root
SET mysqlpath=mysqldump


:: Nothing to change bellow this
::_____________________________________

COLOR 2f
ECHO.
ECHO   ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»
ECHO   º                                                                          º
ECHO   º                           BACKUP SQL DUMP SO EASY                        º 
ECHO   º                                      BY                                  º 
ECHO   º                               RONI KUMAR SAHA                            º 
ECHO   º                                                                          º
ECHO   ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ 
ECHO.

where /q %mysqlpath%
IF ERRORLEVEL 1 (
	SET MSG=Enter Full Path Of your mysqldump.exe
    GOTO GETMYSQLPATH
) ELSE (
    GOTO EVERYTHINGFNE
)

IF EXIST %mysqlpath% GOTO EVERYTHINGFNE

:GETMYSQLPATH
SET /P mysqlpath=%MSG% : 
SET MSG=Path incorrect!. Enter valid file Path 
IF NOT EXIST %mysqlpath% GOTO GETMYSQLPATH


:EVERYTHINGFNE
@ECHO OFF
:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error

:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
   IF "%%~L"=="" goto s_done
      Set _yyyy=%%L
      Set _mm=00%%J
      Set _dd=00%%G
      Set _hour=00%%H
      SET _minute=00%%I
      SET _second=00%%K
)
:s_done

:: Pad digits with leading zeros
      Set _mm=%_mm:~-2%
      Set _dd=%_dd:~-2%
      Set _hour=%_hour:~-2%
      Set _minute=%_minute:~-2%
      Set _second=%_second:~-2%

Set logtimestamp=%_yyyy%-%_mm%-%_dd%_%_hour%_%_minute%_%_second%
goto make_dump

:s_error
echo WMIC is not available, using default log filename
Set logtimestamp=_

:make_dump
set bkupfilename=%mysqldb%_%logtimestamp%.sql

echo Backing up DB %mysqldb% to file: %backup_dir%%bkupfilename%
echo .
echo Starting Backup of Mysql Database on server 

%mysqlpath% -u %mysqluser% -p%mysqlpassword% %mysqldb% > "%backup_dir%%bkupfilename%"

echo on


echo delete old backup

forfiles /p "%backup_dir%" /s /m *.* /d -3 /c "cmd /c del @file : date >= 30days"

echo Backup Complete! Have A Nice Day