# databases-backup **Repository Path**: Beansp/databases-backup ## Basic Information - **Project Name**: databases-backup - **Description**: 提供Mysql数据库表部分数据备份/删除能力,避免数据库表随数据熵增后过于庞大的解决方案 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-05-16 - **Last Updated**: 2022-10-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # databases-backup ### 介绍 提供Mysql数据库表部分数据备份/删除能力,避免数据库表随数据熵增后过于庞大的解决方案 ### 使用步骤 ####1. 拉取项目,编译本项目到本地maven仓库 ####2. 在需要使用该项目的其他项目中依赖该maven坐标 ``` com.white databases-backup 0.0.1-SNAPSHOT ``` ####3. 在你的项目中进行配置包扫描,扫描databases-backup项目 ```java //以SpringBoot项目为例 @ComponentScan(basePackages = "com.white.databasesbackup") public class MainApplication { public static void main(String[] args) { SpringApplication.run(MainApplication.class, args); } } ``` ####4. 在你项目的application配置文件中增加一条配置 ```yaml #用作为指定数据库备份的位置 databasesbackup: path: D:\XXXX\XXXX\数据库备份位置 ``` ####4. 在你依赖该项目的项目中的数据库中新增如下表 ```sql drop table if exists databases_backup_configure; /*==============================================================*/ /* Table: databases_backup_configure */ /*==============================================================*/ create table databases_backup_configure ( id int(11) not null auto_increment comment 'ID', databases_name varchar(64) not null comment '数据库名称', table_name varchar(64) not null comment '表名称', date_field_name varchar(64) not null comment '日期字段名称', retention_time int(11) not null default 60 comment '保留时间(天)', data_threshold bigint(20) not null default 0 comment '数据阈值', enable tinyint(1) not null default 0 comment '是否启用', backup tinyint(1) not null default 1 comment '是否备份(1是,0否,如果为否则会删除数据不会备份数据)', timestamp tinyint(1) not null default 0 comment '日期字段是否是时间戳', timestamp_unit tinyint(4) not null default 1 comment 'timestamp字段为1时,该字段才生效,时间戳单位(1:毫秒,2:秒)', remark varchar(500) comment '备注', primary key (id) ); alter table databases_backup_configure comment 'databases_backup_configure#数据库备份配置'; drop table if exists databases_backup_log; /*==============================================================*/ /* Table: databases_backup_log */ /*==============================================================*/ create table databases_backup_log ( id int(11) not null auto_increment comment 'ID', databases_name varchar(64) not null comment '数据库名称', table_name varchar(64) not null comment '表名称', delete_count bigint(20) not null default 0 comment '数据删除条数', count bigint(20) not null default 0 comment '备份数据条数', create_time datetime not null comment '备份日期', exec_status tinyint(1) not null comment '执行状态(1成功,0失败)', fail_msg text comment '失败原因', primary key (id) ); alter table databases_backup_log comment 'databases_backup_log#数据库备份记录'; ``` ####5. 编写表 databases_backup_configure ```text retention_time: 指表数据保留的最大天数 data_threshold: 当表数据总量 大于 data_threshold 值时,才进行数据备份与删除 ``` ####6. 注入databases-backup提供的Bean进行执行备份 ```java import org.springframework.stereotype.Service; @Service public class MyService { @Resource private Backup backup; public void backup(){ //对表databases_backup_configure中配置的所有数据库表进行执行相关逻辑备份 backup.backup(); } } ```