changeset 1674:af18eacf187c

automatic restore
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 29 May 2022 23:38:34 -0600
parents 1b9f9fdb3b41
children 445048f9eca7
files src/goodjava/lucene/backup/Backup.java src/goodjava/lucene/backup/BackupIndexWriter.java src/goodjava/lucene/backup/BackupServer.java src/luan/modules/lucene/LuceneIndex.java
diffstat 4 files changed, 65 insertions(+), 40 deletions(-) [+]
line wrap: on
line diff
--- a/src/goodjava/lucene/backup/Backup.java	Sat May 28 21:00:30 2022 -0600
+++ b/src/goodjava/lucene/backup/Backup.java	Sun May 29 23:38:34 2022 -0600
@@ -128,9 +128,9 @@
 	void handleZip(RpcServer rpc) throws IOException {
 		File zip = File.createTempFile("luan_",".zip");
 		IoUtils.delete(zip);
-		String cmd = "zip -r " + zip + " " + dir.getName();
+		String cmd = "zip " + zip + " *";
 		synchronized(this) {
-			Process proc = Runtime.getRuntime().exec(cmd,null,dir.getParentFile());
+			Process proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir);
 			IoUtils.waitFor(proc);
 		}
 		InputStream in = new BufferedInputStream(new FileInputStream(zip));
--- 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);
+	}
+
 }
--- a/src/goodjava/lucene/backup/BackupServer.java	Sat May 28 21:00:30 2022 -0600
+++ b/src/goodjava/lucene/backup/BackupServer.java	Sun May 29 23:38:34 2022 -0600
@@ -1,13 +1,6 @@
 package goodjava.lucene.backup;
 
 import java.io.File;
-import java.io.OutputStream;
-import java.io.FileOutputStream;
-import java.io.BufferedOutputStream;
-import java.io.Writer;
-import java.io.FileWriter;
-import java.io.Reader;
-import java.io.FileReader;
 import java.io.IOException;
 import java.util.Map;
 import java.net.Socket;
@@ -80,29 +73,31 @@
 
 	private void handle(Socket socket) {
 		RpcServer rpc = new RpcServer(socket);
-		Backup backup;
-		{
+		Backup backup = null;
+		while( !rpc.isClosed() ) {
 			RpcCall call = rpc.read();
-			if( !call.cmd.equals("login") ) {
+			if( call == null )
+				break;
+			if( call.cmd.equals("exists") ) {
+				String name = (String)call.args[0];
+				rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) );
+			} else if( call.cmd.equals("login") ) {
+				String name = (String)call.args[0];
+				synchronized(backups) {
+					backup = backups.get(name);
+					if( backup == null ) {
+						backup = new Backup(new File(backupDir,name));
+						backups.put(name,backup);
+					}
+				}
+				rpc.write(Rpc.OK);
+			} else if( backup != null ) {
+				backup.handle(rpc,call);
+			} else {
 				rpc.write( new RpcException("login expected") );
 				rpc.close();
 				return;
 			}
-			String name = (String)call.args[0];
-			synchronized(backups) {
-				backup = backups.get(name);
-				if( backup == null ) {
-					backup = new Backup(new File(backupDir,name));
-					backups.put(name,backup);
-				}
-			}
-			rpc.write(Rpc.OK);
-		}
-		while( !rpc.isClosed() ) {
-			RpcCall call = rpc.read();
-			if( call == null )
-				break;
-			backup.handle(rpc,call);
 		}
 	}
 
@@ -120,15 +115,4 @@
 		return new RpcClient(socket);
 	}
 
-	public static void getBackup(String backupDomain,String name,File zip) throws IOException, RpcException {
-		RpcClient rpc = BackupServer.rpcClient(backupDomain);
-		RpcCall call = new RpcCall("zip",name);
-		rpc.write(call);
-		RpcResult result = rpc.read();
-		OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
-		IoUtils.copyAll(result.in,out);
-		out.close();
-		rpc.close();
-	}
-
 }
--- a/src/luan/modules/lucene/LuceneIndex.java	Sat May 28 21:00:30 2022 -0600
+++ b/src/luan/modules/lucene/LuceneIndex.java	Sun May 29 23:38:34 2022 -0600
@@ -212,8 +212,7 @@
 				String name = this.domain;
 				if( this.name != null )
 					name += "~" + this.name;
-				writer = new BackupIndexWriter((LuceneIndexWriter)writer,logDir,logTime,name);
-//qqq
+				writer = BackupIndexWriter.newWithRestore((LuceneIndexWriter)writer,logDir,logTime,name);
 			}
 		}
 		reader = DirectoryReader.open(fsDir);