java 备份数据库
2022/4/30 2:14:01
本文主要是介绍java 备份数据库,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.common.taskTiming; import com.common.utils.DateFormatter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import java.util.Date; /** * @author Mongo * @date 2022-4-29-0029 下午 9:27:25 * @Email mongo_chen@wesurance.io * @return */ @Slf4j public class BackupUtil { private static final String dbBinPath = "D:\\mysql-5.6.40-winx64\\bin"; private static final String savePath = "D:\\a_db_backup\\"; private static final String ipPath = null; private String user_name;//数据库用户名 private String user_psw;//数据库密码 private String db_name;//需要备份的数据库名 private String host_ip;//主机IP private String user_charset;//字符集 private String backup_path;//存放备份文件的路径 private String stmt;//命令 public BackupUtil(String dbBinPath,String user_name, String user_psw, String db_name, String host_ip, String user_charset, String backup_path) { this.user_name = user_name; this.user_psw = user_psw; this.db_name = db_name; // 主机IP; if (StringUtils.isBlank(host_ip)) { // 默认为本机 this.host_ip = "localhost"; } else { this.host_ip = host_ip; } // 字符集 if (StringUtils.isBlank(user_charset)) { // 默认为安装时设置的字符集 this.user_charset = " "; } else { this.user_charset = " --default-character-set=" + user_charset; } this.backup_path = backup_path; this.stmt = dbBinPath + "\\mysqldump " + this.db_name + " -h " + this.host_ip + " -u" + this.user_name + " -p" + this.user_psw + this.user_charset + " --result-file=" + this.backup_path; } public boolean backup_run() { boolean run_result = false; try { Runtime.getRuntime().exec(this.stmt); run_result = true; } catch (Exception e) { e.printStackTrace(); } return run_result; } public static void main(String[] args) { String time = DateFormatter.getStringByDate(new DateFormatter().yyyyMMdd_HHmmss,new Date()); BackupUtil backup = new BackupUtil("root",//账号 "1234", //密码 "store", //库名 ipPath , //ip地址(默认本机) "utf8mb4",//字符集 savePath + "db_store_"+time+".sql"); boolean result = backup.backup_run(); if (result) { log.info("== 备份成功!=="); }else{ log.info("== 备份失败!=="); } } }
这篇关于java 备份数据库的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-27消息中间件底层原理资料详解
- 2024-11-27RocketMQ底层原理资料详解:新手入门教程
- 2024-11-27MQ底层原理资料详解:新手入门教程
- 2024-11-27MQ项目开发资料入门教程
- 2024-11-27RocketMQ源码资料详解:新手入门教程
- 2024-11-27本地多文件上传简易教程
- 2024-11-26消息中间件源码剖析教程
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器