数据库的还原与备份 java mysql

java备份mysql数据库

package com.lear.util;

import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 数据库定时备份任务
 */
@Component
public class DbJob {
    private final Log logger = LogFactory.getLog(DbJob.class);

    @Scheduled(cron = "58 05 19 * * ?")
    public void back(){
        System.out.println("现在时间是"+new Date());
        Runtime runtime = Runtime.getRuntime();  //获取Runtime实例
        String user = "root";
        String password = "root";
        String database = "mydb"; // 需要备份的数据库名
        Date currentDate = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
        String sdfDate = sdf.format(currentDate);
        String filepath = "d:\\database_copy\\time_" + sdfDate + ".sql"; // 备份的路径地址
        //执行命令
        String stmt = "mysqldump --column-statistics=0 -h 127.0.0.1 -u "+user+" -p"+password+" --databases "+database+" > "+filepath;
        System.out.println(stmt);
        try {
            String[] command = { "cmd", "/c", stmt};
            Process process = runtime.exec(command);
            InputStream input = process.getInputStream();
            System.out.println(IOUtils.toString(input, "UTF-8"));
            //若有错误信息则输出
            InputStream errorStream = process.getErrorStream();
            System.out.println(IOUtils.toString(errorStream, "UTF-8"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

java还原mysql数据库

    @Scheduled(cron = "00 40 20 * * ?")
    /**
     * 导入Mysql数据库
     * @param hostIP 数据库地址
     * @param hostPort 端口
     * @param userName 用户名
     * @param password 密码
     * @param importFilePath 数据库文件路径
     * @param sqlFileName 要导入的文件名
     * @param databaseName 要导入的数据库名
     * @return
     * @throws InterruptedException
     */
    public static boolean importDatabase() {

        String hostIP="127.0.0.1";
        String hostPort="3306";
        String userName="root";
        String password="root";
        String importFilePath="D:\\database_copy\\";
        String sqlFileName="time_2022-03-13_19-05-58.sql";
        String databaseName="mydb";

        File imporFile = new File(importFilePath);
        if (!imporFile.exists()) {
            imporFile.mkdirs();
        }

        if (!importFilePath.endsWith(File.separator)) {
            importFilePath = importFilePath + File.separator;
        }

        //mysql -h127.0.0.1 -uroot -P3306 -p test<C:\chengzheming\backupDatabase\
        try {
            Process process = Runtime.getRuntime().exec("cmd /C" + "mysql -h"+hostIP+" -P"+hostPort+" -u"+userName+" -p"+password+" "+databaseName+"<"+importFilePath+sqlFileName);
            if (process.waitFor()==0){
                return true;
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e){
            e.printStackTrace();
        }
        return false;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值