java SpringBoot 设置定时备份Mysql数据库

  1. 在项目启动类上添加注解 开启定时任务
@EnableScheduling
  1. 编写定时任务类
    sqlUrl 这个的值如果没有配置环境mysql环境变量 需要指定到mysql的安装目录上去执行
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 * * * * ? ")
    public static void exportDatabaseTool() throws IOException {
        System.out.println("定时备份任务开始执行....>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
        String ip = "localhost";  // 数据库的ip地址
        String host = "3306";   // 数据库的端口
        String userName = "root";   // 数据库的账户
        String password = "root";  // 数据库的密码
        String databaseName = "project";  // 所连接的数据库
        String savePath = "D:\\备份";    // 备份的sql文件存储地址(可自行修改)
        SimpleDateFormat sdf = new SimpleDateFormat( "yyyy年MM月dd日HH时mm分ss秒sss" );
        String fileName = "project"+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 -h" + ip + " -P" + host + " -u" + userName + " -p" + password + " --default-character-set=utf8 " + 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<File> 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) {
                e.printStackTrace();
            }
        }
        //return false;
    }

}
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿彤木11

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值