windows和linux下备份mysql

近期因为项目需求,需要在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;
	}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值