├── .travis.yml ├── .gitignore ├── src └── main │ └── java │ └── com │ └── d2fn │ └── guano │ ├── Job.java │ ├── FSVisitor.java │ ├── FSWalker.java │ ├── RestoreJob.java │ ├── Guano.java │ └── DumpJob.java ├── LICENSE.txt ├── readme.md └── pom.xml /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | script: mvn clean verify 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .project 2 | .settings 3 | /target 4 | .checkstyle 5 | .idea 6 | *.iml 7 | /vagrant/.vagrant 8 | /vagrant/ansible 9 | .classpath 10 | /bin 11 | 12 | -------------------------------------------------------------------------------- /src/main/java/com/d2fn/guano/Job.java: -------------------------------------------------------------------------------- 1 | package com.d2fn.guano; 2 | 3 | /** 4 | * Job 5 | * @author Dietrich Featherston 6 | */ 7 | public interface Job { 8 | public void go(); 9 | } 10 | -------------------------------------------------------------------------------- /src/main/java/com/d2fn/guano/FSVisitor.java: -------------------------------------------------------------------------------- 1 | package com.d2fn.guano; 2 | 3 | import java.io.File; 4 | 5 | /** 6 | * FSVisitor 7 | * @author Dietrich Featherston 8 | */ 9 | public interface FSVisitor { 10 | public void visit(File f, byte[] data, String znode); 11 | } 12 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2016 Dietrich Featherston 2 | 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 5 | 6 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 7 | 8 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 9 | -------------------------------------------------------------------------------- /src/main/java/com/d2fn/guano/FSWalker.java: -------------------------------------------------------------------------------- 1 | package com.d2fn.guano; 2 | 3 | import org.apache.commons.io.FileUtils; 4 | 5 | import java.io.*; 6 | 7 | /** 8 | * FSWalker 9 | * @author Dietrich Featherston 10 | */ 11 | public class FSWalker { 12 | 13 | private String localRoot; 14 | private String znodeRoot; 15 | 16 | public FSWalker(String localRoot, String znodeRoot) { 17 | this.localRoot = localRoot; 18 | this.znodeRoot = znodeRoot; 19 | } 20 | 21 | public void go(FSVisitor visitor) { 22 | File f = new File(localRoot); 23 | walk(visitor, f, znodeRoot); 24 | } 25 | 26 | private void walk(FSVisitor visitor, File f, String znode) { 27 | 28 | byte[] data = new byte[0]; 29 | 30 | if(znode.endsWith("_znode")) { 31 | znode = znode.substring(0, znode.length()-6); 32 | } 33 | if(znode.endsWith("/") && znode.length() > 1) { 34 | znode = znode.substring(0, znode.length()-1); 35 | } 36 | 37 | try { 38 | data = FileUtils.readFileToByteArray(f); 39 | } catch (IOException e) {} 40 | 41 | visitor.visit(f, data, znode); 42 | 43 | if(f.isDirectory()) { 44 | File[] children = f.listFiles(); 45 | for(File child : children) { 46 | walk(visitor, child, (znode.equals("/") ? "" : znode) + "/" + child.getName()); 47 | } 48 | } 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | BADGES 2 | 3 | [ ![Download](https://api.bintray.com/packages/feldoh/Guano/guano/images/download.svg) ](https://bintray.com/feldoh/Guano/guano/_latestVersion) [![Build Status](https://travis-ci.org/feldoh/guano.svg?branch=master)](https://travis-ci.org/feldoh/guano) 4 | 5 | NAME 6 | 7 | guano -- dump or restore Zookeeper trees 8 | 9 | SYNOPSIS 10 | 11 | guano [operands ...] 12 | 13 | DESCRIPTION 14 | 15 | This tool dumps and restores zookeeper state to/from a matching folder structure on disk. 16 | The server argument is mandatory then either a node to dump and where to dump it, 17 | or a previous dump to restore and the root to restore it to. 18 | 19 | usage: guano [-v] -s [-d -o ] [-r -i ] 20 | 21 | -s,--server *Required, the zookeeper remote server to connect to 22 | i.e. "localhost:2181" 23 | 24 | -d,--dump-znode the znode to dump (recursively) 25 | -o,--output-dir the output directory to which znode 26 | information should be written (must be a 27 | normal, empty directory) 28 | 29 | -i,--input-dir the input directory from which znode 30 | information should be read 31 | -r,--restore-znode the znode into which read data should be 32 | restored 33 | 34 | -v,--verbose enable debug output 35 | 36 | Note: When restoring you need to provide one level up as the node selected for the dump is included. 37 | e.g. java -jar target/guano-0.1a.jar -s "zookeeper-01.mydomain.com" -o "/tmp/zkdump" -d "/myroot" 38 | java -jar target/guano-0.1a.jar -s "zookeeper-01.mydomain.com" -i "/tmp/zkdump" -r "/" 39 | 40 | PREBUILT BINARIES 41 | 42 | Pre-built binaries are available for most common platforms on Bintray 43 | -------------------------------------------------------------------------------- /src/main/java/com/d2fn/guano/RestoreJob.java: -------------------------------------------------------------------------------- 1 | package com.d2fn.guano; 2 | 3 | import org.apache.zookeeper.*; 4 | import org.apache.zookeeper.data.ACL; 5 | 6 | import java.io.File; 7 | import java.util.ArrayList; 8 | import java.util.Arrays; 9 | 10 | /** 11 | * RestoreJob 12 | * @author Dietrich Featherston 13 | */ 14 | public class RestoreJob implements Job, Watcher, FSVisitor { 15 | 16 | private String zkServer; 17 | private String znode; 18 | private String inputDir; 19 | 20 | private ZooKeeper zk; 21 | 22 | public RestoreJob(String zkServer, String znode, String inputDir) { 23 | this.zkServer = zkServer; 24 | this.znode = znode; 25 | this.inputDir = inputDir; 26 | } 27 | 28 | public void go() { 29 | System.out.println("running restore job: "); 30 | System.out.println("zookeeper server: " + zkServer); 31 | System.out.println("reading from local directory: " + inputDir); 32 | System.out.println("restoring to zookeeper path: " + znode); 33 | 34 | try { 35 | zk = new ZooKeeper(zkServer + znode, 10000, this); 36 | while(!zk.getState().isConnected()) { 37 | System.out.println("connecting to " + zkServer + " with chroot " + znode); 38 | Thread.sleep(1000L); 39 | } 40 | FSWalker walker = new FSWalker(inputDir, "/"); 41 | walker.go(this); 42 | 43 | } catch (Exception e) { 44 | System.err.println(e.getMessage()); 45 | System.exit(1); 46 | } 47 | } 48 | 49 | /** 50 | * on the left we have data read from a file on the local file system 51 | * 52 | * @param f 53 | * @param data 54 | * @param znode 55 | */ 56 | @Override 57 | public void visit(File f, byte[] data, String znode) { 58 | // System.out.println(f.getPath()); 59 | // System.out.println(" -> " + znode); 60 | createOrSetZnode(data, znode); 61 | } 62 | 63 | private void createOrSetZnode(byte[] data, String znode) { 64 | try { 65 | String s = zk.create(znode, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); 66 | } catch (KeeperException.NoNodeException e) { 67 | e.printStackTrace(System.err); 68 | } catch (KeeperException.NodeExistsException e) { 69 | try { 70 | byte[] zdata = zk.getData(znode, false, null); 71 | if(!Arrays.equals(data, zdata)) { 72 | zk.setData(znode, data, -1); 73 | } 74 | } catch (Exception e2) { 75 | e2.printStackTrace(System.err); 76 | } 77 | } catch (KeeperException e) { 78 | e.printStackTrace(System.err); 79 | } catch (InterruptedException e) { 80 | e.printStackTrace(System.err); 81 | } 82 | } 83 | 84 | @Override 85 | public void process(WatchedEvent watchedEvent) { 86 | ;; 87 | } 88 | } 89 | -------------------------------------------------------------------------------- /src/main/java/com/d2fn/guano/Guano.java: -------------------------------------------------------------------------------- 1 | package com.d2fn.guano; 2 | 3 | import org.apache.commons.cli.*; 4 | 5 | /** 6 | * Guano 7 | * @author Dietrich Featherston 8 | */ 9 | public class Guano implements Job { 10 | 11 | private Options options; 12 | private CommandLine cmd; 13 | 14 | private Job job; 15 | 16 | public Guano(Options options, CommandLine cmd) { 17 | // buildJob(cmd) 18 | this.options = options; 19 | this.cmd = cmd; 20 | buildJob(); 21 | } 22 | 23 | public void buildJob() { 24 | 25 | boolean verbose = false; 26 | 27 | if(cmd.hasOption("v")) { 28 | verbose = true; 29 | } 30 | 31 | if(!cmd.hasOption("s")) { 32 | usage(options); 33 | } 34 | 35 | String server = cmd.getOptionValue("s"); 36 | 37 | // dump? 38 | if(cmd.hasOption("d") && cmd.hasOption("o")) { 39 | String znode = cmd.getOptionValue("d"); 40 | String outputDir = cmd.getOptionValue("o"); 41 | job = new DumpJob(server, outputDir, znode); 42 | } 43 | // restore 44 | else if(cmd.hasOption("r") && cmd.hasOption("i")) { 45 | String znode = cmd.getOptionValue("r"); 46 | String inputDir = cmd.getOptionValue("i"); 47 | job = new RestoreJob(server, znode, inputDir); 48 | } 49 | else { 50 | usage(options); 51 | } 52 | } 53 | 54 | @Override 55 | public void go() { 56 | job.go(); 57 | } 58 | 59 | public static void main(String[] args) { 60 | Options options = initOptions(); 61 | CommandLineParser parser = new PosixParser(); 62 | CommandLine cmd = null; 63 | try { 64 | cmd = parser.parse(options, args); 65 | } catch (ParseException e) { 66 | usage(options); 67 | } 68 | 69 | Guano g = new Guano(options, cmd); 70 | g.go(); 71 | } 72 | 73 | public static void usage(Options options) { 74 | HelpFormatter formatter = new HelpFormatter(); 75 | formatter.printHelp("guano", options); 76 | System.exit(1); 77 | } 78 | 79 | public static Options initOptions() { 80 | Options options = new Options(); 81 | options.addOption("s", "server", true, "the zookeeper remote server to connect to (ie \"localhost:2181\""); 82 | options.addOption("r", "restore-znode", true, "the znode into which read data should be restored"); 83 | options.addOption("d", "dump-znode", true, "the znode to dump (recursively)"); 84 | options.addOption("o", "output-dir", true, "the output directory to which znode information should be written (must be a normal, empty directory)"); 85 | options.addOption("i", "input-dir", true, "the input directory from which znode information should be read"); 86 | options.addOption("v", "verbose", false, "enable debug output"); 87 | return options; 88 | } 89 | } -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | 5 | com.d2fn 6 | guano 7 | 0.1a 8 | 9 | guano 10 | jar 11 | 12 | 13 | 14 | commons-cli 15 | commons-cli 16 | 1.3.1 17 | 18 | 19 | org.apache.commons 20 | commons-io 21 | 1.3.2 22 | 23 | 24 | org.apache.zookeeper 25 | zookeeper 26 | 3.4.9 27 | 28 | 29 | com.sun.jmx 30 | jmxri 31 | 32 | 33 | com.sun.jdmk 34 | jmxtools 35 | 36 | 37 | javax.jms 38 | jms 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | org.apache.maven.plugins 48 | maven-compiler-plugin 49 | 2.3.2 50 | 51 | 1.6 52 | 1.6 53 | 54 | 55 | 56 | org.apache.maven.plugins 57 | maven-shade-plugin 58 | 1.4 59 | 60 | true 61 | 62 | 63 | *:* 64 | 65 | META-INF/*.SF 66 | META-INF/*.DSA 67 | META-INF/*.RSA 68 | 69 | 70 | 71 | 72 | 73 | 74 | package 75 | 76 | shade 77 | 78 | 79 | 80 | 82 | 84 | com.d2fn.guano.Guano 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | -------------------------------------------------------------------------------- /src/main/java/com/d2fn/guano/DumpJob.java: -------------------------------------------------------------------------------- 1 | package com.d2fn.guano; 2 | 3 | import org.apache.zookeeper.WatchedEvent; 4 | import org.apache.zookeeper.Watcher; 5 | import org.apache.zookeeper.ZooKeeper; 6 | import org.apache.zookeeper.data.Stat; 7 | 8 | import java.io.File; 9 | import java.io.FileOutputStream; 10 | import java.io.IOException; 11 | import java.util.List; 12 | 13 | /** 14 | * DumpJob 15 | * @author Dietrich Featherston 16 | */ 17 | public class DumpJob implements Job, Watcher { 18 | 19 | private String zkServer; 20 | private String outputDir; 21 | private String znode; 22 | 23 | public DumpJob(String zkServer, String outputDir, String znode) { 24 | this.zkServer = zkServer; 25 | this.outputDir = outputDir; 26 | this.znode = znode; 27 | } 28 | 29 | public void go() { 30 | System.out.println("dumping data from zookeeper"); 31 | System.out.println("zookeeper server: " + zkServer); 32 | System.out.println("reading from zookeeper path: " + znode); 33 | System.out.println("dumping to local directory: " + outputDir); 34 | 35 | try { 36 | ZooKeeper zk = new ZooKeeper(zkServer + znode, 10000, this); 37 | go(zk); 38 | } catch (IOException e) { 39 | System.err.println("error connecting to " + zkServer); 40 | System.exit(1); 41 | } 42 | } 43 | 44 | private void go(ZooKeeper zk) { 45 | try { 46 | while(!zk.getState().isConnected()) { 47 | System.out.println("connecting to " + zkServer + " with chroot " + znode); 48 | Thread.sleep(1000L); 49 | } 50 | dumpChild(zk, outputDir + znode, "", ""); 51 | } catch (Exception e) { 52 | System.err.println(e.getMessage()); 53 | } 54 | } 55 | 56 | private void dumpChild(ZooKeeper zk, String outputPath, String znodeParent, String znode) throws Exception { 57 | 58 | String znodePath = znodeParent + znode; 59 | 60 | System.out.println("znodePath: " + znodePath); 61 | System.out.println("outputPath: " + outputPath); 62 | String currznode = znodePath.length() == 0 ? "/" : znodePath; 63 | List children = zk.getChildren(currznode, false); 64 | if(!children.isEmpty()) { 65 | 66 | // ensure parent dir is created 67 | createFolder(outputPath); 68 | 69 | // this znode is a dir, so ensure the directory is created and build a __znode value in its dir 70 | writeZnode(zk, outputPath + "/_znode", currznode); 71 | 72 | for(String c : children) { 73 | System.out.println("c: " + c); 74 | dumpChild(zk, outputPath + "/" + c, znodePath + "/", c); 75 | } 76 | } 77 | else { 78 | // this znode has no contents to write a plan file with the znode contents here 79 | writeZnode(zk, outputPath, currznode); 80 | } 81 | } 82 | 83 | private void writeZnode(ZooKeeper zk, String outFile, String znode) throws Exception { 84 | Stat stat = new Stat(); 85 | byte[] data = zk.getData(znode, false, stat); 86 | if(data != null && data.length > 0 && stat.getEphemeralOwner() == 0) { 87 | String str = new String(data); 88 | if(!str.equals("null")) { 89 | FileOutputStream out = new FileOutputStream(outFile); 90 | out.write(data); 91 | out.flush(); 92 | out.close(); 93 | } 94 | } else { 95 | if (!outFile.contains("_znode") && stat.getEphemeralOwner() == 0) { 96 | // Create an empty folder for Permanent nodes. 97 | createFolder(outFile); 98 | } 99 | 100 | } 101 | } 102 | 103 | private void createFolder(String path) { 104 | File f = new File(path); 105 | if(!f.exists() ) { 106 | boolean s = f.mkdirs(); 107 | if (s) { 108 | System.out.println("Created folder: " + path); 109 | } 110 | } 111 | 112 | } 113 | 114 | @Override 115 | public void process(WatchedEvent watchedEvent) { 116 | ;; 117 | } 118 | } 119 | --------------------------------------------------------------------------------