java linux mysql 备份_Linux java mysql 定时备份和手动备份 (一 )

1 packagecn.goldencis.tsa.common.utils;2

3 importjava.io.BufferedReader;4 importjava.io.File;5 importjava.io.FileOutputStream;6 importjava.io.IOException;7 importjava.io.InputStreamReader;8 importjava.io.OutputStreamWriter;9 importjava.io.PrintWriter;10 importjava.io.UnsupportedEncodingException;11 importjava.text.SimpleDateFormat;12 importjava.util.Date;13

14 importorg.springframework.web.context.ContextLoader;15

16 importcn.goldencis.tsa.common.entity.BackupProperties;17

18 /**

19 * About mysql backup restore.... util class20 * At first considered the system compatibility(windows and linux),21 * however, the time was pressing and later added22 * Note that some of the linux system to complete,23 * and some of the windows system to complete, so note is english24 *@authormll25 * 2017年4月10日上午10:41:4826 *27 */

28 public classBackupUtil {29

30 boolean isWindows =ServerListener.getInstance().checkOsWindows();31

32 private static BackupUtil instance = newBackupUtil();33

34 privateBackupProperties pro;35

36 privateString allFileName;37

38 privateString fileName;39

40 privateString refileName;41

42 @Override43 publicString toString() {44 return "username: "+pro.getUsername()+"\npassword:" +pro.getPassword()+"\ndatabaseName:" +pro.getDatabaseName()45 +"\nfilePath:" +pro.getWindowsFilePath()+"\n backupFileName:" +pro.getBackupFileName()46 +"\nbackupFileMsg:" +pro.getBackupFileMsg()+"\nip:" +pro.getIp()+"\nwinFileMysqlPath: "+pro.getWinFileMysqlPath();47 }48

49 privateBackupUtil(){50

51 }52

53 public staticBackupUtil getInstance(){54 instance.setPro((BackupProperties) ContextLoader.getCurrentWebApplicationContext().getBean("backupProperties"));55 returninstance;56 }57

58 /**

59 * 拼接备份命令60 *@authormll61 *@returnString62 */

63 privateString splitJointBackup(String cfileName){64 String str="mysqldump -h "+ pro.getIp() + " -u " +pro.getUsername() + " -p" + pro.getPassword() +" "+pro.getDatabaseName() ;65 Date d = newDate();66 SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");67 String time=sdf.format(d);68 if(isWindows){69 if(StringUtil.isEmpty(cfileName)){70 refileName = pro.getBackupFileName() + time + ".sql";71 fileName = pro.getWindowsFilePath() +refileName;72

73 }else{74 refileName = cfileName + time + ".sql";75 fileName = pro.getWindowsFilePath() +refileName;76 }77 str = pro.getWinFileMysqlPath() +str;78

79 returnstr;80 }else{81 if(StringUtil.isEmpty(cfileName)){82 refileName = pro.getBackupFileName() + time + ".sql";83 fileName = pro.getLinuxFilePath() +refileName;84 }else{85 refileName = cfileName + time + ".sql";86 fileName = pro.getLinuxFilePath() +refileName;87 }88 returnstr;89 }90 }91

92 /**

93 * 拼接还原命令94 *@authormll95 *@returnString96 */

97 privateString splitJointRestore(String cfileName){98

99 String str="mysql -h "+ pro.getIp() + " -u" +pro.getUsername() + " -p" +pro.getPassword()100 +" "+pro.getDatabaseName() ;101

102 if(isWindows){103 fileName = pro.getWindowsFilePath() +cfileName;104 str = pro.getWinFileMysqlPath() +str;105

106 returnstr;107 }else{108 fileName = pro.getLinuxFilePath() +cfileName;109 returnstr;110 }111 }112

113 /**

114 * FLag is true auto delete115 *@paramcfileName116 *@paramisAuto117 *@return

118 */

119 privateString splitJoinDeleteBackup(String cfileName){120 String str;121 String arrStr[]=cfileName.split(",");122

123 StringBuilder sb;124

125 if(isWindows){126 sb = newStringBuilder();127 for(String temp:arrStr){128 sb.append(pro.getWindowsFilePath() + temp + " ");129 }130 str = "del " +sb.toString();131 }else{132 sb = newStringBuilder();133 for(String temp:arrStr){134 sb.append(pro.getLinuxFilePath() + temp + " ");135 }136 str = "rm " + sb.toString() + " -rf ";137 }138 returnstr;139 }140

141 /**

142 * 删除备份143 *@paramcfileName144 *@paramisAuto145 */

146 public voiddeleteBackup(String cfileName){147 String cmd =splitJoinDeleteBackup(cfileName);148 System.out.println(cmd);149 try{150 Runtime.getRuntime().exec(cmd);151

152 } catch(IOException e) {153 e.printStackTrace();154 }155 }156

157

158 /**

159 * 备份160 *@authormll161 *@paramcfileName162 *@returnString163 */

164 publicString backup(String cfileName) {165 String cmd =splitJointBackup(cfileName);166 PrintWriter p = null;167 BufferedReader reader = null;168 File filePath = newFile(pro.getLinuxFilePath());169 FileOutputStream fileout = null;170 try{171 if(!filePath.exists()){172 filePath.mkdirs();173 }174 fileout = newFileOutputStream(fileName);175 p = new PrintWriter(new OutputStreamWriter(fileout, "utf8"));176 Process process =Runtime.getRuntime().exec(cmd);177 InputStreamReader inputStreamReader = newInputStreamReader(process178 .getInputStream(), "utf8");179 reader = newBufferedReader(inputStreamReader);180 String line = null;181 while ((line = reader.readLine()) != null) {182 p.println(line);183 }184 p.flush();185 } catch(UnsupportedEncodingException e) {186 e.printStackTrace();187 } catch(IOException e) {188 e.printStackTrace();189 } finally{190 try{191 if (reader != null) {192 reader.close();193 }194 if (p != null) {195 p.close();196 }197 if (fileout != null) {198 fileout.close();199 }200 } catch(IOException e) {201 e.printStackTrace();202 }203 }204 returnrefileName;205 }206

207 /**

208 * 还原209 *@authormll210 *@paramcfileName void211 */

212 public voidrestore(String cfileName) {213 String cmd =splitJointRestore(cfileName);214 try{215 System.out.println(cmd + "

217

218 Process process = Runtime.getRuntime().exec("mysql -u root -proot -h 192.168.3.89 goldencis_tsa

220 //输出执行结果

221 InputStreamReader in = newInputStreamReader(process.getInputStream());222 BufferedReader br = newBufferedReader(in);223 String line;224 while((line = br.readLine()) != null){225 System.out.println(line);226 }227 br.close();228 in.close();229

230 //输出错误信息

231 InputStreamReader in2 = newInputStreamReader(process.getErrorStream());232 BufferedReader br2 = newBufferedReader(in2);233 String line2 ;234 while((line2 = br2.readLine()) != null){235 System.out.println("="+line2);236 }237 br2.close();238 in2.close();239 } catch(UnsupportedEncodingException e) {240 e.printStackTrace();241 } catch(IOException e) {242 e.printStackTrace();243 }244 }245

246 publicString getAllFileName() {247 returnallFileName;248 }249

250 public voidsetAllFileName(String allFileName) {251 this.allFileName =allFileName;252 }253

254

255 publicBackupProperties getPro() {256 returnpro;257 }258

259 public voidsetPro(BackupProperties pro) {260 this.pro =pro;261 }262

263 publicString getFileName() {264 returnfileName;265 }266

267 publicString getRefileName() {268 returnrefileName;269 }270

271 public voidsetRefileName(String refileName) {272 this.refileName =refileName;273 }274 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值