comparison src/luan/host/Backup.java @ 1185:94cf2576a922

implement WebHandler for nginx
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 21 Feb 2018 21:22:16 -0700
parents src/luan/host/jetty/Backup.java@0b55a1af5a44
children 83c8a5a47f70
comparison
equal deleted inserted replaced
1184:2eba58842bbb 1185:94cf2576a922
1 package luan.host;
2
3 import java.io.File;
4 import java.io.IOException;
5 import java.nio.file.Files;
6 import java.util.Map;
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9 import org.apache.lucene.index.SnapshotDeletionPolicy;
10 import org.apache.lucene.index.IndexCommit;
11 import org.apache.lucene.store.FSDirectory;
12 import luan.LuanState;
13 import luan.LuanTable;
14 import luan.LuanException;
15 import luan.modules.PackageLuan;
16 import luan.modules.lucene.LuceneIndex;
17 import luan.host.Log4j;
18
19
20 public final class Backup {
21 private static final Logger logger = LoggerFactory.getLogger(Backup.class);
22
23 private Backup() {} // never
24
25 private static void mkdir(File dir) {
26 if( !dir.mkdirs() )
27 throw new RuntimeException("couldn't make "+dir);
28 }
29
30 private static void link(File from,File to) throws IOException {
31 Files.createLink( to.toPath(), from.toPath() );
32 }
33
34 private static void backupNonlocal(File from,File to) throws IOException {
35 mkdir(to);
36 for( File fromChild : from.listFiles() ) {
37 File toChild = new File( to, fromChild.getName() );
38 if( fromChild.isDirectory() ) {
39 if( !fromChild.getName().equals("local") )
40 backupNonlocal( fromChild, toChild );
41 } else if( fromChild.isFile() ) {
42 link( fromChild, toChild );
43 } else {
44 throw new RuntimeException(fromChild+" isn't dir or file");
45 }
46 }
47 }
48
49 private static final String getLucenes =
50 "local Lucene = require 'luan:lucene/Lucene.luan'\n"
51 +"local Table = require 'luan:Table.luan'\n"
52 +"return Table.copy(Lucene.instances)\n"
53 ;
54
55 private static void backupLucene(File from,File to) throws IOException {
56 if( !new File(from,"site/init.luan").exists() ) {
57 return;
58 }
59 String fromPath = from.getCanonicalPath() + "/";
60 LuanTable luceneInstances;
61 LuanState luan = null;
62 try {
63 if( WebHandler.isServing() ) {
64 luceneInstances = (LuanTable)WebHandler.runLuan( from.getName(), getLucenes, "getLucenes" );
65 } else {
66 luan = new LuanState();
67 WebHandler.initLuan( luan, from.toString(), from.getName() );
68 PackageLuan.load(luan,"site:/init.luan");
69 luceneInstances = (LuanTable)luan.eval(getLucenes);
70 }
71 } catch(LuanException e) {
72 throw new RuntimeException(e);
73 }
74 for( Map.Entry entry : luceneInstances.rawIterable() ) {
75 LuanTable tbl = (LuanTable)entry.getKey();
76 LuceneIndex li = (LuceneIndex)tbl.rawGet("java");
77 SnapshotDeletionPolicy snapshotDeletionPolicy = li.snapshotDeletionPolicy();
78 IndexCommit ic = snapshotDeletionPolicy.snapshot();
79 try {
80 FSDirectory fsdir = (FSDirectory)ic.getDirectory();
81 File dir = fsdir.getDirectory();
82 String dirPath = dir.toString();
83 if( !dirPath.startsWith(fromPath) )
84 throw new RuntimeException(fromPath+" "+dirPath);
85 File toDir = new File( to, dirPath.substring(fromPath.length()) );
86 mkdir(toDir);
87 for( String name : ic.getFileNames() ) {
88 link( new File(dir,name), new File(toDir,name) );
89 }
90 } finally {
91 snapshotDeletionPolicy.release(ic);
92 }
93 }
94 if( luan != null )
95 luan.close();
96 }
97
98 public static void backup(File sitesDir,File backupDir) throws IOException {
99 mkdir(backupDir);
100 for( File siteDir : sitesDir.listFiles() ) {
101 File to = new File( backupDir, siteDir.getName() );
102 backupNonlocal( siteDir, to );
103 backupLucene( siteDir, to );
104 }
105 }
106
107 public static void main(String[] args) throws Exception {
108 Log4j.initForConsole();
109 backup( new File(args[0]), new File(args[1]) );
110 System.exit(0);
111 }
112 }