近期因为项目需求,需要在windows下和linux下备份数据库和还原数据库。特此整理了一番。
//windows下备份mysql
public Map<String,String> backWindows(List<String> propList,String DB_BACKUP_SERVERPATH,String backupName){
Map<String,String> map = new HashMap<String,String>();
String db_name = "";
try {
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String date = sDateFormat.format(new java.util.Date());
//备份的数据库路径
String sqlPath = DB_BACKUP_SERVERPATH + File.separator + backupName + "_" + date + ".sql";
//执行备份数据库指令
String executeSql = " mysqldump " +" -h 127.0.0.1 "+ " -u " + propList.get(0) + " -p" + propList.get(1) +" "+ propList.get(2) +" > "+sqlPath;
Process proc = java.lang.Runtime.getRuntime().exec("cmd /c " + executeSql);
proc.waitFor();
db_name = backupName + "_" + date + ".sql";
File f = new File(sqlPath);
long t = f.length();
if(f.exists()){
DecimalFormat df = new DecimalFormat("###.###");
map.put("backupSize", String.valueOf(df.format((float) ((t)/(1024.0*1024.0)))));
}else {
map.put("backupSize", String.valueOf(0.0));
}
map.put("db_name", db_name);
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
return map;
}
//Linux下备份mysql
public Map<String,String> backLinux(List<String> propList,String DB_BACKUP_SERVERPATH,String backupName){
Map<String,String> map = new HashMap<String,String>();
String db_name = "";
try {
SimpleDateFormat sDateFormat = new SimpleDateFormat("yyyyMMddHHmmssSSS");
String date = sDateFormat.format(new java.util.Date());
//备份的数据库路径
String sqlPath = DB_BACKUP_SERVERPATH + File.separator + backupName + "_" + date + ".sql";
//执行备份数据库指令
String executeSql = " mysqldump " +" -h 127.0.0.1 "+ " -u " + propList.get(0) + " -p" + propList.get(1) +" "+propList.get(2)+" > "+sqlPath;
Process proc = java.lang.Runtime.getRuntime().exec( new String[] { "sh", "-c", executeSql});
proc.waitFor();
db_name = backupName + "_" + date + ".sql";
File f = new File(sqlPath);
long t = f.length();
if(f.exists()){
DecimalFormat df = new DecimalFormat("###.###");
map.put("backupSize", String.valueOf(df.format((float) ((t)/(1024.0*1024.0)))));
}else {
map.put("backupSize", String.valueOf(0.0));
}
map.put("db_name", db_name);
} catch (Exception e) {
logger.error(e.getMessage(),e);
}
return map;
}
//windows下还原mysql
public boolean restoreWindows(List<String> propList,String dbName,String DB_BACKUP_SERVERPATH){
boolean flag = false;
//还原的数据库路径
String sqlPath = DB_BACKUP_SERVERPATH + File.separator + dbName;
File filess = new File(sqlPath);
if(filess.exists()){
try {
//执行备份数据库指令
String executeSql = " mysql --host=localhost --port=3306 --user=" + propList.get(0) + " --password="+ propList.get(1) + " --default-character-set=utf8 "+propList.get(2) + " < " + sqlPath;
java.lang.Runtime.getRuntime().exec("cmd /c " + executeSql);
flag = true;
} catch (IOException e) {
logger.error(e.getMessage(),e);
flag = false;
}
}else{
//还原文件未找到
flag = false;
}
return flag;
}
//Linux下还原mysql
public boolean restoreLinux(List<String> propList,String dbName,String DB_BACKUP_SERVERPATH){
boolean flag = false;
//还原的数据库路径
String sqlPath = DB_BACKUP_SERVERPATH + File.separator + dbName;
File filess = new File(sqlPath);
if(filess.exists()){
try {
//执行备份数据库指令
String executeSql = " mysql --host=localhost --port=3306 --user=" + propList.get(0) + " --password="+ propList.get(1) + " --default-character-set=utf8 "+propList.get(2) + " < " + sqlPath;
java.lang.Runtime.getRuntime().exec( new String[] { "sh", "-c", executeSql});
flag = true;
} catch (IOException e) {
logger.error(e.getMessage(),e);
flag = false;
}
}else{
//还原文件未找到
flag = false;
}
return flag;
}