DbBackUpJob.java 1.44 KB
package com.order.erp.job;

import com.order.erp.common.utils.DateUtils;
import com.order.erp.common.utils.DbUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * @author: xms
 * @description: TODO
 * @date: 2023/6/25 10:35
 * @version: 1.0
 */
@Slf4j
@Component
public class DbBackUpJob {

    @Value("${db.mysql.ip}")
    private String ip;

    @Value("${db.mysql.port}")
    private String port;

    @Value("${db.mysql.user}")
    private String user;

    @Value("${db.mysql.password}")
    private String password;

    @Value("${db.mysql.savePath}")
    private String savePath;

    @Value("${db.mysql.databaseName}")
    private String databaseName;

    @Resource
    private DbUtil dbUtil;

    /**
     * 每隔30分执行一次
     */
//    @Scheduled(cron = "0 */30 * * * ?")
//    @Scheduled(cron = "0 */1 * * * ?")
    public void backup() {
        log.info("定时任务-开始备份db-时间:{}", DateUtils.format(LocalDateTime.now(), DateUtils.DATE_TIME));
        String fileName = "db-backup-file";
        dbUtil.exportDatabaseTool(ip, port, user, password, databaseName, savePath, fileName);
        log.info("定时任务-完成备份db-时间:{}", DateUtils.format(LocalDateTime.now(), DateUtils.DATE_TIME));
    }
}