fltp备份文件后统计验证( 二 )


1、存储根目录/模块/直接存储文件
2、存储根目录/模块/年月/日/存储文件
3、存储根目录/模块/年月日/存储文件
4、存储根目录/年月/日/存储文件
5、存储根目录/年月/日/模块/存储文件
推荐使用方式2或方式5,而且所有存储格式应该统一,这样便于备份脚本只写一种遍历即可 。我这边目前因为采用了多种存储方式 , 导致编写备份脚本时要分开写多个(参考上一篇文章) 。
一个备份和统计的实例如下:
if [ -d 存储根目录/模块/年月/日 ]; thenecho "目录存在"ls 存储根目录/模块/年月/日/存储文件 -l|grep "^-"| wc -l >> /本地脚本目录/bak_logs/年月/日/模块.bak.countlftp -u 账号,密码 ftp服务器ip << EOFmirror --reverse --only-missing --only-newer 存储根目录/模块/年月/日 --parallel=3 --log=/本地脚本目录/logs/年月/日/模块_小时.logls 存储根目录/模块/年月/日/存储文件 |grep "^-"| wc -l >> /本地脚本目录/bak_logs/年月/日/模块.bak.countbyeEOFelseecho "不存在"fi有了统计个数,就可以通过代码来读取并推送邮件给管理员了 。

fltp备份文件后统计验证

文章插图
fltp备份文件后统计验证

文章插图
package test;import java.io.BufferedReader;import java.io.File;import java.io.FileReader;import java.io.IOException;import java.sql.SQLException;import java.text.SimpleDateFormat;import java.util.ArrayList;import java.util.Calendar;import java.util.List;import java.util.Properties;public class BakFile {private Properties props;private WarningEmail warningEmail;public BakFile(Properties _props) {props = _props;}public void run() {try {// 初始化this.InitSetup();// 获取统计信息this.readResultCount();} catch (Exception exIO) {warningEmail.send_report_mail("核验备份文件出错啦!!!", exIO.toString());exIO.printStackTrace();}}private void InitSetup() throws IOException, SQLException {// 提醒邮件warningEmail = new WarningEmail(props);}public void readResultCount() {// 获取昨天的日期,Calendar cal = Calendar.getInstance();cal.add(Calendar.DATE,-1);//昨天String yesterday_ym = new SimpleDateFormat("yyyy-MM").format(cal.getTime());String yesterday_d = new SimpleDateFormat("dd").format(cal.getTime());String yesterday = yesterday_ym+"-"+yesterday_d;String path = "/var/jenkins_home/bak_logs/" + yesterday_ym + "/" + yesterday_d;File dirFile = new File(path);//如果dir对应的文件不存在 , 或者不是一个目录 , 则退出if (!dirFile.isDirectory()) {warningEmail.send_report_mail(yesterday+"核验备份文件异常", "未获取到昨天备份目录");return;}//获取文件夹下所有文件File[] files = dirFile.listFiles();File file;List<String> list;String content = "";String bodyTrContent = "";if(files!=null && files.length>0) {for (int i = 0; i < files.length; i++) {file = files[i];if(file.getName().endsWith("count")) {list = readFileContent(file);content = setContent(file, list);bodyTrContent = String.format("%s%s", bodyTrContent, content);}}content = setMailHtml(bodyTrContent, yesterday);warningEmail.send_report_mail(yesterday+"文件备份情况", content);} else {warningEmail.send_report_mail(yesterday+"文件备份为空", "未获取到昨天备份目录");}}public List<String> readFileContent(File file) {BufferedReader reader = null;FileReader fileReader = null;List<String> list = new ArrayList<String>();try {fileReader = new FileReader(file);reader = new BufferedReader(fileReader);String tempString = null;// 一次读入一行,直到读入null为文件结束while ((tempString = reader.readLine()) != null) {list.add(tempString);//System.out.println(tempString);}} catch (IOException e) {} finally {if (reader != null) {try {reader.close();} catch (IOException e1) {}}if(fileReader != null) {try {fileReader.close();} catch (IOException e1) {}}}return list;}public String setContent(File file, List<String> list) {if(list.size()>=2) {String line1 = list.get(list.size()-2);String line2 = list.get(list.size()-1);return setTableBodyTr(file.getName().replace(".bak.count", ""), line1, line2);} else if(list.size()==1){ //String line1 = list.get(list.size()-1);return setTableBodyTr(file.getName().replace(".bak.count", ""), line1, "未同步");} else {return setTableBodyTr(file.getName().replace(".bak.count", ""), "未获取到文件夹", "未同步");}}public String setMailHtml(String content, String yesterday) {String html = String.format("<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01//EN' 'http://www.w3.org/TR/html4/strict.dtd'>" +" <html lang='en'> " +"<head>" +"<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>" +"</head> " +"<body>"+ "<div style='font-size:12px;'>%s文件备份情况:<br/><br/>"+ "<table width='584' style='border-collapse: collapse;font-size:9pt;width:438pt'><tr style='background-color:#f7f7f7;'>"+ "<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px;width:38pt;'>目录</td>"+ "<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px;width:60pt;'>本地</td>"+ "<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px;width:60pt;'>ftp</td>"+ "</tr>", yesterday);html = String.format("%s%s", html, content);html = String.format("%s</table></body></html>", html);return html;}public String setTableBodyTr(String mName, String local, String ftp) {String content = "";if(!local.equals(ftp)) {ftp = String.format("<span style='color:red;' color='red'>%s</span>", ftp);}content = String.format("%s<tr><td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px 5px 0 5px;width:38pt;'>%s</td>", content, mName);content = String.format("%s<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px 5px 0 5px;width:60pt;'>%s</td>", content, local);content = String.format("%s<td style='border:solid 1px #ccc;text-align:center;vertical-align:top;padding:5px 5px 0 5px;width:60pt;'>%s</td>", content, ftp);content = String.format("%s</tr>", content);return content;}}

推荐阅读