数据备份和还原_java

数据备份和还原

数据备份

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

	private final static String hostIP = "ip";
    private final static String userName = "username";
    private final static String password = "pw";
    private final static String databaseName = "databaseName";

    /**
     * 备份数据库
     * savePath sql存放全路径前缀
     * name sql文件名称
     * @return
     */
    public static String dumpSql(String name,String savePath){

        String fileName = name+new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+".sql";
        File saveFile = new File(savePath);
        // 如果目录不存在
        if (!saveFile.exists()) {
            // 创建文件夹
            saveFile.mkdirs();
        }
        if(!savePath.endsWith(File.separator)){
            savePath = savePath + File.separator;
        }
        String dumpStr = " mysqldump -h" + hostIP + " -u" + userName + " -p" + password + " --set-charset=UTF8 " + databaseName;
        try {
            PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));
            cmdExecute(dumpStr,printWriter);
        }catch (Exception e) {
            e.printStackTrace();
        }
        return fileName;
    }

	public static void cmdExecute(String cmd,PrintWriter printWriter){
        BufferedReader bufferedReader = null;
        try {
            Process process = Runtime.getRuntime().exec(cmd);
            InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            while((line = bufferedReader.readLine())!= null){
                printWriter.println(line);
            }
            printWriter.flush();
        }catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (bufferedReader != null) {
                    bufferedReader.close();
                }
                if (printWriter != null) {
                    printWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

数据还原

	private final static String hostIP = "ip";
    private final static String userName = "username";
    private final static String password = "pw";
    private final static String databaseName = "databaseName";
/**
     * 还原数据库
     * filepath sql全路径
     * @return
     */
    public static void reductionSql(String filepath) throws IOException {
        String stmt2 = "mysql -h " + hostIP + " -u " + userName + " -p" + password +" " + databaseName;
        System.out.println(stmt2);
        Runtime rt = Runtime.getRuntime();
        Process child =
                rt.exec(stmt2);
        OutputStream out = child.getOutputStream();//控制台的输入信息作为输出流
        String inStr;
        StringBuffer sb = new StringBuffer();
        String outStr;
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(filepath), "utf8"));
        while ((inStr = br.readLine()) != null) {
            sb.append(inStr + "\r\n");
        }
        outStr = sb.toString();
        OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
        writer.write(outStr);
        // 注:这里如果用缓冲方式写入文件的话,会导致中文乱码,用flush()方法则可以避免
        writer.flush();
        // 别忘记关闭输入输出流
        out.close();
        br.close();
        writer.close();
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值