comparison src/luan/host/Backup.java @ 1135:707a5d874f3e

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