数据库备份,github 有项目

1.// 数据库备份(1-4)

参数:主机ip,mysql数据库账户信息,同步出sql文件保存路径。文件名,数据库名

public static boolean exportDatabaseTool(String hostIP, String userName, String password, String savePath,

String fileName, String databaseName) throws InterruptedException {

File saveFile = new File(savePath);

if (!saveFile.exists()) {// 如果目录不存在

saveFile.mkdirs();// 创建文件夹

}

if (!savePath.endsWith(File.separator)) {

savePath = savePath + File.separator;

}

PrintWriter printWriter = null;

BufferedReader bufferedReader = null;

try {

printWriter = new PrintWriter(new OutputStreamWriter(new FileOutputStream(savePath + fileName), "utf8"));

StringBuilder stringBuilder = new StringBuilder();

stringBuilder.append("mysqldump").append(" --opt").append(" -h").append(hostIP);

stringBuilder.append(" --user=").append(userName).append(" --password=").append(password)

.append(" --lock-all-tables=true");

stringBuilder.append(" --result-file=").append(savePath + fileName).append(" --default-character-set=utf8 ")

.append(databaseName);

Process process = Runtime.getRuntime().exec(stringBuilder.toString());

InputStreamReader inputStreamReader = new InputStreamReader(process.getInputStream(), "utf8");

bufferedReader = new BufferedReader(inputStreamReader);

String line;

while ((line = bufferedReader.readLine()) != null) {

printWriter.println(line);

}

printWriter.flush();

if (process.waitFor() == 0) {// 0 表示线程正常终止。

return true;

}

} catch (IOException e) {

e.printStackTrace();

} finally {

try {

if (bufferedReader != null) {

bufferedReader.close();

}

if (printWriter != null) {

printWriter.close();

}

} catch (IOException e) {

e.printStackTrace();

}

}

return false;

}

2.// 数据库恢复

参数:sql文件存放位置

public static void recover(String path) throws IOException {

Runtime runtime = Runtime.getRuntime();

// 恢复 到数据库的账户信息

Process process = runtime.exec("mysql -h 192.168.1.165 -u root -proot --default-character-set=utf8 aaaaaa");

OutputStream outputStream = process.getOutputStream();

BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

String str = null;

StringBuffer sb = new StringBuffer();

while ((str = br.readLine()) != null) {

sb.append(str + "\r\n");

}

str = sb.toString();

System.out.println(str);

OutputStreamWriter writer = new OutputStreamWriter(outputStream, "utf-8");

writer.write(str);

writer.flush();

outputStream.close();

br.close();

writer.close();

}

3.// 数据库同步操作

public void databaseBackup() throws IOException {

try {

// 备份

exportDatabaseTool("127.0.0.1", "root", "root", "D:/backupDatabase", "qinmei.sql", "qinmei");

// 恢复

recover("D:/backupDatabase/qinmei.sql");

} catch (InterruptedException e) {

e.printStackTrace();

}

}

4.// 定时操作(多久备份一次)

public void run(){

Timer timer = new Timer(true);

timer.schedule(

new java.util.TimerTask() {

public void run(){

try {

databaseBackup();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

}, 0, 1 * 60 * 1000);

}


5.// 监听器,项目启动时执行自动备份

public class TaskManager implements ServletContextListener {

/**

* 每天的毫秒数

*/

public static final long PERIOD_DAY = 10;

/**

* 一周内的毫秒数

*/

public static final long PERIOD_WEEK = PERIOD_DAY * 7;

/**

* 无延迟

*/

public static final long NO_DELAY = 0;

/**

* 定时器

*/

private Timer timer;

MySQLDatabaseController databaseController = new MySQLDatabaseController();

/**

* 在Web应用启动时初始化任务

*/

public void contextInitialized(ServletContextEvent event) {

Calendar calendar = Calendar.getInstance();

calendar.set(Calendar.HOUR_OF_DAY, 17); // 控制时

calendar.set(Calendar.MINUTE, 10); // 控制分

calendar.set(Calendar.SECOND, 0); // 控制秒

Date date = calendar.getTime();

// 定义定时器

timer = new Timer("数据库表备份", true);

// 启动备份任务,参数一是某个定时类,包含run 方法,自动执行 run 方法

timer.schedule(databaseController, date);

}

/**

* 在Web应用结束时停止任务

*/

public void contextDestroyed(ServletContextEvent event) {

timer.cancel(); // 定时器销毁

}

}

6.// web.xml 配置监听器

com.qinmei.controller.TaskManager


7.// github保存完整项目

mysql

你可能感兴趣的:(数据库备份,github 有项目)