Loading world from backup

Discussion in 'Plugin Development' started by vsams14, Sep 2, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    vsams14

    As title says... Pseudocode is acceptable.
    Current method, does not work for some reason:
    1. Unload world
    2. Delete all world files
    3. Copy world files from backup to the original folder
    4. restart server

    My copy method is:
    PHP:
        public void copyFiles(File srcFile destthrows IOException {
            
    //Check to ensure that the source is valid
            
    if (!src.exists()) {
                throw new 
    IOException("copyFiles: Can not find source: " src.getAbsolutePath()+".");
            } else if (!
    src.canRead()) { //check to ensure we have rights to the source...
                
    throw new IOException("copyFiles: No right to source: " src.getAbsolutePath()+".");
            }
            
    //is this a directory copy?
            
    if (src.isDirectory())    {
                if (!
    dest.exists()) { //does the destination already exist?
                    //if not we need to make it exist if possible (note this is mkdirs not mkdir)
                    
    if (!dest.mkdirs()) {
                        throw new 
    IOException("copyFiles: Could not create direcotry: " dest.getAbsolutePath() + ".");
                    }
                }
                
    //get a listing of files...
                
    String list[] = src.list();
                
    //copy all the files in the list.
                
    for (int i 0< list.lengthi++)
                {
                    
    File dest1 = new File(dest, list[i]);
                    
    File src1 = new File(src, list[i]);
                    
    copyFiles(src1 dest1);
                }
            } else {
                
    //This was not a directory, so lets just copy the file
                
    FileInputStream fin null;
                
    FileOutputStream fout null;
                
    byte[] buffer = new byte[4096]; //Buffer 4K at a time (you can change this).
                
    int bytesRead;
                try {
                    
    //open the files for input and output
                    
    fin =  new FileInputStream(src);
                    
    fout = new FileOutputStream (dest);
                    
    //while bytesRead indicates a successful read, lets write...
                    
    while ((bytesRead fin.read(buffer)) >= 0) {
                        
    fout.write(buffer,0,bytesRead);
                    }
                    
    log.info("Copied "+src+" to "+dest);
                } catch (
    IOException e) { //Error copying file...
                    
    IOException wrapper = new IOException("copyFiles: Unable to copy file: " +
                            
    src.getAbsolutePath() + "to" dest.getAbsolutePath()+".");
                    
    wrapper.initCause(e);
                    
    wrapper.setStackTrace(e.getStackTrace());
                    throw 
    wrapper;
                } finally { 
    //Ensure that the files are closed (if they were open).
                    
    if (fin != null) { fin.close(); }
                    if (
    fout != null) { fout.close(); }
                }
            }
        }
    Thanks!
     
  2. Offline

    escortkeel

    You'll have to ensure that the server has died before executing this code.
     
  3. Offline

    vsams14

    So it's impossible with a plugin?
     
  4. Offline

    escortkeel

    It is possible, but hard. You need to start a non-deamon thread which continuously polls the server to see if it has shut down.
     
  5. Offline

    vsams14

    I'm not sure I know how to do that. Could you impart your knowledge onto me?
     
  6. Offline

    ferrybig

    I say its imposiable to do, because the file handles are stored inside an class as static reference, unless you can access that class by an hackky way (reflection) and close them, the restoring of the world will fail
     
  7. Offline

    vsams14

    I believe that nothing is truly impossible. I'm not trying to copy over the world while the server is on. That IS impossible because bukkit makes it so. However, if I can get the server to start an external task that does the copying once the server is shut down, it should be quite easy. I just don't know how to do the "start an external task that runs after server shuts down" bit...
     
  8. Offline

    escortkeel

    Ok. First investigate whether when the server is shutting down it calls System.exit(). If it does:

    You are going to need to start your task in another JVM triggered by a shutdown thread (registered via Rutime.getRuntime().registerShutdownHook()).

    If it does not:

    You can just start a thread that (in a while loop) sleeps until the server is shut down. Once it has shut down, just run your task then.
     
  9. Offline

    vsams14

    Not sure I know how to do those things either >.<
    Could you help? I'm sure that if very basic code is provided, or a wiki page to look at, I'll figure it out...

    Thanks!
     
  10. Offline

    Tj3

    Just guessing here, never done it before.
    You could create a seperate executable jar to do the actual backup/restore and re-start the server when it finishes. Then you'd call it with the Runtime.exec method and stop the server.
     
  11. Offline

    vsams14

    Might be acceptable to just have a class file that gets launched at server stop... Not sure though...

    Got the shutdown hook to work. How do I start the server back up afterwards? Is there any javacode that can initialize the bukkit server? If not, should I start a batch file from java to start the server? My client is running a Ubuntu server, if that helps...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 28, 2016
Thread Status:
Not open for further replies.

Share This Page