annotate src/goodjava/lucene/backup/Backup.java @ 1693:45eaaf5146f3

add backup autostart
author Vadim Filimonov <fffilimonov@yandex.ru>
date Mon, 20 Jun 2022 10:29:43 +0300
parents ea7075b7afe1
children aff2309ae510
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
1 package goodjava.lucene.backup;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
2
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
3 import java.io.File;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
4 import java.io.InputStream;
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
5 import java.io.FileInputStream;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
6 import java.io.IOException;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
7 import java.util.List;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
8 import java.util.ArrayList;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
9 import java.util.Map;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
10 import java.util.Arrays;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
11 import goodjava.io.IoUtils;
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
12 import goodjava.io.BufferedInputStream;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
13 import goodjava.rpc.RpcServer;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
14 import goodjava.rpc.RpcCall;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
15 import goodjava.rpc.RpcResult;
1512
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
16 import goodjava.rpc.RpcException;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
17 import goodjava.logging.Logger;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
18 import goodjava.logging.LoggerFactory;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
19 import goodjava.lucene.logging.LogFile;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
20 import goodjava.lucene.logging.LoggingIndexWriter;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
21 import goodjava.lucene.logging.LogOutputStream;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
22
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
23
1672
8dd8c556c449 backup work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1548
diff changeset
24 final class Backup {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
25 private static final Logger logger = LoggerFactory.getLogger(Backup.class);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
26
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
27 private final File dir;
1677
ea7075b7afe1 switch to index.json
Franklin Schmidt <fschmidt@gmail.com>
parents: 1674
diff changeset
28 private final File indexFile;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
29
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
30 Backup(File dir) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
31 this.dir = dir;
1677
ea7075b7afe1 switch to index.json
Franklin Schmidt <fschmidt@gmail.com>
parents: 1674
diff changeset
32 this.indexFile = new File(dir,"index.json");
1673
1b9f9fdb3b41 remove backup password
Franklin Schmidt <fschmidt@gmail.com>
parents: 1672
diff changeset
33 dir.setLastModified(System.currentTimeMillis());
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
34 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
35
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
36 void handle(RpcServer rpc,RpcCall call) {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
37 try {
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
38 IoUtils.mkdirs(dir);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
39 if( call.cmd.equals("zip") ) {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
40 handleZip(rpc);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
41 } else {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
42 handle2(rpc,call);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
43 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
44 } catch(IOException e) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
45 throw new RuntimeException(e);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
46 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
47 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
48
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
49 private static final RpcResult OK = new RpcResult(new Object[]{"ok"});
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
50
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
51 synchronized void handle2(RpcServer rpc,RpcCall call) throws IOException {
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
52 //logger.info(call.cmd+" "+Arrays.asList(call.args));
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
53 String fileName = null;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
54 if( call.cmd.equals("check") ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
55 // nothing
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
56 } else if( call.cmd.equals("add") || call.cmd.equals("append") ) {
1512
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
57 fileName = (String)call.args[1];
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
58 File f = new File(dir,fileName);
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
59 if( call.cmd.equals("add") )
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
60 IoUtils.delete(f);
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
61 LogFile log = new LogFile(f);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
62 LogOutputStream out = log.output();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
63 IoUtils.copyAll(call.in,out);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
64 out.commit();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
65 out.close();
1512
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
66 //logger.info(call.cmd+" "+fileName+" "+call.lenIn);
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
67 } else {
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
68 logger.error("bad cmd '"+call.cmd+"'");
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
69 rpc.write( new RpcException("bad cmd '"+call.cmd+"'") );
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
70 return;
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
71 }
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
72 List logInfo = (List)call.args[0];
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
73 //logger.info("check "+logInfo);
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
74 RpcResult result = OK;
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
75 for( Object obj : logInfo ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
76 Map fileInfo = (Map)obj;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
77 String name = (String)fileInfo.get("name");
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
78 File f = new File(dir,name);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
79 if( !f.exists() ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
80 if( name.equals(fileName) ) logger.error("missing");
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
81 result = new RpcResult(new Object[]{"missing",name});
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
82 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
83 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
84 long end = (Long)fileInfo.get("end");
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
85 LogFile log = new LogFile(f);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
86 long logEnd = log.end();
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
87 if( logEnd > end ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
88 logger.error("logEnd > end - shouldn't happen, file="+name+" logEnd="+logEnd+" end="+end);
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
89 result = new RpcResult(new Object[]{"missing",name});
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
90 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
91 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
92 if( logEnd < end ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
93 if( name.equals(fileName) ) logger.error("incomplete");
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
94 result = new RpcResult(new Object[]{"incomplete",name,logEnd});
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
95 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
96 }
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
97 Object checksumObj = fileInfo.get("checksum");
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
98 if( checksumObj != null ) {
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
99 long checksum = (Long)checksumObj;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
100 if( log.checksum() != checksum ) {
1677
ea7075b7afe1 switch to index.json
Franklin Schmidt <fschmidt@gmail.com>
parents: 1674
diff changeset
101 indexFile.delete();
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
102 result = new RpcResult(new Object[]{"bad_checksum",name});
1508
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
103 break;
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
104 }
86c5e7000ecf lucene.backup checksum
Franklin Schmidt <fschmidt@gmail.com>
parents: 1501
diff changeset
105 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
106 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
107 if( call.cmd.equals("add") ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
108 boolean complete = true;
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1512
diff changeset
109 final LogFile[] logs = new LogFile[logInfo.size()];
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1512
diff changeset
110 for( int i=0; i<logs.length; i++ ) {
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1512
diff changeset
111 Map fileInfo = (Map)logInfo.get(i);
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
112 String name = (String)fileInfo.get("name");
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
113 File f = new File(dir,name);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
114 if( !f.exists() ) {
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
115 complete = false;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
116 break;
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
117 }
1548
736ec76bbf42 lucene log work
Franklin Schmidt <fschmidt@gmail.com>
parents: 1512
diff changeset
118 logs[i] = new LogFile(f);
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
119 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
120 if( complete ) {
1677
ea7075b7afe1 switch to index.json
Franklin Schmidt <fschmidt@gmail.com>
parents: 1674
diff changeset
121 LoggingIndexWriter.writeIndex(logs,indexFile);
1512
31b543826ca9 lucene.backup login
Franklin Schmidt <fschmidt@gmail.com>
parents: 1509
diff changeset
122 //logger.info("write index");
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
123 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
124 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
125 rpc.write(result);
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
126 }
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
127
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
128 void handleZip(RpcServer rpc) throws IOException {
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
129 File zip = File.createTempFile("luan_",".zip");
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
130 IoUtils.delete(zip);
1674
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
131 String cmd = "zip " + zip + " *";
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
132 synchronized(this) {
1674
af18eacf187c automatic restore
Franklin Schmidt <fschmidt@gmail.com>
parents: 1673
diff changeset
133 Process proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir);
1509
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
134 IoUtils.waitFor(proc);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
135 }
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
136 InputStream in = new BufferedInputStream(new FileInputStream(zip));
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
137 RpcResult result = new RpcResult(in,zip.length(),new Object[0]);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
138 rpc.write(result);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
139 IoUtils.delete(zip);
0ba144491a42 lucene.backup zip
Franklin Schmidt <fschmidt@gmail.com>
parents: 1508
diff changeset
140 }
1499
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
141
22e15cf73040 lucene.backup
Franklin Schmidt <fschmidt@gmail.com>
parents:
diff changeset
142 }