comparison 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
comparison
equal deleted inserted replaced
1696:2958cf04d844 1697:aff2309ae510
90 } 90 }
91 String name = (String)args[1]; 91 String name = (String)args[1];
92 return name==null ? domain : domain + "~" + name; 92 return name==null ? domain : domain + "~" + name;
93 } 93 }
94 94
95 private Backup getBackup(String name) {
96 synchronized(backups) {
97 Backup backup = backups.get(name);
98 if( backup == null ) {
99 backup = new Backup( new File(backupDir,name) );
100 backups.put(name,backup);
101 }
102 return backup;
103 }
104 }
105
95 private void handle(Socket socket) { 106 private void handle(Socket socket) {
96 RpcServer rpc = new RpcServer(socket); 107 RpcServer rpc = new RpcServer(socket);
97 Backup backup = null; 108 Backup backup = null;
98 while( !rpc.isClosed() ) { 109 while( !rpc.isClosed() ) {
99 RpcCall call = rpc.read(); 110 RpcCall call = rpc.read();
104 if( name==null ) return; 115 if( name==null ) return;
105 rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) ); 116 rpc.write( new RpcResult(new Object[]{new File(backupDir,name).exists()}) );
106 } else if( call.cmd.equals("login") ) { 117 } else if( call.cmd.equals("login") ) {
107 String name = getName(rpc,call.args); 118 String name = getName(rpc,call.args);
108 if( name==null ) return; 119 if( name==null ) return;
109 synchronized(backups) { 120 backup = getBackup(name);
110 backup = backups.get(name);
111 if( backup == null ) {
112 backup = new Backup(new File(backupDir,name));
113 backups.put(name,backup);
114 }
115 }
116 rpc.write(Rpc.OK); 121 rpc.write(Rpc.OK);
117 } else if( backup != null ) { 122 } else if( backup != null ) {
118 backup.handle(rpc,call); 123 backup.handle(rpc,call);
124 } else if( call.cmd.equals("copy") ) {
125 String dirName = (String)call.args[0];
126 copy(new File(dirName));
127 rpc.write(Rpc.OK);
119 } else { 128 } else {
120 rpc.write( new RpcException("login expected") ); 129 rpc.write( new RpcException("login expected") );
121 rpc.close(); 130 rpc.close();
122 return; 131 return;
123 } 132 }
133 }
134 }
135
136 private void copy(File dir) {
137 try {
138 IoUtils.deleteRecursively(dir);
139 IoUtils.mkdirs(dir);
140 for( File f : backupDir.listFiles() ) {
141 if( f.isDirectory() && new File(f,"index.json").exists() ) {
142 String name = f.getName();
143 Backup backup = getBackup(name);
144 backup.copyTo( new File(dir,name) );
145 }
146 }
147 } catch(IOException e) {
148 throw new RuntimeException(e);
124 } 149 }
125 } 150 }
126 151
127 152
128 // for client 153 // for client
136 ((SSLSocket)socket).setEnabledCipherSuites(BackupServer.cipherSuites); 161 ((SSLSocket)socket).setEnabledCipherSuites(BackupServer.cipherSuites);
137 } 162 }
138 return new RpcClient(socket); 163 return new RpcClient(socket);
139 } 164 }
140 165
166 public static void copyBackupTo(String dirName) throws IOException, RpcException {
167 RpcClient rpc = BackupServer.rpcClient("localhost");
168 rpc.write( new RpcCall("copy",dirName) );
169 RpcResult result = rpc.read();
170 rpc.close();
171 }
172
141 } 173 }