dbbackup.java
4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
//package com.huaheng.framework.dbbackups;
//
//import com.huaheng.common.exception.service.ServiceException;
//import org.junit.Test;
//
//import java.io.File;
//import java.io.IOException;
//import java.util.Date;
//import java.text.SimpleDateFormat;
//import java.util.Properties;
//
///**
// * @author zengbo
// * 数据库备份类
// * 使用前提是运行本机装有mysql客户端,Windows环境下,使用前需要将Mysql/bin文件夹中的mysqldump文件放到C盘根目录下。
// * Linux环境下,需先使用whereis mysqldump命令查看mysqldump命令所在路径;
// * mysqldump文件夹父级目录不能有空格等特殊符号,否则备份失败
// */
//public class dbbackup {
//
//
// @Test
// public void dbbackup() {
// /** 判断项目运行环境是linux还是windows */
// Properties prop = System.getProperties();
// String os = prop.getProperty("os.name");
// if (os != null && os.toLowerCase().indexOf("linux") > -1) {
// System.out.println("系统为linux!");
// try{
// String[] cmd=new String[]{"/bin/sh ","-c ","/usr/bin/mysqldump -uroot -padmin minas >/usr/2.sql "};
// Runtime.getRuntime().exec(cmd);
// }catch(Exception e){
// }
// } else if (os != null && os.toLowerCase().indexOf("win") > -1){
// System.out.println("系统为windows!");
// String hostIP = "172.16.29.45";
// String userName = "root";
// String password = "hhsoftware";
// String savePath = "D:\\DBbackups\\";
// Date currentTime = new Date();
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
// String Date = formatter.format(currentTime);
// String fileName = Date + "ningxiang_sfs";
// String databaseNmae = " ningxiang_sfs";
// if (backup(hostIP, userName, password, savePath, fileName, databaseNmae) == false) {
// throw new ServiceException("备份数据库失败!");
// }else {
// System.out.println("备份数据库成功!");
// }
// }
// }
//
// /**
// * @param hostIP ip地址,可以是本机也可以是远程
// * @param userName 数据库的用户名
// * @param password 数据库的密码
// * @param savePath 备份的路径
// * @param fileName 备份的文件名
// * @param databaseName 需要备份的数据库的名称
// * @return
// */
// public boolean backup(String hostIP, String userName, String password, String savePath, String fileName,
// String databaseName) {
// fileName += ".sql";
// File saveFile = new File(savePath);
// if (!saveFile.exists()) {// 如果目录不存在
// saveFile.mkdirs();// 创建文件夹
// }
// if (!savePath.endsWith(File.separator)) {
// savePath = savePath + File.separator;
// }
//
// //拼接命令行的命令
// StringBuilder stringBuilder = new StringBuilder();
// stringBuilder.append("C:\\mysqldump").append(" -h").append(hostIP).append(" -P3306");
// stringBuilder.append(" -u").append(userName).append(" -p").append(password).append(databaseName).append(">")
// .append(savePath).append(fileName);
//
// try {
// //调用外部执行exe文件的javaAPI
// System.out.println("cmd命令为:" + stringBuilder.toString());
// Runtime runtime = Runtime.getRuntime();
//
// System.out.println("开始备份:" + databaseName);
// Process process = runtime.exec("cmd /c " + stringBuilder.toString());
// System.out.println("备份成功!");
// process.waitFor();
// if (process.waitFor() == 0) {// 0 表示线程正常终止。
// if(process != null){
// process.getOutputStream().close();
// return true;
// }
// }
// } catch (InterruptedException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// return false;
// }
//
//}