changeset 1697:aff2309ae510

add copy_backups.luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 27 Jun 2022 18:36:56 -0600
parents 2958cf04d844
children 2dbcc8360a3e
files .hgignore backup/copy_backups.luan src/goodjava/lucene/backup/Backup.java src/goodjava/lucene/backup/BackupServer.java
diffstat 4 files changed, 66 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/.hgignore	Sun Jun 26 14:40:08 2022 -0600
+++ b/.hgignore	Mon Jun 27 18:36:56 2022 -0600
@@ -16,3 +16,4 @@
 push.sh
 backup/logs/
 backup/backups/
+backup/backups.copy/
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/backup/copy_backups.luan	Mon Jun 27 18:36:56 2022 -0600
@@ -0,0 +1,4 @@
+require "java"
+local BackupServer = require "java:goodjava.lucene.backup.BackupServer"
+
+BackupServer.copyBackupTo("backups.copy")
--- a/src/goodjava/lucene/backup/Backup.java	Sun Jun 26 14:40:08 2022 -0600
+++ b/src/goodjava/lucene/backup/Backup.java	Mon Jun 27 18:36:56 2022 -0600
@@ -26,14 +26,20 @@
 
 	private final File dir;
 	private final File indexFile;
+	private boolean updated = false;
 
 	Backup(File dir) {
 		this.dir = dir;
 		this.indexFile = new File(dir,"index.json");
-		dir.setLastModified(System.currentTimeMillis());
 	}
 
 	void handle(RpcServer rpc,RpcCall call) {
+		synchronized(this) {
+			if( !updated ) {
+				dir.setLastModified(System.currentTimeMillis());
+				updated = true;
+			}
+		}
 		try {
 			IoUtils.mkdirs(dir);
 			if( call.cmd.equals("zip") ) {
@@ -48,7 +54,7 @@
 
 	private static final RpcResult OK = new RpcResult(new Object[]{"ok"});
 
-	synchronized void handle2(RpcServer rpc,RpcCall call) throws IOException {
+	private synchronized void handle2(RpcServer rpc,RpcCall call) throws IOException {
 		//logger.info(call.cmd+" "+Arrays.asList(call.args));
 		String fileName = null;
 		if( call.cmd.equals("check") ) {
@@ -125,7 +131,7 @@
 		rpc.write(result);
 	}
 
-	void handleZip(RpcServer rpc) throws IOException {
+	private void handleZip(RpcServer rpc) throws IOException {
 		File zip = File.createTempFile("luan_",".zip");
 		IoUtils.delete(zip);
 		String cmd = "zip " + zip + " *";
@@ -139,4 +145,17 @@
 		IoUtils.delete(zip);
 	}
 
+	synchronized void copyTo(File dirTo) throws IOException {
+		IoUtils.mkdirs(dirTo);
+		for( File f : dir.listFiles() ) {
+			String name = f.getName();
+			File to = new File(dirTo,name);
+			if( name.equals("index.json") ) {
+				IoUtils.copy(f,to);
+			} else {
+				IoUtils.link(f,to);
+			}
+		}
+	}
+
 }
--- 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();
+	}
+
 }