diff src/goodjava/lucene/backup/BackupServer.java @ 1697:aff2309ae510

add copy_backups.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 27 Jun 2022 18:36:56 -0600
parents 973d3039c421
children 2dbcc8360a3e
line wrap: on
line diff
--- a/src/goodjava/lucene/backup/BackupServer.java	Sun Jun 26 14:40:08 2022 -0600
+++ b/src/goodjava/lucene/backup/BackupServer.java	Mon Jun 27 18:36:56 2022 -0600
@@ -92,6 +92,17 @@
 		return name==null ? domain : domain + "~" + name;
 	}
 
+	private Backup getBackup(String name) {
+		synchronized(backups) {
+			Backup backup = backups.get(name);
+			if( backup == null ) {
+				backup = new Backup( new File(backupDir,name) );
+				backups.put(name,backup);
+			}
+			return backup;
+		}
+	}
+
 	private void handle(Socket socket) {
 		RpcServer rpc = new RpcServer(socket);
 		Backup backup = null;
@@ -106,16 +117,14 @@
 			} else if( call.cmd.equals("login") ) {
 				String name = getName(rpc,call.args);
 				if( name==null )  return;
-				synchronized(backups) {
-					backup = backups.get(name);
-					if( backup == null ) {
-						backup = new Backup(new File(backupDir,name));
-						backups.put(name,backup);
-					}
-				}
+				backup = getBackup(name);
 				rpc.write(Rpc.OK);
 			} else if( backup != null ) {
 				backup.handle(rpc,call);
+			} else if( call.cmd.equals("copy") ) {
+				String dirName = (String)call.args[0];
+				copy(new File(dirName));
+				rpc.write(Rpc.OK);
 			} else {
 				rpc.write( new RpcException("login expected") );
 				rpc.close();
@@ -124,6 +133,22 @@
 		}
 	}
 
+	private void copy(File dir) {
+		try {
+			IoUtils.deleteRecursively(dir);
+			IoUtils.mkdirs(dir);
+			for( File f : backupDir.listFiles() ) {
+				if( f.isDirectory() && new File(f,"index.json").exists() ) {
+					String name = f.getName();
+					Backup backup = getBackup(name);
+					backup.copyTo( new File(dir,name) );
+				}
+			}
+		} catch(IOException e) {
+			throw new RuntimeException(e);
+		}
+	}
+
 
 	// for client
 
@@ -138,4 +163,11 @@
 		return new RpcClient(socket);
 	}
 
+	public static void copyBackupTo(String dirName) throws IOException, RpcException {
+		RpcClient rpc = BackupServer.rpcClient("localhost");
+		rpc.write( new RpcCall("copy",dirName) );
+		RpcResult result = rpc.read();
+		rpc.close();
+	}
+
 }