Simple Mysql backup BAT script sharing under Windows

  • 2021-07-24 11:51:50
  • OfStack

Preface

This paper introduces a simple BAT script for backing up Mysql under Windows. The script uses mysqldump Command to back up a specified Mysql database to a file in the format of %dbname%-yyyyMMddHHmmss.sql Keep only the most recent 60-day backups. If you want to execute regularly, you can add a task plan in Windows. You can refer to this article for details.

The sample code is as follows


@echo off
set hour=%time:~0,2%
if "%time:~0,1%"==" " set hour=0%time:~1,1%
set now=%Date:~0,4%%Date:~5,2%%Date:~8,2%%hour%%Time:~3,2%%Time:~6,2%
echo %now%
set host=xxx.xxx.xxx.xxx
set port=3306
set user=root
set pass=root
set dbname=dataname
set backupfile=E:\backup\db\%dbname%-%now%.sql
E:\backup\mysql-5.7.13-winx64\bin\mysqldump -h%host% -P%port% -u%user% -p%pass% -c --add-drop-table %dbname% > %backupfile%
echo delete files before 60 days
forfiles /p "E:\backup\db" /m *.sql /d -60 /c "cmd /c del @file /f"

Summarize


Related articles: