SpringBoot定时MySQL数据库备份

1、启动类中添加注解,开启定时功能

@EnableScheduling

2、编写定时备份功能代码

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


//数据库定时备份
@Component
public class Backups {
    public static void main(String[] args) throws IOException {
        exportDatabaseTool();
    }

    //   例:  0 0 11,18 1/1 * ?    每天11点和18点执行一次备份
//    cron表达式  每2小时执行一次, 具体可以自行设置
//    @Scheduled(cron = "0 0 0/2 * * ? *")
//    @Scheduled(cron = "0/30 * * * * ? ")
    //每天10点30分定时备份
//    @Scheduled(cron = "0 30 10 * * ?")
    //在14时,10,11,12,13,14,15,16时内,每隔10分钟备份一次
    @Scheduled(cron = "0 0/10 10,11,12,13,14,15,16 * * ?")
    public static void exportDatabaseTool() throws IOException {
        System.out.println("定时备份任务开始执行....>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        String ip = "localhost";  // 数据库的ip地址
        String host = "3306";   // 数据库的端口
        String userName = "root";   // 数据库的账户
        String password = "root";  // 数据库的密码
        String databaseName = "数据库名";  // 所连接的数据库
        String savePath = "C:\\数据库备份";    // 备份的sql文件存储地址(可自行修改)
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy年MM月dd日HH时mm分ss秒sss" );
        String fileName = "jeesite"+sdf.format(new Date())+".sql";
        File saveFile = new File(savePath);
        if (!saveFile.exists()) {// 如果目录不存在
            saveFile.mkdirs();// 创建文件夹
        }
//        拼接文件路径
        if(!savePath.endsWith(File.separator)){
            savePath = savePath + File.separator;
        }

        PrintWriter printWriter = null;
        BufferedReader bufferedReader = null;
        try {
            printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));
//            执行命令  需要写mysql的安装路径
            String sqlUrl = "C:\\Program Files\\MySQL\\MySQL Server 8.0\\bin\\mysqldump --column-statistics=0 -R -E -h" + ip + " -P" + host + " -u" + userName + " -p" + password + " " + databaseName;
            System.out.println(sqlUrl);
            Process process = Runtime.getRuntime().exec(sqlUrl);
            InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while((line = bufferedReader.readLine())!= null){
                printWriter.println(line);
            }
            printWriter.flush();
			/*if(process.waitFor() == 0){//0 表示线程正常终止。
				return true;
			}*/

            // 判断文件夹中备份数量,数量大于60个则清理掉第一个文件
            List fileList = Arrays.asList(saveFile.listFiles());
            if (fileList.size() >=61){
                fileList.get(0).delete();
            }

        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (printWriter != null) {
                    printWriter.close();
                }
            } catch (IOException e) {
                System.out.println(e);
                e.printStackTrace();
            }
        }
        //return false;
    }

}

你可能感兴趣的:(java,mysql,java)