1 package com.learn4j.bat; 2 3 public class Backup { 4 private String user_name;// 数据库用户名 5 private String user_psw;// 数据库密码 6 private String db_name;// 需要备份的数据库名 7 private String host_ip;// 主机IP 8 private String user_charset;// 字符集 9 private String backup_path; // 存放备份文件的路径 10 private String stmt;// 命令 11 12 public Backup(String user_name, String user_psw, String db_name, 13 String host_ip, String user_charset, String backup_path) { 14 this.user_name = user_name; 15 this.user_psw = user_psw; 16 this.db_name = db_name; 17 // 主机IP; 18 if (host_ip == null || host_ip.equals("")) 19 this.host_ip = "localhost";// 默认为本机 20 else 21 this.host_ip = host_ip; 22 // 字符集 23 if (user_charset == null || user_charset.equals("")) 24 this.user_charset = " "; // 默认为安装时设置的字符集 25 else 26 this.user_charset = " --default-character-set=" + user_charset; 27 this.backup_path = backup_path; 28 this.stmt = "c:\\wamp\\bin\\mysql\\mysql5.5.20\\bin\\mysqldump " 29 + this.db_name + " -h " + this.host_ip + " -u" + this.user_name 30 + " -p" + this.user_psw + this.user_charset + " --result-file=" 31 + this.backup_path; 32 } 33 34 public boolean backup_run() { 35 boolean run_result = false; 36 try { 37 Runtime.getRuntime().exec(this.stmt); 38 run_result = true; 39 } catch (Exception e) { 40 e.printStackTrace(); 41 } 42 return run_result; 43 } 44 45 public static void main(String[] args) { 46 Backup backup = new Backup("root", "123456", "student", null, "utf8", 47 "d:\\test.sql"); 48 boolean result = backup.backup_run(); 49 if (result) 50 System.out.println("备份成功"); 51 } 52 }
原文链接:http://blog.sina.com.cn/s/blog_59ae45de0100ds17.html
时间: 2024-11-19 10:14:27