Benutzer-Werkzeuge

Webseiten-Werkzeuge


Seitenleiste

3D

INTERN

ADOBE

ARDUINO

BITBUCKET

DOCKER

C4D

GIT

HTML

IPHONE

JAVASCRIPT

JTL-SHOP

LARAVEL

MAGENTO2

MYSQL

PHP

PLESK

PROCESSING

PYTHON

SUITECRM

SEO

TRYTON

THUNDERBIRD / ICEDOVE / FossaMail

TYPO3

TYPO3: BACKEND

TYPO3: TYPOSCRIPT

UNIX

VLC

VVVV

WINDOWS

WINDOWS 10

WORDPRESS

XAMPP

XT:C 4 (Veyton)

batch-script-to-backup-mysql-on-windows

Batch Script for mysqlbackup and restore

Assumptions / Prerequisits

  • have mysqldump.exe in your PATH USER or SYSTEM variable
  • using xampp installed to C:\xampp
  • directory structure as in 'backupDir'

The Script 'mysql-backup.bat'

@echo off
 
tasklist /FI "IMAGENAME eq mysqld.exe" 2>NUL | find /I /N "mysqld.exe">NUL
if "%ERRORLEVEL%"=="0" (
    goto continue
) else (
    goto sql_not_running
)
 
:continue
set dbUser=root
set /p dbPassword= Please give mysql password:
set dbName=YOURDB
set projectDir=PROJECTNAME
set backupDir=C:\xampp\htdocs\%projectDir%\backups\sql\

:: 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 filename=%dbName%_%logtimestamp%.sql
 
mysqldump -u%dbuser% -p%dbPassword% %dbName% > %backupDir%%filename%
 
exit
 
:sql_not_running
echo -----------------------------
echo !!WARNING COULDN'T CONTINUE!!
echo -----------------------------
echo Message: mysqld.exe is not running, please start it (e.g. via xampp-control)

The Script 'mysql-restore.bat'

  • reads all file in the backup directory.
  • drops database
  • creates database and uses newest file to restore data
@echo off
 
set dbUser=root
set /p dbPassword= Please type sql password for root user:
cls
set dbName=YOURDB
set projectDir=PROJECTNAME
set backupDir=C:\xampp\htdocs\%projectDir%\backups\sql\
 
echo.
echo Restoring latest file:
FOR /F "delims=|" %%I IN ('DIR "%backupDir%*.sql" /B /O:D') DO SET NewestFile=%%I
REM copy "%DatabaseBackupPath%\WebServer\%NewestFile%" "D:\"
echo.
echo File is: %NewestFile%
 
set /p answer=WARNING! deletion of %dbName% !! Continue ? (y/n)
 
if /i {%answer%}=={y} (goto :yes)
if /i {%answer%}=={yes} (goto :yes)
goto :no
:yes
 
mysqladmin -u%dbUser% -p%dbPassword% drop %dbName%
mysqladmin -u%dbUser% -p%dbPassword% create %dbName%
 
mysql -u%dbUser% -p%dbPassword% %dbName% < %backupDir%%NewestFile%
 
echo Congrats. the newest backup file has been imported
exit /b 0
 
:no
echo Okay, maybe next time.
exit /b 1
batch-script-to-backup-mysql-on-windows.txt · Zuletzt geändert: 2015/01/06 09:13 von admin