欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot項目實現(xiàn)定時備份數(shù)據(jù)庫導(dǎo)出sql文件方式

 更新時間:2025年03月28日 09:16:05   作者:想太多會累i  
這篇文章主要介紹了springboot項目實現(xiàn)定時備份數(shù)據(jù)庫導(dǎo)出sql文件方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教

之所以做這個功能的原因是我的服務(wù)器上的數(shù)據(jù)庫被攻擊了,還好服務(wù)器上沒有什么重要的數(shù)據(jù),但是數(shù)據(jù)沒了就很肉疼,因此做了這個功能,用來定時備份數(shù)據(jù)庫數(shù)據(jù)

添加依賴

這里用到了 hutool 工具包 這個包挺好用的,推薦大家可以多看看他的官方文檔

官方文檔:https://www.hutool.cn/docs/#/

<!-- hutool工具類-->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.3.3</version>
</dependency>

添加配置文件和配置

因為這里只是需要導(dǎo)出sql 所以就使用了hutool工具中的DB

resources目錄下創(chuàng)建db.setting數(shù)據(jù)庫配置文件

文件中內(nèi)容:

## url 數(shù)據(jù)庫連接地址
## user 連接數(shù)據(jù)庫賬號
## pass 連接數(shù)據(jù)庫密碼
url = jdbc:mysql://localhost:3306/test
user = root
pass = 123456

緊接著在application.yml文件中添加sql文件的存放的路徑

# 導(dǎo)出sql文件的位置
sql:
  dbname: notepad # 數(shù)據(jù)庫名
  filePath: /data/sql/ # 導(dǎo)出sql文件的位置 win下會直接在項目所在磁盤下建立 data/sql文件

編寫導(dǎo)出SQL的方法

方法直接貼在下面:

package com.an.notepad.task;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.io.file.FileWriter;
import cn.hutool.core.text.StrBuilder;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import cn.hutool.db.ds.DSFactory;
import lombok.Data;
import lombok.SneakyThrows;
import lombok.experimental.Accessors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.io.File;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.util.List;

@Configuration
public class ExportSQL {
    // 用于打印執(zhí)行日志
    private static final Logger logger = LoggerFactory.getLogger(ExportSQL.class);

    // 存儲的路徑
    @Value("${sql.filePath}")
    private String filePath;

    // 數(shù)據(jù)庫名
    @Value("${sql.dbname}")
    private String database_name;

    // 需要導(dǎo)出的表名
    private final List<TempBean> tempBeanList = CollectionUtil.newArrayList(
            // 這里寫需要存儲的表名 有幾張表就new幾個
            new TempBean().setTable("n_note"),
            new TempBean().setTable("n_user")
    );

    @SneakyThrows
    public void export() {
        //獲取默認(rèn)數(shù)據(jù)源
        DataSource ds = DSFactory.get();
        // 獲取數(shù)據(jù)庫連接對象
        Connection connection = ds.getConnection();
        // 獲取連接信息
        DatabaseMetaData metaData = connection.getMetaData();
        // 創(chuàng)建sql文件對象
        FileWriter sqlFileWriter = FileWriter.create(new File(filePath + database_name + ".sql"));
        sqlFileWriter.write("");
        sqlFileWriter.append("USE " + database_name + ";\n");
        sqlFileWriter.append("/*\n");
        sqlFileWriter.append(" --------------------------------------------------\n");
        sqlFileWriter.append(" Target Server Type    : " + metaData.getDatabaseProductName() + ";\n");
        sqlFileWriter.append(" Target Server Version : " + metaData.getDatabaseProductVersion() + ";\n");
        sqlFileWriter.append(" \n");
        sqlFileWriter.append(" Target Server Date : " + DateTime.now() + ";\n");
        sqlFileWriter.append(" \n");
        sqlFileWriter.append(" --------------------------------------------------\n");
        sqlFileWriter.append("*/\n");
        sqlFileWriter.append("SET NAMES utf8mb4;\n");
        sqlFileWriter.append("SET FOREIGN_KEY_CHECKS = 0;\n");
        for (TempBean tempBean : tempBeanList) {
            String table = tempBean.table;
            sqlFileWriter.append("\n\n\n");
            // DROP TABLE
            sqlFileWriter.append("DROP TABLE IF EXISTS `" + table + "`;\n");
            // CREATE TABLE
            Entity createTableEntity = Db.use().queryOne("SHOW CREATE TABLE " + table);
            sqlFileWriter.append((String) createTableEntity.get("Create Table"));
            sqlFileWriter.append(";\n");
            // 看配置,是否需要insert語句
            if (!tempBean.insert) {
                continue;
            }
            // INSERT INTO
            List<Entity> dataEntityList = Db.use().query("SELECT * FROM " + table);
            for (Entity dataEntity : dataEntityList) {
                StrBuilder field = StrBuilder.create();
                StrBuilder data = StrBuilder.create();

                dataEntity.forEach((key, value) -> {
                    field.append(key).append(", ");
                    if (ObjectUtil.isNotNull(value)) {
                        if (StrUtil.equals("true", String.valueOf(value))) {
                            data.append("b'1'");
                        } else if (StrUtil.equals("false", String.valueOf(value))) {
                            data.append("b'0'");
                        } else {
                            data.append("'").append(value).append("'");
                        }
                    } else {
                        data.append("NULL");
                    }
                    data.append(", ");
                });

                sqlFileWriter.append("INSERT INTO `" + table + "`(");
                String fieldStr = field.subString(0, field.length() - 2);
                sqlFileWriter.append(fieldStr);
                sqlFileWriter.append(") VALUES (");
                String dataStr = data.subString(0, data.length() - 2);
                sqlFileWriter.append(dataStr);
                sqlFileWriter.append(");\n");
            }
        }
        sqlFileWriter.append("\n\n\n");
        sqlFileWriter.append("SET FOREIGN_KEY_CHECKS = 1;\n");
        logger.info(">>>>>>>>>> 存儲sql成功" + DateTime.now());
    }

    @Data
    @Accessors(chain = true)
    static class TempBean {
        // 表名
        public String table;
        // 是否需要insert語句,默認(rèn)需要 (表中數(shù)據(jù))
        public Boolean insert = true;
    }
}

創(chuàng)建定時任務(wù)

package com.an.notepad.task;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;

/**
 * 定時導(dǎo)出數(shù)據(jù)庫sql功能
 */
@Configuration
@EnableScheduling // 開啟定時任務(wù)
public class ExportTask {
    @Autowired
    private ExportSQL exportSQL;

    //@Scheduled(cron = "0 0 1 * * ?") // 每天凌晨1點觸發(fā)一次
    @Scheduled(cron = "*/5 * * * * ?") // 5秒觸發(fā)一次
    public void task() {
        exportSQL.export();
    }
}

編寫完成 運(yùn)行測試一下

我這里項目在D盤中,因此D盤D:\data\sql 默認(rèn)會有生成的文件

成功,我這邊測試了一下,數(shù)據(jù)庫是可以導(dǎo)入的

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java數(shù)據(jù)庫連接池連接Oracle過程詳解

    Java數(shù)據(jù)庫連接池連接Oracle過程詳解

    這篇文章主要介紹了Java數(shù)據(jù)庫連接池連接Oracle過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下
    2020-09-09
  • Java設(shè)計模式:組合模式

    Java設(shè)計模式:組合模式

    這篇文章主要介紹了快速理解Java設(shè)計模式中的組合模式,具有一定參考價值,需要的朋友可以了解下,希望能夠給你帶來幫助
    2021-09-09
  • jpa?onetomany?使用級連表刪除被維護(hù)表數(shù)據(jù)時的坑

    jpa?onetomany?使用級連表刪除被維護(hù)表數(shù)據(jù)時的坑

    這篇文章主要介紹了jpa?onetomany?使用級連表刪除被維護(hù)表數(shù)據(jù)時的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 教你在一分鐘之內(nèi)理解Java Lambda表達(dá)式并學(xué)會使用

    教你在一分鐘之內(nèi)理解Java Lambda表達(dá)式并學(xué)會使用

    今天給大家?guī)У奈恼率荍ava8新特性的相關(guān)知識,文章圍繞著如何在一分鐘之內(nèi)理解Java Lambda表達(dá)式并學(xué)會使用展開,文中有非常詳細(xì)的介紹,需要的朋友可以參考下
    2021-06-06
  • Java匿名類,匿名內(nèi)部類實例分析

    Java匿名類,匿名內(nèi)部類實例分析

    這篇文章主要介紹了Java匿名類,匿名內(nèi)部類,結(jié)合實例形式分析了Java匿名類,匿名內(nèi)部類相關(guān)原理、用法及操作注意事項,需要的朋友可以參考下
    2020-04-04
  • SpringBoot項目嵌入RocketMQ的實現(xiàn)示例

    SpringBoot項目嵌入RocketMQ的實現(xiàn)示例

    本文主要介紹了SpringBoot項目嵌入RocketMQ的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-05-05
  • @Cacheable 拼接key的操作

    @Cacheable 拼接key的操作

    這篇文章主要介紹了@Cacheable 拼接key的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-02-02
  • springMVC在restful風(fēng)格的性能優(yōu)化方案

    springMVC在restful風(fēng)格的性能優(yōu)化方案

    這篇文章主要介紹了springMVC在restful風(fēng)格的性能優(yōu)化方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • @Valid 校驗無效,BindingResult未獲得錯誤的解決

    @Valid 校驗無效,BindingResult未獲得錯誤的解決

    這篇文章主要介紹了@Valid 校驗無效,BindingResult未獲得錯誤的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • SpringBoot統(tǒng)一功能處理實現(xiàn)的全過程

    SpringBoot統(tǒng)一功能處理實現(xiàn)的全過程

    最近在做項目時需要對異常進(jìn)行全局統(tǒng)一處理,主要是一些分類入庫以及記錄日志等,下面這篇文章主要給大家介紹了關(guān)于SpringBoot統(tǒng)一功能處理實現(xiàn)的相關(guān)資料,文中通過圖文以及實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-01-01

最新評論