diff src/goodjava/lucene/backup/BackupIndexWriter.java @ 1674:af18eacf187c

automatic restore
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 29 May 2022 23:38:34 -0600
parents 1b9f9fdb3b41
children 445048f9eca7
line wrap: on
line diff
--- a/src/goodjava/lucene/backup/BackupIndexWriter.java	Sat May 28 21:00:30 2022 -0600
+++ b/src/goodjava/lucene/backup/BackupIndexWriter.java	Sun May 29 23:38:34 2022 -0600
@@ -2,6 +2,9 @@
 
 import java.io.File;
 import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.BufferedOutputStream;
+import java.io.FileOutputStream;
 import java.io.IOException;
 import java.net.Socket;
 import java.net.ConnectException;
@@ -181,4 +184,43 @@
 		}
 	}
 
+	public static BackupIndexWriter newWithRestore(LuceneIndexWriter indexWriter,File logDir,long logTime,String name)
+		throws IOException
+	{
+		if( !logDir.exists() ) {
+			RpcClient rpc = BackupServer.rpcClient(backupDomains[0]);
+			try {
+				RpcCall call;
+				RpcResult result;
+				call = new RpcCall("exists",name);
+				rpc.write(call);
+				result = rpc.read();
+				boolean exists = (Boolean)result.returnValues[0];
+				if( exists ) {
+					logger.error("restoring "+logDir+" from backup");
+					File zip = File.createTempFile("luan_",".zip");
+					IoUtils.delete(zip);
+					call = new RpcCall("login",name);
+					rpc.write(call);
+					rpc.read();
+					call = new RpcCall("zip");
+					rpc.write(call);
+					result = rpc.read();
+					OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
+					IoUtils.copyAll(result.in,out);
+					out.close();
+					IoUtils.mkdirs(logDir);
+					String cmd = "unzip " + zip;
+					Process proc = Runtime.getRuntime().exec(cmd,null,logDir);
+					IoUtils.waitFor(proc);
+					IoUtils.delete(zip);
+				}
+			} catch(RpcException e) {
+				throw new RuntimeException(e);
+			}
+			rpc.close();
+		}
+		return new BackupIndexWriter(indexWriter,logDir,logTime,name);
+	}
+
 }