当前位置:网站首页>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
边栏推荐
- Leetcode剑指offer JZ9 双栈实现队列
- Leetcode refers to offer jz5 to replace the space string
- leetcode剑指offer JZ23:链表中环的入口节点
- Understanding of Flink parallelism
- Install Apache manually
- Summary of ten common vulnerabilities (principle, harm, defense)
- IP lesson summary (3)
- IP作业(2)RIP
- Leetcode sword finger offer jz25 merges two sorted linked lists
- 测试经理/测试组长/测试主管面试题
猜你喜欢

How to build a website full of ritual sense and publish it on the public website 1-2

Install Apache manually

leetcode剑指offer JZ25 合并两个排序的链表
![Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]](/img/2a/43ba2839b842e0901a550d2883b883.png)
Quickly and simply set up FTP server, and achieve public network access through intranet [no need for public IP]

Homework in the second week

Playing RTSP video stream on webpage

Ia class summary (2)

服务器硬件及RAID配置实战

RESTful API介绍

Use intranet penetration to realize public network access to the Intranet
随机推荐
IP job (2) rip
[no need for public IP] configure a fixed public TCP port address for remote desktop raspberry pie
sed命令
go的环境搭建和起步
How to build a website full of ritual sense and publish it on the public website 1-2
Configure a fixed remote desktop address [intranet penetration, no need for public IP]
Batch operation of generating MySQL statements from Excel
Unity 3D frame rate statistics script
IA课总结(1)
Dameng database_ Dmfldr tool instructions
进行挂载永久挂载后无法开机
RESTful API介绍
mysql 忘记退出直接关闭窗口现在要删除整个文件夹如何删除
数据集和预训练模型
IP作业(6)
Flink checkpoint configuration details
leetcode剑指offer JZ73 翻转单词序列
Leetcode sword finger offer jz23: the entry node of the link in the linked list
Set up a WordPress personal blog locally and launch it through the intranet (22)
Simple three-step fast intranet penetration