| | |
| | | import java.net.ServerSocket;
|
| | | import java.net.Socket;
|
| | | import java.net.UnknownHostException;
|
| | | import java.text.SimpleDateFormat;
|
| | | import java.util.Arrays;
|
| | | import java.util.Comparator;
|
| | | import java.util.Date;
|
| | | import java.util.zip.ZipEntry;
|
| | | import java.util.zip.ZipInputStream;
|
| | | import java.util.zip.ZipOutputStream;
|
| | |
|
| | |
|
| | | public class HzipServer extends Thread {
|
| | | public static final String MYSQLBACKUPDIR = "MYSQLBACKUPDIR"; //mysql数据库备份文件夹名称
|
| | | public static final String MYSQLBACKUPDIR = "MySqlBackUpDir"; //mysql数据库备份文件夹名称
|
| | | public static final int MAXSTORAGE = 3;
|
| | | public static boolean ISNEEDDELETE = false;
|
| | | public Socket socket;
|
| | |
| | | InputStream ins = socket.getInputStream();
|
| | | ZipInputStream zis=new ZipInputStream(ins);
|
| | |
|
| | | String rootDir=null;
|
| | | String rootDir="REGULAR_BACKUP";
|
| | | //在此次连接中,是否是第一次读到ZipEntry.读到的第一个Entry,就是正在被传输的文件夹。
|
| | | boolean isFirst=true;
|
| | | String savePath="D:"+File.separator+MYSQLBACKUPDIR+File.separator;
|
| | |
| | | while( (ze=zis.getNextEntry())!=null )
|
| | | {
|
| | | String name=ze.getName();
|
| | | System.out.println(name);
|
| | | File file=null;
|
| | | if(ze.isDirectory())
|
| | | {
|
| | |
| | | }
|
| | | else
|
| | | {
|
| | | int index=name.indexOf(rootDir);
|
| | | int index;
|
| | | if(isFirst) {
|
| | | index = name.lastIndexOf("\\");
|
| | | }else { |
| | | index=name.indexOf(rootDir);
|
| | | }
|
| | | String tempFileDir=name.substring(index,name.length());
|
| | | file=new File(savePath+tempFileDir);
|
| | | fos=new FileOutputStream(file);
|
| | |
| | | }
|
| | | }
|
| | | socket.close();
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | System.out.println("Backup MySql OK at " + sdf.format(new Date()));
|
| | | ISNEEDDELETE = true;
|
| | | } catch (UnknownHostException e) {
|
| | | e.printStackTrace();
|
| | |
| | | String savePath="D:"+File.separator+MYSQLBACKUPDIR+File.separator;
|
| | | File fileroot = new File(savePath);
|
| | | File[] files = fileroot.listFiles();
|
| | | if(ISNEEDDELETE && files.length >3) {
|
| | | if(ISNEEDDELETE && fileroot.exists() && files.length >3) {
|
| | | deleteFileRoot(savePath, MAXSTORAGE); //删除超过3次的备份数据
|
| | | ISNEEDDELETE = false;
|
| | | }
|
| | |
| | | if(file.exists()) {
|
| | | File[] files = file.listFiles();
|
| | | Arrays.sort(files, new CompratorByLastModified());
|
| | | if(files.length>=filecount && filecount>0) {
|
| | | if(files.length>filecount && filecount>0) {
|
| | | //System.out.println("文件夹个数:"+files.length + "\t 最大笔数:"+filecount);
|
| | | for(int i=(filecount-1);i<files.length;i++) {
|
| | | for(int i=filecount;i<files.length;i++) {
|
| | | deleteDir(files[i]);
|
| | | }
|
| | | }
|
| | |
| | | DeleteRegularThread deletethread = server.createDeleteRegularThreadStructure();
|
| | | deletethread.start(); //启动删除多余备份数据库线程
|
| | |
|
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
| | | |
| | | System.out.println("MySql BackUpThread Start at "+sdf.format(new Date()));
|
| | | while(true) {
|
| | | Socket s = serveracept.accept();
|
| | | //System.out.println("监听到客户端连接");
|
| | | server = new HzipServer(s);
|
| | | server.start();
|
| | | try {
|
| | | Socket s = serveracept.accept();
|
| | | //System.out.println("监听到客户端连接");
|
| | | server = new HzipServer(s);
|
| | | server.start();
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | }
|
| | |
|
| | |
|