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;

/**
 * Mysql数据备份工具
 * @USER: Cat Slave
 **/
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;
    }

    /**
     * 备份全部
     * @return
     */
    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;
    }

    /**
     * 备份数据库某个表
     * @param tableName  表名
     * @return
     */
    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;
    }

    /**
     * 备份数据库某个表加条件
     * @param tableName  表名
     * @param whereStr   条件字符串
     * @return
     */
    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;
    }

    /**
     * 备份数据库多个表加条件
     * @param tableNames  表名数组
     * @param whereStr   条件字符串
     * @return
     */
    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;
    }

    /**
     * 压缩sql文件为zip
     * @param filePath sql文件路径
     * @param zipPath  要生成的zip压缩包路径
     */
    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成功");
    }

} 

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值