Java implements database backup and recovery

Preface

In order to cope with the sudden crash of the database in the project, resulting in data loss, regular backup and recovery of the database can be implemented through code. Even if the database is down, we can restore the previously backed up data information to the database. Implementing database backup and recovery through java code is actually to operate the command line through java code.

So how to implement database backup and recovery? The following cases are available for reference.

1. Database backup

  /**
  * 数据库文件备份
  */
 @RequestMapping("/doBackup")
 public R doBackup(){
    
    
     System.out.println("现在时间是"+new Date());
     Runtime runtime = Runtime.getRuntime();  //获取Runtime实例
     String database1 = "equipments"; // 需要备份的数据库名
     Date currentDate = new Date();
     SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
     String sdfDate = sdf.format(currentDate);

     String filepath = "d:\\sql\\time_" + sdfDate + ".sql"; // 备份的路径地址
     //执行命令(此处需要定位到mysqldump.exe路径,--default-character-set=gbk是为了解决中文乱码问题)
     String stmt = "d:\\wamp64\\bin\\mysql\\mysql5.7.24\\bin\\mysqldump -h localhost -uroot -p12345678 --default-character-set=gbk "+database1+" > "+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, "gbk"));

         //保存备份数据信息到数据库中
         SqlFileEntity sqlFileEntity = new SqlFileEntity();
         sqlFileEntity.setName("time_" + sdfDate + ".sql");
         sqlFileEntity.setPath(filepath);
         sqlFileEntity.setCreateTime(currentDate);
         this.save(sqlFileEntity);
     } catch (IOException e) {
    
    
         e.printStackTrace();
     }
     return R.ok();
 }

2. Database recovery

  /**
  * 还原数据库
  */
 @RequestMapping("/restore")
 public R restore(@RequestBody String filename) {
    
    
     String database = "equipments"; // 需要备份的数据库名
     System.out.println("现在时间是" + new Date());
     Runtime runtime = Runtime.getRuntime();
     try {
    
    
         String filePath =  "d:\\sql\\"+filename; // sql文件路径
         String stmt = "mysql -h localhost -uroot -p12345678 "+database+"< " + filePath;
         System.out.println(stmt);
         String[] command = {
    
    "cmd", "/c", stmt};
         Process process = runtime.exec(command);
         //若有错误信息则输出
         InputStream errorStream = process.getErrorStream();
         System.out.println(IOUtils.toString(errorStream, "gbk"));
         //等待操作
         int processComplete = process.waitFor();
         if (processComplete == 0) {
    
    
             System.out.println("还原成功.");
         } else {
    
    
             throw new RuntimeException("还原数据库失败.");
         }
     } catch (Exception e) {
    
    
         e.printStackTrace();
     }
     return R.ok();
 }

Summarize

The above is what I will talk about today. This article only briefly introduces the Java code to implement database backup and recovery. If you want to implement scheduled backup and recovery of the database, you also need to write a timer or scheduled task to add database backup and recovery to the schedule. Just on task.

Guess you like

Origin blog.csdn.net/fish332/article/details/114178510