栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java备份导出Mysql数据方法

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java备份导出Mysql数据方法

闲话不多说,直接上代码(*^_^*)~~~~~~~~~~~~~~

import com.hx.exception.TipsException;
import com.hx.util.DateUtil;

import java.io.*;
import java.util.Date;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class MysqlDataBackupUtil {

    //sql文件存储的路径
    private static final String savePath = "C:/work/sql/";
    // 用户名
    private static final String username = "root";
    // 密码
    private static final String password = "root";
    // 导入的目标数据库所在的主机
    private static final String host = "localhost";
    // 使用的端口号
    private static final String port = "3306";
    // 导入的目标数据库的名称
    private static final String exportDatabaseName = "vote";
    // mysql bin目录路径
    private static final String MysqlPath = getMysqlPath();

    private static final int BUFFER = 8192;

    public static void main(String args[]){
        String exportPath = savePath + exportDatabaseName +"_"+ DateUtil.formatDate_1(new Date()) + ".sql";

        //每次处理都先删除文件
        if(!FileUtil.deleteFolder(savePath)){
            throw new TipsException("删除文件失败!");
        }

        File file = new File(savePath);
        //判断文件目录是否存在
        if(!file.exists()){
            file.mkdirs();
        }

        //        backupAll();
//        backupTable("zip_record");
//        backupTableByWhere(exportPath, "zip_record", "activityNo='1'");

        //需要备份数据的表名
        String [] tables = { "activity""banner", "data_cul_item", "modular","user" };

        backupMoreTableByWhere(exportPath, tables, "activityNo='1'");
    }

    //根据系统判断使用mysql命令路径
    private static String getMysqlPath(){
        String osName = System.getProperty("os.name");
        if(Pattern.matches("Linux.*", osName)){
            //linux读动态库,把动态库复制到/usr/lib下
            return "/usr/local/mysql/bin/";
        } else if (Pattern.matches("Windows.*", osName)) {
            //windows系统读动态库
            return "C:\Program Files\MySQL\MySQL Server 5.6\bin\";
        }
        return null;
    }

    
    public static boolean backupAll(String exportPath) {
        Runtime runtime = Runtime.getRuntime();
        // 这里其实是在命令窗口中执行的 command 命令行
        try {
            StringBuffer command = new StringBuffer();
            // 注意哪些地方要空格,哪些不要空格 || 密码是用的小p,而端口是用的大P。
            command.append(MysqlPath).append("mysqldump -u").append(username).append(" -p").append(password)
                    .append(" -h").append(host).append(" -P").append(port).append(" ").append(exportDatabaseName)
                    .append(" -r ").append(exportPath);
            runtime.exec(command.toString());
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    
    public static boolean backupTable(String exportPath, String tableName) {
        Runtime runtime = Runtime.getRuntime();
        // 这里其实是在命令窗口中执行的 command 命令行
        try {
            StringBuffer command = new StringBuffer();
            // 注意哪些地方要空格,哪些不要空格 || 密码是用的小p,而端口是用的大P。
            command.append(MysqlPath)
                    //账号和密码
                    .append("mysqldump -u").append(username).append(" -p").append(password)
                    //地址和端口
                    .append(" -h").append(host).append(" -P").append(port)
                    //导出数据库
                    .append(" ").append(exportDatabaseName)
                    //导出表名
                    .append(" ").append(tableName)
                    //生成文件位置
                    .append(" -r ").append(exportPath);
            runtime.exec(command.toString());
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    
    public static boolean backupTableByWhere(String exportPath, String tableName, String whereStr) {
        Runtime runtime = Runtime.getRuntime();
        // 这里其实是在命令窗口中执行的 command 命令行
        try {
            StringBuffer command = new StringBuffer();
            // 注意哪些地方要空格,哪些不要空格 || 密码是用的小p,而端口是用的大P。
            command.append(MysqlPath)
                    //账号和密码
                    .append("mysqldump -u").append(username).append(" -p").append(password)
                    //地址和端口
                    .append(" -h").append(host).append(" -P").append(port)
                    //导出数据库
                    .append(" ").append(exportDatabaseName)
                    //导出表名
                    .append(" ").append(tableName)
                    //导出条件,whereStr字符串如:'id<5'
                    .append(" --where=").append(whereStr)
                    //生成文件位置
                    .append(" -r ").append(exportPath);
            runtime.exec(command.toString());
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    
    public static boolean backupMoreTableByWhere(String exportPath, String [] tableNames, String whereStr) {
        Runtime runtime = Runtime.getRuntime();
        // 这里其实是在命令窗口中执行的 command 命令行
        try {
            StringBuffer command = new StringBuffer();
            // 注意哪些地方要空格,哪些不要空格 || 密码是用的小p,而端口是用的大P。
            command.append(MysqlPath);
            //账号和密码
            command.append("mysqldump -u").append(username).append(" -p").append(password);
            //地址和端口
            command.append(" -h").append(host).append(" -P").append(port);
            //导出数据库
            command.append(" ").append(exportDatabaseName);
            //导出表名
            for(String tableName : tableNames){
                command.append(" ").append(tableName);
            }
            //导出条件,whereStr字符串如:'id<5'
            command.append(" --where=").append(whereStr);
            //生成文件位置
            command.append(" -r ").append(exportPath);
            runtime.exec(command.toString());
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    
    public static void zipFile(String filePath,String zipPath) {
        ZipOutputStream out = null;
        try {
            out = new ZipOutputStream(new FileOutputStream(zipPath));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        //得到文件列表信息
        File file = new File(filePath);
        // 压缩zip包
        try {
            if (!file.exists()) {
                return;
            }
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
            try {

                ZipEntry entry = new ZipEntry(file.getName());
                out.putNextEntry(entry);
                int count;
                byte data[] = new byte[BUFFER];
                while ((count = bis.read(data, 0, BUFFER)) != -1) {
                    out.write(data, 0, count);
                }

            } catch (Exception e) {
                throw new RuntimeException(e);
            }finally {
                out.closeEntry();
                bis.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("生成zip成功");
    }

} 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/777907.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号