zoukankan      html  css  js  c++  java
  • java备份Mysql数据库Code

    public class DbUtils{
    
        private static final Logger logger = LoggerFactory.getLogger(DbUtils.class);
    
    /**
             * host
             */
            public static final String host = "127.0.0.1";
    
            /**
             * database
             */
            public static final String database = "test";
    
            /**
             * username
             */
            public static final String username = "root";
    
            /**
             * password
             */
            public static final String password = "root";
    
            /**
             * 备份文件保存位置
             */
            public static final String backPath = "/data/backup/";
    
            /**
             * 备份文件命名规则
             */
            public static final String fileName = database + ".sql";
    
            /**
             * 备份工具绝对路径
             */
            public static final String toolPath = "/usr/local/mysql/bin/";
    
    
        /**
         * 备份操作工具
         */
        public static String dbBackUp() {
    
            // 备份文件名
            String finalFileName = DateUtils.getNowTime(DateUtils.patternTime9, 0) + "-" + fileName;
            //sql备份绝对路径
            String pathSql = backPath + finalFileName;
            StringBuilder backCmd = new StringBuilder();
            backCmd.append(toolPath + "mysqldump");
            backCmd.append(" -h" + host);
            backCmd.append(" -u" + username);
            backCmd.append(" -p" + password);
            backCmd.append(" --databases " + database + " > ");
            backCmd.append(pathSql);
            logger.info("待备份sql文件绝对路径:{}", pathSql);
            logger.info("待执行cmd命令:{}", backCmd.toString());
            logger.info("开始备份数据库:{} >>>>> ", database);
            String[] cmd = {"sh", "-c", backCmd.toString()};
            return exec(cmd);
        }
    
        private static String exec(String[] cmd) {
            try {
                Process process = Runtime.getRuntime().exec(cmd);
                boolean res = process.waitFor(20, TimeUnit.SECONDS);
                if (!res) {
                    logger.info("执行time out");
                    return "time out";
                }
                InputStream inputStream = process.getInputStream();
                byte[] data = new byte[1024];
                StringBuilder result = new StringBuilder();
                while (inputStream.read(data) != -1) {
                    result.append(new String(data, "GBK"));
                }
                if (result.toString().equals("")) {
                    InputStream errorStream = process.getErrorStream();
                    while (errorStream.read(data) != -1) {
                        result.append(new String(data, "GBK"));
                    }
                }else {
                    result.delete(0,result.length()).append("success");
                    logger.info("result:{}",result);
                }
                return result.toString();
            } catch (Exception e) {
                logger.info("error:{}", e.getMessage());
                return "error:" + e.getMessage();
            }
        }
    
        /**
         * 数据恢复操作工具
         */
        public static String dbRestore(String fileName) {
            String filePath = backPath + fileName;
            logger.info("待还原sql文件:{}",filePath);
            StringBuilder restoreCmd = new StringBuilder();
            restoreCmd.append(toolPath + "mysql");
            restoreCmd.append(" -h" + host);
            restoreCmd.append(" -u" + username);
            restoreCmd.append(" -p" + password);
            restoreCmd.append(" < ");
            restoreCmd.append(filePath);
            logger.info("cmd命令为:" + restoreCmd.toString());
            logger.info("开始还原数据");
            String[] cmd = {"sh", "-c", restoreCmd.toString()};
            return exec(cmd);
        }
    
        /**
         * 删除数据备份
         */
        public static String remove(String fileName) {
            String file = DBConstants.backPath + fileName;
            logger.info("待删除sql文件:{}",file);
            File del_file = new File(file);
            if (del_file.isFile()) {
                if (!del_file.delete()) {
                    logger.info("删除失败");
                    return "delete failed";
                }
            }
            logger.info("删除成功");
            return "delete success";
    
        }
    
    }
  • 相关阅读:
    pytest05-参数化
    pytest04-conftest配置文件
    pytest03-fixture
    pytest02-setup和teardown
    SimpleDateFormat 是线程不安全的类,一般不要定义为 static 变量,如果定义为 static ,必须加锁,或者使用 DateUtils 工具类
    线程池不使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式
    线程资源最好通过线程池提供
    获取单例对象需要保证线程安全,其中的方法也要保证线程安全
    高度注意 Map 类集合 K / V 能不能存储 null 值的情况,如下表格
    使用 entrySet 遍历 Map 类集合 KV ,而不是 keySet 方式进行遍历的好处
  • 原文地址:https://www.cnblogs.com/padazala/p/15403949.html
Copyright © 2011-2022 走看看