当前位置:网站首页>Windows下bat脚本备份MySQL数据库
Windows下bat脚本备份MySQL数据库
2022-07-24 05:23:00 【踩坑之路】
MySQL数据库备份bat 脚本链接
https://download.csdn.net/download/qq_15735767/12558587
脚本描述:
1、多个数据库名定义在文件中,脚本通过读取文件,循环获得数据库名称,进行备份
2、备份sql存储在以时间命名的文件夹中,自动压缩并删除压缩前的文件
3、删除前7天的备份文件
具体脚本信息如下:
@echo off
:: 使用mysqldump逐个备份所有数据库到指定目录
:: 注意,实现把所有数据库逐行写入database_info.txt文件,并且该文件和数据库备份目录在同一级
setlocal ENABLEDELAYEDEXPANSION
:: 数据库备份目录
set "base_dir=E:\MySQL_Back"
set ip=xxxxx
set user=xxxxx
set pass=xxxxx
set port=xxxxx
set "Ymd=%date:~0,4%%date:~5,2%%date:~8,2%0%time:~1,1%%time:~3,2%%time:~6,2%"
:: 存储数据库名,循环使用
set "dbFiles=%base_dir%\database_info.txt"
:: 保存sql备份成功和失败的数据库
echo backup success info > %base_dir%\backup_success.txt
echo backup fail info > %base_dir%\backup_fail.txt
set "successFile=%base_dir%\backup_success.txt"
set "failFile=%base_dir%\backup_fail.txt"
:: 以当前时间命名创建目录
md %base_dir%\%Ymd%_sql
:: 删除7天以前以 _sql.rar 结尾的文件
forfiles /p %base_dir% /m "*_sql.rar" /d -7 /c "cmd /c if @isdir==TRUE (rmdir /q /s @path) else (del /f @path)" >nul 2>nul
:: 备份,Utils下有mysqldump工具
cd /d "%base_dir%\Utils"
echo MySQL Start Backup...
for /f %%i in (%dbFiles%) do (
set db=%%i
echo !db!
mysqldump --no-defaults --force -h%ip% -u%user% -p%pass% -P%port% --databases !db! > %base_dir%\%Ymd%_sql\!db!.sql 2>nul
:: 判断 备份成功或失败记录到相关文件
if errorlevel 0 (
echo !db! Backup finished. >> %successFile%
) else (
echo !db! Backup fail. >> %failFile%
)
)
echo MySQL Backup complete!
:: 使用Rar工具压缩,取决于服务器安装了什么压缩软件
echo Compressing file, please wait...
"%base_dir%\WinRAR\Rar.exe" a -ep1 "%base_dir%\%Ymd%_sql.rar" "%base_dir%\%Ymd%_sql" 2>nul
echo Compressed file complete!
rmdir /q /s %base_dir%\%Ymd%_sql
pause
边栏推荐
- ip作业(1)
- Unity shader migrated from built-in rendering pipeline to URP
- 手动安装Apache
- 【218】CS架构和BS架构以及数据放在服务端和客户端的利与弊?
- 【217】#!/ The meaning of usr/bin/env
- [301] grotesque behavior - predictable irrationality
- RESTful API介绍
- IP lesson summary (3)
- Leetcode refers to offer jz5 to replace the space string
- Server hardware and RAID configuration practice
猜你喜欢
随机推荐
leetcode剑指offer JZ25 合并两个排序的链表
Ia class summary (2)
Sword finger offer jz10 Fibonacci sequence
【251】常见的测试工具
mysql 忘记退出直接关闭窗口现在要删除整个文件夹如何删除
本地搭建WordPress个人博客,并内网穿透发布上线 (22)
Simple three-step fast intranet penetration
Unity shader migrated from built-in rendering pipeline to URP
IP笔记(10)
IP作业(6)
[214] what is an automation framework
General paging 01
RAID5和LVM组合使用
IP job (6)
Install Apache manually
Tensorflow GPU installation -- 056
Homework in the second week
RESTful API介绍
leetcode剑指offer JZ3 数组中重复的数字
IP课笔记(5)
![[214] what is an automation framework](/img/bb/24c35613c49357258876ce3f1611ee.jpg)








