博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
增量更新项目时的备份MyBak
阅读量:7033 次
发布时间:2019-06-28

本文共 7808 字,大约阅读时间需要 26 分钟。

在增量更新项目时,做好备份十分重要,这里提供一个方法备份java Web所更新的文件。

把更新包放在指定目录,配好如下webappFolder、updateFolder以及bakeupFolder的路径。

import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.io.PrintStream;import java.text.SimpleDateFormat;import java.util.Date;  public class MyBak{  private static StringBuffer globalTxtMessage = new StringBuffer("");  private static StringBuffer stdoutMessage = new StringBuffer("");  private static int totalFileNumber = 0;  private static int totalSuccessCopyNumber = 0;  private static int totalNewFileNumber = 0;    public static void main(String[] args)  {    String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());      String webappFolder = "/www/htdocs/webapps/M4";      String updateFolder = "/home/whaty/bakeForUpdate/update/" + today;      String backupFolder = "/home/whaty/bakeForUpdate/bake/" + today;      if (webappFolder.equals(backupFolder)) {      stdoutMessage.append("you can't just copy thins to the webapps folder!\n");      globalTxtMessage = new StringBuffer("");      stdoutMessage = new StringBuffer("");    } else {      String msg = backupFiles(updateFolder, webappFolder, backupFolder);      System.out.print(msg);      globalTxtMessage = new StringBuffer("");      stdoutMessage = new StringBuffer("");    }  }    public static String backupFiles(String _updateFolder, String _webappFolder, String _backupFolder)  {    String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());    String now = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date());    try {      File updateFolder = new File(_updateFolder);      File webappFolder = new File(_webappFolder);      File backupFolder = new File(_backupFolder);        globalTxtMessage.append("============" + now + "Backup Log==========\n");      globalTxtMessage.append("============初始化条件测试开始===============\n");      stdoutMessage.append("============init test start===============\n");        if (!(updateFolder.exists())) {        globalTxtMessage.append(_updateFolder + "更新包文件夹找不到!\n");        stdoutMessage.append(" the upload folder \"" + _updateFolder + "\" can Not found! \n please make sure you have upload the folder!!!!\n");      }      if (!(webappFolder.exists())) {        globalTxtMessage.append("项目Webapp文件夹" + _webappFolder + "找不到!\n");        stdoutMessage.append("the Webapp folder " + _webappFolder + " can Not found!!!\n please make sure the folder is exists!!!!\n");        return stdoutMessage.toString();      }      if (!(backupFolder.exists())) {        globalTxtMessage.append("自动创建备份文件夹" + backupFolder.getAbsolutePath() + "\n");        stdoutMessage.append("auto create folder " + backupFolder.getAbsolutePath() + "\n");        backupFolder.mkdirs();      }      globalTxtMessage.append("============初始化条件测试结束===============\n");      stdoutMessage.append("============init  test end===============\n");        Long start = Long.valueOf(System.currentTimeMillis());        copyFilesToDirectory(updateFolder, webappFolder, backupFolder);        Long end = Long.valueOf(System.currentTimeMillis());        Long oprationMillisTime = Long.valueOf(end.longValue() - start.longValue());      Long oprationSecondTime = Long.valueOf((end.longValue() - start.longValue()) / 1000L);        globalTxtMessage.append("备份完成!\n");      globalTxtMessage.append("=========================================\n");      globalTxtMessage.append("总文件数:" + totalFileNumber + "\n");      globalTxtMessage.append("成功复制:" + totalSuccessCopyNumber + "\n");      globalTxtMessage.append("新增文件数:" + totalNewFileNumber + "\n");      globalTxtMessage.append("拷贝文件所需时间[oprationMillisTime]:" + oprationMillisTime + " 毫秒\n");      globalTxtMessage.append("拷贝文件所需时间[oprationSecondTime]:" + oprationSecondTime + " 秒\n");        stdoutMessage.append("backup done!\n");      stdoutMessage.append("total files number:" + totalFileNumber + "\n");      stdoutMessage.append("success copies number:" + totalSuccessCopyNumber + "\n");      stdoutMessage.append("new files number:" + totalNewFileNumber + "\n");      stdoutMessage.append("copy files time[oprationMillisTime]:" + oprationMillisTime + " MillisTime...\n");      stdoutMessage.append("copy files time[oprationSecondTime]:" + oprationSecondTime + " sencends...\n");      stdoutMessage.append("check more info in this file:" + _backupFolder + "/" + today + "_bak" + "/backupLog.txt\n");      File logFile=new File(_backupFolder + "/backupLog.txt");      logFile.createNewFile();      FileWriter fw = new FileWriter(logFile);      BufferedWriter bw = new BufferedWriter(fw);      bw.write(globalTxtMessage.toString());      bw.flush();      bw.close();    }    catch (Exception e)    {      globalTxtMessage.append("============The main function Exception...==============\n");      e.printStackTrace();    }    return stdoutMessage.toString();  }    public static void copyFilesToDirectory(File _updateFolder, File _webappFolder, File _backupFolder) {    String today = new SimpleDateFormat("yyyy-MM-dd").format(new Date());    try {      if (!(_updateFolder.isDirectory())) {        File srcFile = new File(_webappFolder.getAbsolutePath() + _updateFolder.getPath().split(today)[1]);        System.out.println(_webappFolder.getAbsolutePath() + _updateFolder.getPath().split(today)[1]);        totalFileNumber += 1;        if (!(srcFile.exists())) {          totalNewFileNumber += 1;          globalTxtMessage.append("文件:" + srcFile.getPath() + " 在原webapp文件文件夹中不存在,请确认这是个新添加的文件 \n"); return;        }        File destFile = new File(_backupFolder.getAbsolutePath() + _updateFolder.getPath().split(today)[1]);        copyFile(srcFile.getAbsolutePath(), destFile.getAbsolutePath());        totalSuccessCopyNumber += 1; return;      }        File[] files = _updateFolder.listFiles();      for (int i = 0; i < files.length; ++i)      {        copyFilesToDirectory(files[i], _webappFolder, _backupFolder);      }    }    catch (Exception e) {      globalTxtMessage.append("复制文件出错! \n");      e.printStackTrace();    }  }    public static void copyFile(String oldPath, String newPath)  {    int bytesum = 0;    int byteread = 0;    File oldfile = new File(oldPath);    File newfile = new File(newPath);      if (!(newfile.getParentFile().exists())) {      newfile.getParentFile().mkdirs();    }      if (oldfile.exists()) {      InputStream inStream = null;      FileOutputStream fs = null;      try {        inStream = new FileInputStream(oldPath);        fs = new FileOutputStream(newPath);          byte[] buffer = new byte[1444];        while ((byteread = inStream.read(buffer)) != -1) {          bytesum += byteread;          fs.write(buffer, 0, byteread);        }      }catch (FileNotFoundException e) {        globalTxtMessage.append("复制文件:" + oldfile.getName() + " 时找不到文件! \n");        e.printStackTrace();      }catch (IOException e){        globalTxtMessage.append("复制文件:" + oldfile.getName() + " 时出错! \n");        e.printStackTrace();      } finally {        try {          if (inStream != null)            inStream.close();            if (fs != null)            fs.close();        } catch (IOException e) {          e.printStackTrace();        }      }      globalTxtMessage.append("文件复制成功:" + oldfile.getAbsolutePath() + "(size:" + (bytesum / 1024.0D) + "KB)\n");        Long oldFileLastModifiedTime = Long.valueOf(oldfile.lastModified());      newfile.setLastModified(oldFileLastModifiedTime.longValue());    } else {      globalTxtMessage.append("文件:" + oldfile.getAbsolutePath() + " 找不到!\n");    }  }}

运行javac MyBak.java,java MyBak。

这时程序会从旧有项目拷贝一份和更新包相同的文件到bakeupFolder目录。

最后,我们就可以大胆的把更新包在项目中覆盖老文件了。

转载于:https://www.cnblogs.com/windyWu/p/4601607.html

你可能感兴趣的文章
AliOS Things网络适配框架 - SAL
查看>>
iOS 客户端与服务端做时间同步
查看>>
多个请求统一更新界面
查看>>
illuminate/routing 源码分析之注册路由
查看>>
网易公共技术Java研发工程师面经(offer)
查看>>
说说如何在登录页实现生成验证码功能
查看>>
笔记-softmax、softmax loss
查看>>
FastDFS蛋疼的集群和负载均衡(六)之Nginx高可用集群
查看>>
C语言入门经典读书笔记----第十一章 结构化数据
查看>>
Apache Thrift系列详解(二) - 网络服务模型
查看>>
chrome devtools使用详解——Performance
查看>>
了解一下ES6: 解构赋值&字符串
查看>>
7 - 在 Django Admin 后台发布文章
查看>>
SpringBoot+Mybatis+ Druid+PageHelper 实现多数据源并分页
查看>>
Umeng第三方登录
查看>>
EggBorn.js:一款顶级Javascript全栈开发框架
查看>>
前端开始的那件事——表单
查看>>
【前端】HTML属性
查看>>
js 算法3
查看>>
【Java 容器面试题】谈谈你对HashMap 的理解
查看>>