数据库增量备份和全量备份
1.修改配置
首先打开配置文件my.ini
添加以下配置
#log-bin="JSSM-20230617FY-bin"
log-bin="mysql-bin"# Server Id.
server-id=1#指令指定写入二进制日志的事件格式
binlog_format=MIXED
添加完之后对MySQL服务进行重启
重启之后可以发现在MySQL文件的Data目录下多了以下二进制文件
2.撰写增量备份和全量备份的脚本
2.1 全量备份
创建fullBackup.bat@echo off
:: 设置备份相关的配置
::改为自己的账号
set MYSQL_USER=roXX
::改为自己的密码
set MYSQL_PASSWORD=123XX
set MYSQL_HOST=localhost
set MYSQL_PORT=3306
::改成自己的数据库名字
set DATABASE_NAME=trigger
:: ::改成自己要存储全量备份存储路径
set BACKUP_DIR=D:\zengliangandquanliang\full
:: 修改直接的MySQL bin 目录路径
set MYSQL_BIN=C:\ProgramData\MySQL\MySQL Server 8.0\Data:: 获取当前日期时间,格式为YYYY-MM-DD_HH-MM-SS
for /f "tokens=2 delims==" %%I in ('"wmic os get LocalDateTime /value | findstr ="') do set datetime=%%I
set date=%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2%
set time=%datetime:~8,2%-%datetime:~10,2%-%datetime:~12,2%
set timestamp=%date%_%time%:: 创建备份目录
mkdir "%BACKUP_DIR%" 2>nul:: 设置备份文件路径
set BACKUP_FILE=%BACKUP_DIR%\MySQL_FullBackup_%timestamp%.sql:: 执行全量备份
echo 开始全量备份...::跳转自己mysql安装位置
pushd C:\Program Files\MySQL\MySQL Server 8.0\bin
mysqldump -u %MYSQL_USER% -p%MYSQL_PASSWORD% -h%MYSQL_HOST% -P%MYSQL_PORT% %DATABASE_NAME% --routines --events --single-transaction > "%BACKUP_FILE%"REM 删除超过30天的备份文件(可选)
forfiles /p "%BACKUP_DIR%" /m *.sql /d -30 /c "cmd /c del @path"[mysqldump]user=user_name
password=password
按照以下要求进行修改
修改完之后双击运行,下图所示为全量备份的SQL文件
2.2 增量备份
创建incremental_backup.bat@echo off
:: 设置备份相关的配置
set MYSQL_USER=roXXt
set MYSQL_PASSWORD=123XXX
set MYSQL_HOST=localhost
set MYSQL_PORT=3306
set DATABASE_NAME=trigger
:: 增量备份存储路径
set BACKUP_DIR=D:\zengliangandquanliang\incremental
:: MySQL bin 目录路径
set MYSQL_BIN=C:\ProgramData\MySQL\MySQL Server 8.0\Data:: 获取当前日期时间,格式为YYYY-MM-DD HH:MM:SS
for /f "tokens=2 delims==" %%I in ('"wmic os get LocalDateTime /value | findstr ="') do set datetime=%%I
set current_date=%datetime:~0,4%-%datetime:~4,2%-%datetime:~6,2%
set current_time=%datetime:~8,2%-%datetime:~10,2%-%datetime:~12,2%
set timestamp=%current_date% %current_time%pushd C:\Program Files\MySQL\MySQL Server 8.0\bin
for /f "tokens=1,2" %%a in ('mysql -u%MYSQL_USER% -p%MYSQL_PASSWORD% -e "SHOW MASTER STATUS;" -N') do (set binlog_file=%%a
)for /f "delims=" %%A in ('powershell -NoProfile -Command "(Get-Date).AddMinutes(-10).ToString('yyyy-MM-dd HH:mm:ss')"') do set past_time=%%A
echo 当前时间减去10分钟:%past_time%:: 输出备份时间范围
:: echo 上次备份时间:%LAST_BACKUP_TIME%
echo 十分钟前时间:%past_time%
echo 当前时间:%timestamp%:: 设置备份文件路径
REM set BACKUP_FILE=%BACKUP_DIR%\MySQL_IncrementalBackup_%current_date%_%current_time%.binlog
set BACKUP_FILE=%BACKUP_DIR%\MySQL_IncrementalBackup_%current_date%_%current_time%.sqlecho %BACKUP_FILE%
:: 执行基于时间的增量备份echo 开始增量备份,从时间:%past_time% 到时间:%timestamp% ...pushd C:\Program Files\MySQL\MySQL Server 8.0\bin
mysqlbinlog -u%MYSQL_USER% -p%MYSQL_PASSWORD% "C:\ProgramData\MySQL\MySQL Server 8.0\Data\%binlog_file%" --start-datetime="%past_time%" --stop-datetime="%timestamp%" > %BACKUP_FILE%REM 删除超过30天的备份文件(可选)
forfiles /p "%BACKUP_DIR%" /m *.sql /d -30 /c "cmd /c del @path"[mysqldump]user=user_name
password=password
3. 增量备份恢复
!
举例:我们在trrgger库中的people表新增一条记录名叫:X123X
执行增量备份的bat文件