ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

java 备份数据库

2022-04-29 22:33:06  阅读:128  来源: 互联网

标签:java name ip 备份 private user backup 数据库 String


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,name,ip,备份,private,user,backup,数据库,String
来源: https://www.cnblogs.com/xuehuashanghe/p/16208557.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有