java代碼--mysql數據的備份和還原

備份數據庫的代碼:

/**
	 * mysql數據庫備份
	 * 
	 * @param savePath
	 * @param fileName
	 * @param hostIP
	 * @param userName
	 * @param passWord
	 * @param databaseName
	 */
	public static void backup(String savePath, String fileName, String hostIP,
			String userName, String passWord, String databaseName) {
		try {
			Runtime rt = Runtime.getRuntime();
			System.out.println("備份開始······");

			Process child = rt.exec("mysqldump -h " + hostIP + " -u" + userName
					+ " -p" + passWord + "  " + databaseName);

			InputStream in = child.getInputStream();

			InputStreamReader xx = new InputStreamReader(in, "utf-8");

			String inStr;
			StringBuffer sb = new StringBuffer("");
			String outStr;

			BufferedReader br = new BufferedReader(xx);
			while ((inStr = br.readLine()) != null) {
				sb.append(inStr + "\r\n");
			}
			outStr = sb.toString();

			File saveFile = new File(savePath);
			if (!saveFile.exists()) {
				saveFile.mkdirs();
			}
			FileOutputStream fout = new FileOutputStream(savePath + "/"
					+ fileName + ".sql");
			OutputStreamWriter writer = new OutputStreamWriter(fout, "utf-8");
			writer.write(outStr);
			writer.flush();
			in.close();
			xx.close();
			br.close();
			writer.close();
			fout.close();

			System.out.println("");
			System.out.println("備份成功······");

		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("備份失敗······");
		}

	}
還原數據庫的代碼:
/**
	 * Mysql數據庫的還原
	 * 
	 * @param fPath
	 * @param hostIP
	 * @param userName
	 * @param passWord
	 * @param databaseName
	 */
	public static void load1(String fPath, String hostIP, String userName,
			String passWord, String databaseName) {
		try {
			System.out.println("還原開始·······");
			// String fPath = "c:/test.sql";

			Runtime rt = Runtime.getRuntime();

			// 調用mysql的cmd命令
			Process child = rt.exec("mysql -u" + userName + " -p" + passWord
					+ "  " + databaseName);
			OutputStream out = child.getOutputStream();
			String inStr;
			StringBuffer sb = new StringBuffer("");
			String outStr;
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(fPath), "utf8"));
			while ((inStr = br.readLine()) != null) {
				sb.append(inStr + "\r\n");
			}
			outStr = sb.toString();

			OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
			writer.write(outStr);

			writer.flush();

			out.close();
			br.close();
			writer.close();

			System.out.println("");
			System.out.println("還原成功······");
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("還原失敗······");
		}

	}
	
測試代碼:

public static void main(String args[]) throws IOException {
		
		
		/**************** 測試Mysql數據庫的備份begin **************************************/
		/*String hostIP = "localhost";
		String userName = "root";
		String passWord = "root";
		String databaseName = "db_test";
		String savePath = "F:/backup";
		String fileName = "db_test11";
		String fPath = "F:/backup/db_test.sql";

		backup(savePath, fileName, hostIP, userName, passWord, databaseName);
		load1(fPath, hostIP, userName, passWord, databaseName);*/
		/******************************* 測試Mysql數據庫的備份end **************************************/

	}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章