懒人策略之:批量备份mysql数据库
写给和我一样的懒人。
数据库多了,备份起来比较麻烦,虽然有Navicat这种工具,但是还是要一个一个去备份,不是很方便,下面这个类可以帮你方便的备份多个数据库。
我所在的是小公司,用到的数据库文件都很小,没见过大的数据库,所以无从测试,有朋友帮忙测试一下,有问题的话描述一下,3q
package com.djwl.tools.database;import java.io.BufferedReader;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStreamWriter;import java.util.ArrayList;import java.util.Calendar;import java.util.List;/** * 描述:批量备份数据库 * * 1. 修改数据库链接:ip、username、password * 2. 修改输出的文件夹:outputPath * 3. 修改getDatabaseList(),加入你的数据库名称 * 4. 保证你配置了mysql的环境变量 * * @author 胡晓 http://huxiao.iteye.com/ kskr@qq.com QQ:376665005 * * 原文链接:http://huxiao.iteye.com/admin/blogs/604093 * 转载请保留作者信息 */public class BackUpMysql {private static String ip = "localhost";private static String username = "root";private static String password = "root";//输出文件夹,我加了一个时间,保证每次生成在不同的文件夹里面,避免多次备份的文件在同一个文件夹里面,影响视听private static String outputPath = "g:/backupdatabase/" + getCurrentDate14() + "/";/** * <p>功能描述:备份数据库</p> * * @param databaseName * @param outputPath * @author 胡晓 <BR> kskr@qq.com <BR> * 时间:Feb 27, 2010 8:34:24 AM <BR> */public static void backup(String databaseName) {try {Process process = Runtime.getRuntime().exec("mysqldump.exe -h" + ip + " -u" + username + " -p" + password + " " + databaseName);InputStream input = process.getInputStream();InputStreamReader xx = new InputStreamReader(input, "utf8");String inputString;StringBuffer sb = new StringBuffer("");String outputString;BufferedReader br = new BufferedReader(xx);while ((inputString = br.readLine()) != null) {sb.append(inputString + "\r\n");}outputString = sb.toString();FileOutputStream output = new FileOutputStream(outputPath + databaseName + "_" + getCurrentDate14() + ".sql");OutputStreamWriter writer = new OutputStreamWriter(output, "utf8");writer.write(outputString);writer.flush();input.close();xx.close();br.close();writer.close();output.close();} catch (Exception e) {System.out.println("备份出错");e.printStackTrace();}}/** * <p>功能描述:创建文件夹</p> * * @param path * @author 胡晓 <BR> kskr@qq.com <BR> * 时间:Feb 27, 2010 8:48:27 AM <BR> */public static void createFolder(String path) {try {File file = new File(path);if (!file.exists()) {file.mkdirs();}} catch (Exception e) {System.out.println("创建文件夹出错");e.printStackTrace();}}/** * <p>功能描述:生成之后自动打开所在文件夹</p> * * @param folderPath * @author 胡晓 <BR> kskr@qq.com <BR> * 时间:Feb 27, 2010 8:48:37 AM <BR> */public static void openFolder(String folderPath) {try {// 打开所在文件夹String path = folderPath.replaceAll("/", "\\\");String cmd[] = { "explorer.exe", path };Runtime.getRuntime().exec(cmd);} catch (Exception e) {System.out.println("打开文件夹“" + folderPath + "”出错");e.printStackTrace();}}/** * <p>功能描述:获取14位的当前时间</p> * * @return * @author 胡晓 <BR> kskr@qq.com <BR> * 时间:Feb 27, 2010 8:47:36 AM <BR> */public static Long getCurrentDate14(){return Long.parseLong(new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()).toString());}/** * <p>功能描述:获取数据库列表</p> * * 这里的数据库列表实际上是可以从mysql自带的information_schema数据库中的tables表中读出来的,如果你懒得像我这样一一列举,可以采取那种方式 * @return * @author 胡晓 <BR> kskr@qq.com <BR> * 时间:Feb 27, 2010 8:48:58 AM <BR> */public static List<String> getDatabaseList() {List<String> list = new ArrayList<String>();list.add("aliwangpu");list.add("djwl");list.add("dog");list.add("dyfy");list.add("fydagl");list.add("hngk");list.add("hxzg");list.add("oa");list.add("practise");list.add("qzjy");list.add("salecount");list.add("swcm");list.add("xszp");list.add("yfhj");list.add("yfjy");return list;}public static void main(String[] args) {createFolder(outputPath);for (String databaseName : getDatabaseList()) {backup(databaseName);}openFolder(outputPath);}}