⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 backupdbutil.java

📁 SSH示范
💻 JAVA
字号:
package com.iplan.portal.framework.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;

import javax.servlet.ServletContext;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.iplan.portal.framework.utils.DateUtil;

/**
 * 备份数据库的工具类
 * <p>
 * http://www.hao-se.cn
 * </p>
 * 
 * @author ws
 */
class BackUpDBUtil {
	private static final Logger logger = Logger.getLogger(BackUpDBUtil.class);

	private final static String DEFAULT_DB_PATH = "{user.home}/iplan/db";

	private static ServletContext context = null;

	public BackUpDBUtil() {

	}

	private static boolean copy(InputStream in1, OutputStream out1) {
		try {
			byte[] bytes = new byte[65536];
			int c;
			while ((c = in1.read(bytes)) != -1) {
				out1.write(bytes, 0, c);
			}
			in1.close();
			out1.close();
			return true;
		} catch (Exception e) {
			logger.error("copy()", e);
			return false;
		} finally {
			try {
				in1.close();
				out1.close();
			} catch (Throwable t) {

			}
		}
	}

	private static String getDbPath(String param) {
		String path = context.getInitParameter(param);

		if (StringUtils.isEmpty(path)) {
			path = DEFAULT_DB_PATH;
		}
		if (path.startsWith("{user.home}")) {
			path = path.replaceFirst("\\{user.home\\}", System.getProperty(
					"user.home").replace('\\', '/'));
		}
		if (path.startsWith("{webIPlan.root}")) {
			path = path.replaceFirst("\\{webIPlan.root\\}", context.getRealPath(
					"/").replace('\\', '/'));
		}
		return path;
	}

	public static void backupDB() {
		String dbPath = getDbPath("hsql.dbPath");
		String backupPath = getDbPath("backup.db");
		String dbName = context.getInitParameter("hsql.dbName");

		File dbPathFile = new File(backupPath);
		if (!dbPathFile.exists()) {
			logger.info("Create Path:" + backupPath);
			if (!dbPathFile.mkdirs()) {
				logger.error("Can not create dbBackupPathHsql:" + backupPath);
				return;
			}
		}

		// 源
		if (!dbPath.endsWith("/")) {
			dbPath = dbPath + "/";
		}
		File dbFileScript = new File(dbPath + dbName + ".script");
		File dbFileProperties = new File(dbPath + dbName + ".properties");
		File dbFileLog = new File(dbPath + dbName + ".log");

		String postfix = DateUtil.format(new Date(), "yyyy-MM-dd.HHmmss");
		postfix = ".".concat(postfix);
		// 需要备份的
		if (!backupPath.endsWith("/")) {
			backupPath = backupPath + "/";
		}
		File backupDbFileScript = new File(backupPath + dbName.concat(postfix)
				+ ".script");
		File backupDbFileProperties = new File(backupPath
				+ dbName.concat(postfix) + ".properties");
		File backupDbFileLog = new File(backupPath + dbName.concat(postfix)
				+ ".log");

		try {
			copy(new FileInputStream(dbFileScript), new FileOutputStream(
					backupDbFileScript));
			copy(new FileInputStream(dbFileProperties), new FileOutputStream(
					backupDbFileProperties));
			copy(new FileInputStream(dbFileLog), new FileOutputStream(
					backupDbFileLog));
		} catch (FileNotFoundException e) {
			logger.error("backupDB()", e);
		}
	}

	public static void setServletContext(ServletContext contextTemp) {
		context = contextTemp;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -