Mercurial Hosting > luan
annotate src/luan/host/Backup.java @ 1330:f41919741100
fix security
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Mon, 11 Feb 2019 01:38:55 -0700 |
parents | 307e76ccd0d6 |
children | 25746915a241 |
rev | line source |
---|---|
1185
94cf2576a922
implement WebHandler for nginx
Franklin Schmidt <fschmidt@gmail.com>
parents:
1182
diff
changeset
|
1 package luan.host; |
1135 | 2 |
1264 | 3 import java.io.Closeable; |
1135 | 4 import java.io.File; |
5 import java.io.IOException; | |
6 import java.nio.file.Files; | |
7 import java.util.Map; | |
1264 | 8 import java.util.List; |
9 import java.util.ArrayList; | |
1135 | 10 import org.slf4j.Logger; |
11 import org.slf4j.LoggerFactory; | |
12 import org.apache.lucene.index.SnapshotDeletionPolicy; | |
13 import org.apache.lucene.index.IndexCommit; | |
14 import org.apache.lucene.store.FSDirectory; | |
15 import luan.LuanState; | |
16 import luan.LuanTable; | |
17 import luan.LuanException; | |
18 import luan.modules.PackageLuan; | |
19 import luan.modules.lucene.LuceneIndex; | |
1182 | 20 import luan.host.Log4j; |
1135 | 21 |
22 | |
23 public final class Backup { | |
24 private static final Logger logger = LoggerFactory.getLogger(Backup.class); | |
25 | |
26 private Backup() {} // never | |
27 | |
28 private static void mkdir(File dir) { | |
29 if( !dir.mkdirs() ) | |
30 throw new RuntimeException("couldn't make "+dir); | |
31 } | |
32 | |
33 private static void link(File from,File to) throws IOException { | |
34 Files.createLink( to.toPath(), from.toPath() ); | |
35 } | |
36 | |
37 private static void backupNonlocal(File from,File to) throws IOException { | |
38 mkdir(to); | |
39 for( File fromChild : from.listFiles() ) { | |
40 File toChild = new File( to, fromChild.getName() ); | |
41 if( fromChild.isDirectory() ) { | |
42 if( !fromChild.getName().equals("local") ) | |
43 backupNonlocal( fromChild, toChild ); | |
44 } else if( fromChild.isFile() ) { | |
45 link( fromChild, toChild ); | |
46 } else { | |
47 throw new RuntimeException(fromChild+" isn't dir or file"); | |
48 } | |
49 } | |
50 } | |
51 | |
52 private static final String getLucenes = | |
53 "local Lucene = require 'luan:lucene/Lucene.luan'\n" | |
54 +"local Table = require 'luan:Table.luan'\n" | |
55 +"return Table.copy(Lucene.instances)\n" | |
56 ; | |
57 | |
58 private static void backupLucene(File from,File to) throws IOException { | |
59 if( !new File(from,"site/init.luan").exists() ) { | |
60 return; | |
61 } | |
62 String fromPath = from.getCanonicalPath() + "/"; | |
63 LuanTable luceneInstances; | |
64 LuanState luan = null; | |
65 try { | |
66 if( WebHandler.isServing() ) { | |
67 luceneInstances = (LuanTable)WebHandler.runLuan( from.getName(), getLucenes, "getLucenes" ); | |
68 } else { | |
69 luan = new LuanState(); | |
1264 | 70 luan.onClose = new LuanState.OnClose() { |
71 private final List<Closeable> onClose = new ArrayList<Closeable>(); | |
72 | |
73 public void onClose(Closeable c) { | |
74 onClose.add(c); | |
75 } | |
76 | |
77 public void close() { | |
78 for( Closeable c : onClose ) { | |
79 try { | |
80 c.close(); | |
81 } catch(IOException e) { | |
82 logger.error(c.toString(),e); | |
83 } | |
84 } | |
85 onClose.clear(); | |
86 } | |
87 }; | |
1321
307e76ccd0d6
generalize separate logging
Franklin Schmidt <fschmidt@gmail.com>
parents:
1264
diff
changeset
|
88 WebHandler.initLuan( luan, from.toString(), from.getName(), false ); |
1135 | 89 PackageLuan.load(luan,"site:/init.luan"); |
90 luceneInstances = (LuanTable)luan.eval(getLucenes); | |
91 } | |
92 } catch(LuanException e) { | |
1187 | 93 throw new RuntimeException(from.getName(),e); |
1135 | 94 } |
95 for( Map.Entry entry : luceneInstances.rawIterable() ) { | |
96 LuanTable tbl = (LuanTable)entry.getKey(); | |
97 LuceneIndex li = (LuceneIndex)tbl.rawGet("java"); | |
98 SnapshotDeletionPolicy snapshotDeletionPolicy = li.snapshotDeletionPolicy(); | |
99 IndexCommit ic = snapshotDeletionPolicy.snapshot(); | |
100 try { | |
101 FSDirectory fsdir = (FSDirectory)ic.getDirectory(); | |
102 File dir = fsdir.getDirectory(); | |
103 String dirPath = dir.toString(); | |
104 if( !dirPath.startsWith(fromPath) ) | |
105 throw new RuntimeException(fromPath+" "+dirPath); | |
106 File toDir = new File( to, dirPath.substring(fromPath.length()) ); | |
107 mkdir(toDir); | |
108 for( String name : ic.getFileNames() ) { | |
109 link( new File(dir,name), new File(toDir,name) ); | |
110 } | |
111 } finally { | |
112 snapshotDeletionPolicy.release(ic); | |
113 } | |
114 } | |
115 if( luan != null ) | |
1264 | 116 luan.onClose.close(); |
1135 | 117 } |
118 | |
119 public static void backup(File sitesDir,File backupDir) throws IOException { | |
120 mkdir(backupDir); | |
121 for( File siteDir : sitesDir.listFiles() ) { | |
122 File to = new File( backupDir, siteDir.getName() ); | |
123 backupNonlocal( siteDir, to ); | |
124 backupLucene( siteDir, to ); | |
125 } | |
126 } | |
127 | |
128 public static void main(String[] args) throws Exception { | |
129 Log4j.initForConsole(); | |
1330 | 130 WebHandler.securityPassword = args[2]; |
1135 | 131 backup( new File(args[0]), new File(args[1]) ); |
132 System.exit(0); | |
133 } | |
134 } |