package net.xx.util; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; /** * * @描述 工具类 - 备份与恢复Mysql数据库 * @创建人 xx * @创建时间 2011-6-16 上午10:45:14 * @版本 v1.0 * @修改人 * @修改时间 * @修改说明 */ public class MysqlUtil { /** * @功能描述 备份mysql数据库 * @创建人 xx * @创建时间 2011-6-16 上午10:25:58 * @param file_dir 备份文件文件目录 * @return 操作结果 */ public static boolean backupMysql(String file_dir) { try{ Properties properties = getProperties("mysql.properties"); // 持久化对象 String user_name = properties.getProperty("user_name"); // 用户名 String user_psw = properties.getProperty("user_psw"); // 密码 String db_name = properties.getProperty("db_name"); // 数据库名字 String host_ip = properties.getProperty("host_ip"); // ip String user_charset = properties.getProperty("user_charset"); // 字符编码 // ip if(host_ip == null || host_ip.equals("")) host_ip="localhost"; // 默认为本机 // 字符集 if(user_charset == null || user_charset.equals("")) user_charset=" "; // 默认为安装时设置的字符集 else user_charset=" --default-character-set="+user_charset+" "; // 设置字符集 // 获取文件对象 File file = getFile(file_dir, db_name); // 备份mysql // -u用户名(root) // -p密码(root) // -h端口号(localhost) // --default-character-set=字符标准(utf8) // chengxiang数据库 // e:\\test1.sql备份文件位置 // mysqldump -hlocalhost -uroot -proot --default-character-set=utf8 chengxiang --result-file=e:\\test1.sql String stmt = "mysqldump -h"+host_ip+" -u"+user_name+" -p"+user_psw+user_charset+db_name+" --result-file="+file.getPath(); // 执行dos, 备份数据库 Runtime.getRuntime().exec(stmt); return true; } catch(Exception e){ e.printStackTrace(); } return false; } /** * @功能描述 恢复mysql数据库 * @创建人 xx * @创建时间 2011-6-16 上午10:25:58 * @param file_dir 备份文件文件目录 * @param file_date 备份文件的日期(格式:20100620 - yyyyMMdd) * @return 操作结果 */ public static boolean recoverMysql(String file_dir, String file_date) { try{ Properties properties = getProperties("mysql.properties"); // 持久化对象 String user_name = properties.getProperty("user_name"); // 用户名 String user_psw = properties.getProperty("user_psw"); // 密码 String db_name = properties.getProperty("db_name"); // 数据库名字 String host_ip = properties.getProperty("host_ip"); // ip String user_charset = properties.getProperty("user_charset"); // 字符编码 // ip if(host_ip == null || host_ip.equals("")) host_ip="localhost"; // 默认为本机 // 字符集 if(user_charset == null || user_charset.equals("")) user_charset=" "; // 默认为安装时设置的字符集 else user_charset=" --default-character-set="+user_charset+" "; // 设置字符集 // 恢复mysql // -u用户名(root) // -p密码(root) // -h端口号(localhost) // --default-character-set=字符标准(utf8) // chengxiang数据库 // e:\\test1.sql备份文件位置 // mysqladmin -uroot -proot create chengxiang // cmd /c mysql -uroot -proot chengxiang < e:\\test1.sql String stmt1 = "mysqladmin -u"+user_name+" -p"+user_psw+" create "+db_name; String stmt2 = "cmd /c mysql -u"+user_name+" -p"+user_psw+" "+db_name+" < "+getOldFileName(file_dir, db_name, file_date); // 执行dos, 恢复数据库 Runtime.getRuntime().exec(stmt1); Runtime.getRuntime().exec(stmt2); return true; } catch(Exception e){ e.printStackTrace(); } return false; } /** * @功能描述 加载properties文件并获取持久化对象 * @创建人 xx * @创建时间 2011-6-16 上午10:25:58 * @param propertiesName properties文件名 * @return 持久化对象 */ private static Properties getProperties(String propertiesName) throws Exception { Properties properties = new Properties(); try { // 加载properties文件 properties.load(MysqlUtil.class.getClassLoader().getResourceAsStream(propertiesName)); return properties; } catch (IOException e) { e.printStackTrace(); } return null; } /** * @功能描述 设置并获取文件对象 * @创建人 xx * @创建时间 2011-6-21 下午10:02:43 * @param backup_dir 文件目录 * @param file_name 文件原名 * @return 文件对象 */ private static File getFile(String backup_dir, String file_name) throws Exception { try { File file = new File(backup_dir); // 当指定文件路径存在文件时, 删除(包括文件和目录) if(file.isFile()) throw new Exception("备份Mysql时, 指定文件目录为文件!"); // 当不为文件目录时, 创建目录(包括不存在的父目录也创建) if(!file.isDirectory()) { if(!file.mkdirs()) throw new Exception("备份Mysql时, 指定文件目录创建失败!"); } // 重新设置file, 当前为完全文件路径 file = new File(getNewFileName(backup_dir, file_name)); // 当指定文件路径存在文件时, 删除(包括文件和目录) if(file.isFile()) { if(!file.delete()) throw new Exception("备份Mysql时, 删除已存在的文件失败!"); } // 创建文件 if(!file.createNewFile()) throw new Exception("备份Mysql时, 创建新文件失败!"); return file; } catch (IOException e) { e.printStackTrace(); } return null; } /** * @功能描述 根据日期(yyyyMMdd)设置文件名字 * @创建人 xx * @创建时间 2011-6-21 下午09:00:16 * @param backup_dir 文件目录 * @param file_name 文件原名 * @return 文件全路径 */ private static String getNewFileName(String backup_dir, String file_name) { // 设置时间格式 SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); // 设置文件名字 file_name += format.format(new Date()) + ".sql"; // 返回 return backup_dir+"\\"+file_name; } /** * @功能描述 根据日期(yyyyMMdd)获取文件名字 * @创建人 xx * @创建时间 2011-6-21 下午09:00:16 * @param backup_dir 文件目录 * @param db_name 项目名字 * @param file_date 文件创建日期(格式:20100620 - yyyyMMdd) * @return 文件全路径 */ private static String getOldFileName(String backup_dir,String db_name, String file_date) throws Exception { File file = new File(backup_dir); // 当指定备份文件目录错误时 if(!file.isDirectory()) { throw new Exception("恢复Mysql时, 指定文件目录错误!"); } // 重新设置file, 当前为完全文件路径 file = new File(backup_dir+"\\"+db_name+file_date+".sql"); // 当指定文件路径存在文件时, 删除(包括文件和目录) if(!file.isFile()) { throw new Exception("恢复Mysql时, 指定文件错误!"); } return file.getPath(); } public static void main(String[] args) { // 根据文件目录, 创建目录或就在该目录, 生成数据库名+时间.sql的文件(chengxinag20110622.sql) //System.out.println(backupMysql("e:\\Mysql")); // 根据文件目录, 根据备份文件创建时间, System.out.println(recoverMysql("e:\\Mysql", "20110622")); } }

lironggang LV38
2023年4月25日
lwp011 LV27
2020年11月30日
阿凝是个小可爱 LV14
2020年5月22日
shanpengnian LV13
2019年9月3日
dachoumomo LV12
2019年7月23日
qq777444 LV4
2019年2月26日
oldfox LV19
2018年10月11日
swing123 LV7
2018年7月5日
JoyKinG LV19
2018年6月11日
dafeigenihao LV13
2017年11月2日

503382513 LV10
2024年12月4日
zzzyyy1 LV2
2024年2月26日
卢本伟不开挂 LV4
2023年9月3日
lironggang LV38
2023年4月25日
liuziqi0529 LV4
2022年3月5日
tcdt160426 LV1
2021年9月23日
newhaijun LV15
2020年12月13日
lwp011 LV27
2020年11月30日
j_heyao LV1
2020年9月29日
woldxy LV12
2020年7月8日