Mercurial Hosting > luan
comparison src/goodjava/lucene/backup/Backup.java @ 1697:aff2309ae510
add copy_backups.luan
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 27 Jun 2022 18:36:56 -0600 |
parents | ea7075b7afe1 |
children |
comparison
equal
deleted
inserted
replaced
1696:2958cf04d844 | 1697:aff2309ae510 |
---|---|
24 final class Backup { | 24 final class Backup { |
25 private static final Logger logger = LoggerFactory.getLogger(Backup.class); | 25 private static final Logger logger = LoggerFactory.getLogger(Backup.class); |
26 | 26 |
27 private final File dir; | 27 private final File dir; |
28 private final File indexFile; | 28 private final File indexFile; |
29 private boolean updated = false; | |
29 | 30 |
30 Backup(File dir) { | 31 Backup(File dir) { |
31 this.dir = dir; | 32 this.dir = dir; |
32 this.indexFile = new File(dir,"index.json"); | 33 this.indexFile = new File(dir,"index.json"); |
33 dir.setLastModified(System.currentTimeMillis()); | |
34 } | 34 } |
35 | 35 |
36 void handle(RpcServer rpc,RpcCall call) { | 36 void handle(RpcServer rpc,RpcCall call) { |
37 synchronized(this) { | |
38 if( !updated ) { | |
39 dir.setLastModified(System.currentTimeMillis()); | |
40 updated = true; | |
41 } | |
42 } | |
37 try { | 43 try { |
38 IoUtils.mkdirs(dir); | 44 IoUtils.mkdirs(dir); |
39 if( call.cmd.equals("zip") ) { | 45 if( call.cmd.equals("zip") ) { |
40 handleZip(rpc); | 46 handleZip(rpc); |
41 } else { | 47 } else { |
46 } | 52 } |
47 } | 53 } |
48 | 54 |
49 private static final RpcResult OK = new RpcResult(new Object[]{"ok"}); | 55 private static final RpcResult OK = new RpcResult(new Object[]{"ok"}); |
50 | 56 |
51 synchronized void handle2(RpcServer rpc,RpcCall call) throws IOException { | 57 private synchronized void handle2(RpcServer rpc,RpcCall call) throws IOException { |
52 //logger.info(call.cmd+" "+Arrays.asList(call.args)); | 58 //logger.info(call.cmd+" "+Arrays.asList(call.args)); |
53 String fileName = null; | 59 String fileName = null; |
54 if( call.cmd.equals("check") ) { | 60 if( call.cmd.equals("check") ) { |
55 // nothing | 61 // nothing |
56 } else if( call.cmd.equals("add") || call.cmd.equals("append") ) { | 62 } else if( call.cmd.equals("add") || call.cmd.equals("append") ) { |
123 } | 129 } |
124 } | 130 } |
125 rpc.write(result); | 131 rpc.write(result); |
126 } | 132 } |
127 | 133 |
128 void handleZip(RpcServer rpc) throws IOException { | 134 private void handleZip(RpcServer rpc) throws IOException { |
129 File zip = File.createTempFile("luan_",".zip"); | 135 File zip = File.createTempFile("luan_",".zip"); |
130 IoUtils.delete(zip); | 136 IoUtils.delete(zip); |
131 String cmd = "zip " + zip + " *"; | 137 String cmd = "zip " + zip + " *"; |
132 synchronized(this) { | 138 synchronized(this) { |
133 Process proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir); | 139 Process proc = Runtime.getRuntime().exec(new String[]{"bash","-c",cmd},null,dir); |
137 RpcResult result = new RpcResult(in,zip.length(),new Object[0]); | 143 RpcResult result = new RpcResult(in,zip.length(),new Object[0]); |
138 rpc.write(result); | 144 rpc.write(result); |
139 IoUtils.delete(zip); | 145 IoUtils.delete(zip); |
140 } | 146 } |
141 | 147 |
148 synchronized void copyTo(File dirTo) throws IOException { | |
149 IoUtils.mkdirs(dirTo); | |
150 for( File f : dir.listFiles() ) { | |
151 String name = f.getName(); | |
152 File to = new File(dirTo,name); | |
153 if( name.equals("index.json") ) { | |
154 IoUtils.copy(f,to); | |
155 } else { | |
156 IoUtils.link(f,to); | |
157 } | |
158 } | |
159 } | |
160 | |
142 } | 161 } |