windowds下备份MySQL(mysqldump)
mytest.bat文件内容如下
@echo off set backup_date=%date:~0,4%%date:~5,2%%date:~8,2% set db_name=db_test01 db_test02 db_test03 for %%i in (%db_name%) do (mysqldump -h localhost -uroot -pmysql -P13306 %%i --default-character-set=utf8 --set-gtid-purged=OFF --skip-tz-utc --skip-disable-keys --skip-add-locks --single-transaction --master-data=2 >D:\mysqlbak\%%i_%backup_date%bak.sql) set dbbak_dir=D:\mysqlbak\ echo 删除7天前的备份文件…… echo. forfiles /P %dbbak_dir% /D -2 -M *bak.sql /C "cmd /c del @file" pause
中文显示乱码的解决办法:将该文件以ANSI格式另存。
查库获取数据库名(mybak.bat)
@echo off set backup_date=%date:~0,4%%date:~5,2%%date:~8,2% mysql -h localhost -uroot -p"mysql" -P13306 -N -e "select schema_name from information_schema.schemata where schema_name not in ('information_schema','mysql','sys','performance_schema') ">d:\dblist.txt for /f %%a in (D:\dblist.txt) do mysqldump -h localhost -uroot -pmysql -P13306 %%a --default-character-set=utf8 --set-gtid-purged=OFF --skip-tz-utc --skip-disable-keys --skip-add-locks --single-transaction --master-data=2 >D:\mysqlbak\%%a_%backup_date%bak.sql