mysql备份
来源:互联网 发布:php怎么防止xss攻击 编辑:程序博客网 时间:2024/05/21 09:47
/**
*
* @praject dtm2
*
* @pakage com.dtm.common.utils
*
* TODO
*
*/
package com.dtm.common.utils;
/**
* @author Jianghong
*
* @createDate 2014-12-16 上午10:23:15
*
* @file q.java
*
* @email jiangjianhua123@qq.com
*
* @version 0.1
* MySQL数据库的备份与恢复 缺陷:可能会被杀毒软件拦截
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import com.dtm.common.DateUtil;
public class DatabaseBackup {
/** MySQL安装目录的Bin目录的绝对路径 */
private String mysqlBinPath;
/** 访问MySQL数据库的用户名 */
private String username;
/** 访问MySQL数据库的密码 */
private String password;
public String getMysqlBinPath() {
return mysqlBinPath;
}
public void setMysqlBinPath(String mysqlBinPath) {
this.mysqlBinPath = mysqlBinPath;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public DatabaseBackup(String mysqlBinPath, String username, String password) {
if (!mysqlBinPath.endsWith(File.separator)) {
mysqlBinPath = mysqlBinPath + File.separator;
}
this.mysqlBinPath = mysqlBinPath;
this.username = username;
this.password = password;
}
/**
* 备份数据库
*
* @param output
* 输出流
* @param dbname
* 要备份的数据库名
*/
public void backup(OutputStream output, String dbname) {
String command = "cmd /c " + mysqlBinPath + "mysqldump.exe -h localhost -u" + username
+ " -p" + password + " --set-charset=utf8 " + dbname;
PrintWriter p = null;
BufferedReader reader = null;
try {
p = new PrintWriter(new OutputStreamWriter(output, "utf8"));
Process process = Runtime.getRuntime().exec(command);
InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
reader = new BufferedReader(inputStreamReader);
String line = null;
while ((line = reader.readLine()) != null) {
p.println(line);
}
p.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (p != null) {
p.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 备份数据库,如果指定路径的文件不存在会自动生成
*
* @param dest
* 备份文件的路径
* @param dbname
* 要备份的数据库
*/
public void backup(String dest, String dbname) {
try {
OutputStream out = new FileOutputStream(dest);
backup(out, dbname);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 恢复数据库
*
* @param input
* 输入流
* @param dbname
* 数据库名
*/
public void restore(InputStream input, String dbname) {
String command = "cmd /c " + mysqlBinPath + "mysql -u" + username + " -p" + password + " " + dbname;
try {
Process process = Runtime.getRuntime().exec(command);
OutputStream out = process.getOutputStream();
String line = null;
String outStr = null;
StringBuffer sb = new StringBuffer("");
BufferedReader br = new BufferedReader(new InputStreamReader(input,"utf8"));
while ((line = br.readLine()) != null) {
sb.append(line + "/r/n");
}
outStr = sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
writer.flush();
out.close();
br.close();
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 恢复数据库
*
* @param dest
* 备份文件的路径
* @param dbname
* 数据库名
*/
public void restore(String dest, String dbname) {
try {
InputStream input = new FileInputStream(dest);
restore(input, dbname);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//mysql.binpath=C:\\"Program Files (x86)"\\MySQL\\"MySQL Server 5.5"\\bin
//connection.username=root
//connection.password=123
String binPath = DatabaseUtils.getProperties().getProperty("mysql.binpath");
String userName = DatabaseUtils.getProperties().getProperty("connection.username");
String pwd = DatabaseUtils.getProperties().getProperty("connection.password");
DatabaseBackup bak = new DatabaseBackup(binPath, userName, pwd);
bak.backup("d:/dtm"+DateUtil.parseToString(new Date(), "yyMMddHHmmss")+".sql", "dtm");
}
}
*
* @praject dtm2
*
* @pakage com.dtm.common.utils
*
* TODO
*
*/
package com.dtm.common.utils;
/**
* @author Jianghong
*
* @createDate 2014-12-16 上午10:23:15
*
* @file q.java
*
* @email jiangjianhua123@qq.com
*
* @version 0.1
* MySQL数据库的备份与恢复 缺陷:可能会被杀毒软件拦截
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import com.dtm.common.DateUtil;
public class DatabaseBackup {
/** MySQL安装目录的Bin目录的绝对路径 */
private String mysqlBinPath;
/** 访问MySQL数据库的用户名 */
private String username;
/** 访问MySQL数据库的密码 */
private String password;
public String getMysqlBinPath() {
return mysqlBinPath;
}
public void setMysqlBinPath(String mysqlBinPath) {
this.mysqlBinPath = mysqlBinPath;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public DatabaseBackup(String mysqlBinPath, String username, String password) {
if (!mysqlBinPath.endsWith(File.separator)) {
mysqlBinPath = mysqlBinPath + File.separator;
}
this.mysqlBinPath = mysqlBinPath;
this.username = username;
this.password = password;
}
/**
* 备份数据库
*
* @param output
* 输出流
* @param dbname
* 要备份的数据库名
*/
public void backup(OutputStream output, String dbname) {
String command = "cmd /c " + mysqlBinPath + "mysqldump.exe -h localhost -u" + username
+ " -p" + password + " --set-charset=utf8 " + dbname;
PrintWriter p = null;
BufferedReader reader = null;
try {
p = new PrintWriter(new OutputStreamWriter(output, "utf8"));
Process process = Runtime.getRuntime().exec(command);
InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");
reader = new BufferedReader(inputStreamReader);
String line = null;
while ((line = reader.readLine()) != null) {
p.println(line);
}
p.flush();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (reader != null) {
reader.close();
}
if (p != null) {
p.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
/**
* 备份数据库,如果指定路径的文件不存在会自动生成
*
* @param dest
* 备份文件的路径
* @param dbname
* 要备份的数据库
*/
public void backup(String dest, String dbname) {
try {
OutputStream out = new FileOutputStream(dest);
backup(out, dbname);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/**
* 恢复数据库
*
* @param input
* 输入流
* @param dbname
* 数据库名
*/
public void restore(InputStream input, String dbname) {
String command = "cmd /c " + mysqlBinPath + "mysql -u" + username + " -p" + password + " " + dbname;
try {
Process process = Runtime.getRuntime().exec(command);
OutputStream out = process.getOutputStream();
String line = null;
String outStr = null;
StringBuffer sb = new StringBuffer("");
BufferedReader br = new BufferedReader(new InputStreamReader(input,"utf8"));
while ((line = br.readLine()) != null) {
sb.append(line + "/r/n");
}
outStr = sb.toString();
OutputStreamWriter writer = new OutputStreamWriter(out, "utf8");
writer.write(outStr);
writer.flush();
out.close();
br.close();
writer.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 恢复数据库
*
* @param dest
* 备份文件的路径
* @param dbname
* 数据库名
*/
public void restore(String dest, String dbname) {
try {
InputStream input = new FileInputStream(dest);
restore(input, dbname);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
//mysql.binpath=C:\\"Program Files (x86)"\\MySQL\\"MySQL Server 5.5"\\bin
//connection.username=root
//connection.password=123
String binPath = DatabaseUtils.getProperties().getProperty("mysql.binpath");
String userName = DatabaseUtils.getProperties().getProperty("connection.username");
String pwd = DatabaseUtils.getProperties().getProperty("connection.password");
DatabaseBackup bak = new DatabaseBackup(binPath, userName, pwd);
bak.backup("d:/dtm"+DateUtil.parseToString(new Date(), "yyMMddHHmmss")+".sql", "dtm");
}
}
0 0
- php备份备份mysql
- MySQL备份
- MySql备份
- mysql备份
- mysql 备份
- mysql备份
- mysql备份
- MySql备份
- mysql备份
- mysql备份
- mysql 备份
- MySQL 备份
- Mysql备份
- mysql备份
- mysql 备份
- mysql 备份
- mysql备份
- mysql 备份
- 28-反射机制-05-反射机制(获取Class中的字段)
- I学霸官方免费教程十九:Java常用类之String类
- 2015年8月11日--日常工作总结
- 关于xloader和uboot的几个初级问题
- DIV属性
- mysql备份
- LeetCode -- 顺时针旋转图片90度
- 9.9递归和动态规划(五)——确定某字符串的所有排列组合
- hdu 1247 Hat’s Words
- HDU 5374 Tetris(模拟俄罗斯方块)
- 28-反射机制-06-反射机制(获取Class中的方法)
- android:ToolBar详解
- Yosemite下安装jdk
- B. Gerald is into Art