git 拉取: https://gitee.com/kevin9401/BackUpDataBase.git
一、需求:
定时备份数据库数据
二、分析:
1. 定时任务
2. 备份数据库表结构和数据
三、实现:
1. pom 文件:
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.2.RELEASE
com.kevin
backupdatabase
1.0-SNAPSHOT
jar
1.8
mysql
mysql-connector-java
8.0.15
com.alibaba
druid
1.1.20
org.springframework.boot
spring-boot-starter
2.2.4.RELEASE
org.awaitility
awaitility
3.1.2
test
org.springframework.boot
spring-boot-maven-plugin
2. application.yml 文件
spring:
datasource:
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://192.168.100.120:3306/test01?useSSL=false&autoReconnect=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: kevin02
password: Kevin123!
sqlbackup:
path: /export/servers/db_backup/
3. BackUpDataBaseManager —— 具体操作数据库备份
package com.kevin.manager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.File;
import java.io.IOException;
/**
* @a