设为首页 加入收藏

TOP

Java 在Window及Linux下备份MySQL数据库(一)
2014-11-24 07:23:51 来源: 作者: 【 】 浏览:6
Tags:Java Window Linux 备份 MySQL 数据库

  首先是Windows系统下:


  给大家提个醒:


  有人说用:"mysqldump --uroot --p123456 --opt"。但是我没有成功,最后使用这种写法成功了:"mysqldump --user=root --password=123456 --opt" (其实正确的为:mysqldump -uroot -p123456 dbname)


  看来要写完整的方式。


  然后是Linux系统下:


  这里很多人遇到的问题是


  程序不报错,命令不执行,sql文件没有生成。我已开始用了以下几种写法:java.lang.Runtime.getRuntime().exec(new String[] { mysql });


  java.lang.Runtime.getRuntime().exec(new String[] { "shell "+mysql });


  java.lang.Runtime.getRuntime().exec(new String[] { "/bin/bash "+mysql });


  java.lang.Runtime.getRuntime().exec(new String[] { "/bin/bash/shell "+mysql });


  都没有成功,最后试验了这种写法成功了:


  java.lang.Runtime.getRuntime().exec(new String[] { "sh", "-c", mysql });


  下面是完整代码:


  -----------------------------------


import java.text.SimpleDateFormat;
import java.util.Date;


/**
*
* @author xxx
*
*
*/


public class BackUpMySQL {


private static final String BASE_PATH_LINUX = "/root/";


private static final String BASE_PATH_WINDEWS = "E:\\";


public void backwindows() {


try {


String sqlname = BASE_PATH_WINDEWS + "shequ."


+ new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
+ ".sql";


String mysql = "mysqldump --user=root --password=jwl --opt shequ> "


+ sqlname;


java.lang.Runtime.getRuntime().exec("cmd /c " + mysql);


} catch (Exception e) {


e.printStackTrace();


}


}


public void backlinux() {
try {


String sqlname = BASE_PATH_LINUX + "mqney."


+ new Date().toString() + ".sql";


String mysql = "mysqldump --user=root --password=123456 --opt mqney> "


+ sqlname;


java.lang.Runtime.getRuntime().exec(


new String[] { "sh", "-c", mysql });


} catch (Exception e) {


e.printStackTrace();


}
}


public static void main(String[] args) {
// new BackUpMySQL().backwindows();
System.out.println(System.getProperty("os.name"));
}
}


附另外一份备份源码参考:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.Date;


public class BakMysql {
// main的方法,主要是我用于测试的,是想着取得CLASS的路径,然后备份的文件写在服务器的类路径下
public static void main(String[] args) {
BakMysql bk = new BakMysql();
// System.out.println(Thread.currentThread().getContextClassLoader().
// getResource(""));
// System.out.println(BakMysql.class.getClassLoader().getResource(""));
// System.out.println(ClassLoader.getSystemResource(" "));
// System.out.println(BakMysql.class.getResource(""));
// System.out.println(BakMysql.class.getResource("/"));
// Class文件所在路径System.out.println(new File("/").getAbsolutePath());
System.out.println(System.getProperty("user.dir"));
// bk.backup();
// bk.load();
bk.backupMySqlToFile();
}


// backup方法是备份数据库到服务器地址
public void backup() {
try {
String filePath = System.getProperty("user.dir") + "\\"
+ new Date().getYear() + "-" + new Date().getMonth() + "-"
+ new Date().getDay() + ".sql";


System.out.println(BakMysql.cl

首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Linux Bash编程基础 下一篇Linux下一个简单的客户端和服务端

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Redis 分布式锁全解 (2025-12-25 17:19:51)
·SpringBoot 整合 Red (2025-12-25 17:19:48)
·MongoDB 索引 - 菜鸟 (2025-12-25 17:19:45)
·What Is Linux (2025-12-25 16:57:17)
·Linux小白必备:超全 (2025-12-25 16:57:14)