一、存储在七牛云
1、在七牛云的 ”对象存储“-->新建存储空间(没有认证的情况下,对象存储应该有1G)
2、导入 七牛云包
okhttp-3.3.1.jar
okio-1.8.0.jar
qiniu-java-sdk-7.2.7.jar
<dependencies>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>qiniu-java-sdk</artifactId>
<version>[7.2.0, 7.2.99]</version>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.3.1</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.qiniu</groupId>
<artifactId>happy-dns-java</artifactId>
<version>0.1.4</version>
<scope>compile</scope>
</dependency>
</dependencies>
3、编写代码
private void backupDb() {
String pghomeString = "D:\\Software86\\MySQL\\MySQLServer5.1\\bin\\";
StringBuilder command = new StringBuilder();
//存储test里的user_bill表
command.append(pghomeString).append("mysqldump -h localhost -uroot -proot --opt --set-charset=UTF8 test user_bill");
try {
Runtime cmd = Runtime.getRuntime();
Process p = cmd.exec(command.toString());
this.uploadToQiNiuYun(p.getInputStream());
if (p.waitFor() == 0) {
System.out.println("Backup created successfully!");
} else {
System.out.println("Could not create the backup");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException exception){
System.out.println("InterruptedException");
private void uploadToQiNiuYun(InputStream inputStream) throws IOException {
//构造一个带指定Zone对象的配置类
Configuration cfg = new Configuration(Zone.zone2());
//...其他参数参考类注释
// 华东 Zone.zone0()
// 华北 Zone.zone1()
// 华南 Zone.zone2()
// 北美 Zone.zoneNa0()
UploadManager uploadManager = new UploadManager(cfg);
//...生成上传凭证,然后准备上传
String accessKey = "eSXcw3Lq_aAZDztVGuTB379i";//这里请替换成自己的AK
String secretKey = "xOJaACrwCPRGhrcJ8GbmXz4f";//这里请替换成自己的SK
String bucket = "sqlbackup";//--空间名
//默认不指定key的情况下,以文件内容的hash值作为文件名
String key = "user_bill.sql";
ByteArrayOutputStream swapStream = new ByteArrayOutputStream();
byte[] buff = new byte[600]; //buff用于存放循环读取的临时数据
int rc = 0;
while ((rc = inputStream.read(buff, 0, 100)) > 0) {
swapStream.write(buff, 0, rc);
}
byte[] uploadBytes = swapStream.toByteArray(); //uploadBytes 为转换之后的结果
//上传凭证
Auth auth = Auth.create(accessKey, secretKey);
String upToken = auth.uploadToken(bucket);
try {
Response response = uploadManager.put(uploadBytes, key, upToken);
//解析上传成功的结果
DefaultPutRet putRet = new Gson().fromJson(response.bodyString(), DefaultPutRet.class);
System.out.println(putRet.key);
System.out.println(putRet.hash);
} catch (QiniuException ex) {
Response r = ex.response;
System.err.println(r.toString());
try {
System.err.println(r.bodyString());
} catch (QiniuException ex2) {
//ignore
}
}
//}
}
一、存储到本地磁盘
private void backupDb() {
String pghomeString = "D:\\Software86\\MySQL\\MySQLServer5.1\\bin\\";
StringBuilder command = new StringBuilder();
command.append(pghomeString).append("mysqldump -h localhost -uroot -proot --opt --set-charset=UTF8 test user_bill");
PrintWriter printWriter = null;
FileOutputStream fileOutput = null;
InputStreamReader inputStreamReader = null;
BufferedReader bufferedReader = null;
try {
Runtime cmd = Runtime.getRuntime();
Process p = cmd.exec(command.toString());
fileOutput = new FileOutputStream("G:\\user_bill.sql");
printWriter = new PrintWriter(new OutputStreamWriter(fileOutput,"utf8"));
inputStreamReader = new InputStreamReader(p.getInputStream(), "utf8");
bufferedReader = new BufferedReader(inputStreamReader);
String line;
while((line = bufferedReader.readLine())!= null){
printWriter.println(line);
}
printWriter.flush();
if (p.waitFor() == 0) {
System.out.println("Backup created successfully!");
} else {
System.out.println("Could not create the backup");
}
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException exception){
System.out.println("InterruptedException");
} finally{
try {
if(null != bufferedReader) {
bufferedReader.close();
}
if(null != printWriter) {
printWriter.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
三、直接转存整个database
command.append(pghomeString).append("mysqldump.exe").append(" --default-character-set=utf8").append(" -u")
.append("用户名").append(" -p").append("密码").append(" ").append("数据库名")
.append(" -B -r ").append("备份路径");
以上代码已经过测试