今天给大家写的内容是用Java实现MySQL的备份与恢复 backup方法是 备份 load 方法是恢复 import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class JavaMysql { /** * * mysql数据备份 接收脚本名,并返回此路径 * * sql为备份的脚本名比如xxx.sql * */ public static void backup(String sql) { Properties pros = getPprVue("prop.properties"); // 这里是读取的属性文件,也可以直接使用 String username = pros.getProperty("username"); String password = pros.getProperty("password"); // 得到MYSQL的用户名密码后调用 mysql 的 cmd: String mysqlpaths = pros.getProperty("mysqlpath"); String databaseName = pros.getProperty("databaseName"); String address = pros.getProperty("address"); String sqlpath = pros.getProperty("sql"); File backupath = new File(sqlpath); if (!backupath.exists()) { backupath.mkdir(); } StringBuffer sb = new StringBuffer(); sb.append(mysqlpaths); sb.append("mysqldump "); sb.append("--opt "); sb.append("-h "); sb.append(address); sb.append(" "); sb.append("--user="); sb.append(username); sb.append(" "); sb.append("--password="); sb.append(password); sb.append(" "); sb.append("--lock-all-tables=true "); sb.append("--result-file="); sb.append(sqlpath); sb.append(sql); sb.append(" "); sb.append("--default-character-set=utf8 "); sb.append(databaseName); Runtime cmd = Runtime.getRuntime(); try { Process p = cmd.exec(sb.toString()); } catch (IOException e) { e.printStackTrace(); } } // 读取属性值 public static Properties getPprVue(String properName) { InputStream inputStream = mysql_util.class.getClassLoader() .getResourceAsStream(properName); Properties p = new Properties(); try { p.load(inputStream); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return p; } public static void load(String filename) { Properties pros = getPprVue("prop.properties"); // 这里是读取的属性文件,也可以直接使用 String root = pros.getProperty("jdbc.username"); String pass = pros.getProperty("jdbc.password"); // 得到MYSQL的用户名密码后调用 mysql 的 cmd: String mysqlpaths = pros.getProperty("mysqlpath"); String sqlpath = pros.getProperty("sql"); String filepath = sqlpath + filename; // 备份的路径地址 // 新建数据库finacing String stmt1 = "mysqladmin -u " + root + " -p" + pass + " create finacing"; // -p后面加的是你的密码 String stmt2 = "mysql -u " + root + " -p" + pass + " finacing < " + filepath; String[] cmd = { "cmd", "/c", stmt2 }; try { Runtime.getRuntime().exec(stmt1); Runtime.getRuntime().exec(cmd); System.out.println("数据已从 " + filepath + " 导入到数据库中"); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { backup("xx.sql"); //load("xx.sql"); } } import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Properties; public class JavaMysql { /** * * mysql数据备份 接收脚本名,并返回此路径 * * sql为备份的脚本名比如xxx.sql * */ public static void backup(String sql) { Properties pros = getPprVue("prop.properties"); // 这里是读取的属性文件,也可以直接使用 String username = pros.getProperty("username"); String password = pros.getProperty("password"); // 得到MYSQL的用户名密码后调用 mysql 的 cmd: String mysqlpaths = pros.getProperty("mysqlpath"); String databaseName = pros.getProperty("databaseName"); String address = pros.getProperty("address"); String sqlpath = pros.getProperty("sql"); File backupath = new File(sqlpath); if (!backupath.exists()) { backupath.mkdir(); } StringBuffer sb = new StringBuffer(); sb.append(mysqlpaths); sb.append("mysqldump "); sb.append("--opt "); sb.append("-h "); sb.append(address); sb.append(" "); sb.append("--user="); sb.append(username); sb.append(" "); sb.append("--password="); sb.append(password); sb.append(" "); sb.append("--lock-all-tables=true "); sb.append("--result-file="); sb.append(sqlpath); sb.append(sql); sb.append(" "); sb.append("--default-character-set=utf8 "); sb.append(databaseName); Runtime cmd = Runtime.getRuntime(); try { Process p = cmd.exec(sb.toString()); } catch (IOException e) { e.printStackTrace(); } } // 读取属性值 public static Properties getPprVue(String properName) { InputStream inputStream = mysql_util.class.getClassLoader() .getResourceAsStream(properName); Properties p = new Properties(); try { p.load(inputStream); inputStream.close(); } catch (IOException e) { e.printStackTrace(); } return p; } public static void load(String filename) { Properties pros = getPprVue("prop.properties"); // 这里是读取的属性文件,也可以直接使用 String root = pros.getProperty("jdbc.username"); String pass = pros.getProperty("jdbc.password"); // 得到MYSQL的用户名密码后调用 mysql 的 cmd: String mysqlpaths = pros.getProperty("mysqlpath"); String sqlpath = pros.getProperty("sql"); String filepath = sqlpath + filename; // 备份的路径地址 // 新建数据库finacing String stmt1 = "mysqladmin -u " + root + " -p" + pass + " create finacing"; // -p后面加的是你的密码 String stmt2 = "mysql -u " + root + " -p" + pass + " finacing < " + filepath; String[] cmd = { "cmd", "/c", stmt2 }; try { Runtime.getRuntime().exec(stmt1); Runtime.getRuntime().exec(cmd); System.out.println("数据已从 " + filepath + " 导入到数据库中"); } catch (IOException e) { e.printStackTrace(); } } public static void main(String[] args) throws IOException { backup("xx.sql"); //load("xx.sql"); } } ---------------------------------------prop.properties username = root password = root mysqlpath = C://Program Files//MySQL//MySQL Server 5.0//bin// sql = E://MySQl// address=localhost databaseName=test
JAVA实现MySQL的备份与恢复
最新推荐文章于 2022-10-10 11:01:57 发布