Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

map backup

Comments in 'Plugin Development' started by mrfoxvolt, Mar 9, 2014.

  1. mrfoxvolt
    Offline

    mrfoxvolt Active Member Plugin Developer

    Joined:
    Jan 26, 2014
    Posts:
    52
    Plugins:
    1
    Minecraft User:
    rirititi
    hello,
    how to do a backup after every some minutes
    please
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Try this. Hope it works. Test this with caution and disclaimer given: this lots of I/O may somehow (although I don't think it will) delete the whole server.
    PHP:
    public function init(){
      
    $fixedTime="YOU_DEFINE";
      
    ServerAPI::request()->schedule($fixedTime, array($this"backup"), array(), true);
    }
    public function 
    backup(){
      
    console("[INFO] Backup in progress...");
      
    $time=time();
      
    $server=ServerAPI::request();
      
    $server->api->console->run("kick @a server backup");
      
    $list=array();
      
    forach($server->api->level->getAll() as $level){
        
    $list[]="$level";
        
    $server->api->level->unloadLevel($leveltrue); // true to force unload default world
      
    }
      
    $dir=dirname(FILE_PATH).$server->api->getProperty("server-name")."-backups/";
      @
    mkdir($dir);
      
    $dir.="Backup-made-at-".date(DATE_ATOM)."-timestamp-".microtime()."/";
      
    $files=0;
      
    $bytes=0;
      if(
    $this->copyDir(FILE_PATH$dir$files$bytes)!==true)
        return 
    console("Backup failed");
      
    $size=$bytes/1024// use your own way to optimize this
      
    $unit="KB"// and this too. A vanilla server is about 23 KB large with one world
      
    console("[INFO] Backup finished. Copied $files files and $size $unit. Reloading worlds.");
      foreach(
    $list as $level){
        
    $server->api->level->loadLevel($level);
      }
      
    $time-=time();
      
    $time*=-1;
      
    console("[INFO] Backup fully completed ($time s). Backup generated at ".FORMAT_GREEN.$dir.FORMAT_AQUA.". Players can join the server now.");
    }
    public function 
    copyDir($orig$targ, &$filesCnt, &$bytesCnt){
      if(
    substr($targ, -1)!="/" and substr($targ, -1)!="\\")
        
    $targ.="/";
      if(
    is_file($targ))return false;
      
    $dir=dir($orig);
      while((
    $name=$dir->read())!==false){
        if(
    is_file($orig.$name)){
          if(
    copy($orig.$name$targ.$name)){
            
    $filesCnt++;
            
    $bytesCnt+=filesize($orig.$name);
          }else 
    console("[WARNING] $name"."$file cannot be copied!"); // I expect console.log to be unable to be copied. But it is possible that it can.
        
    }
        elseif(
    str_replace(array(".""/""\\"), array(" "" "" "), $name)!==""// don't copy self and parent
          
    $this->copyDir($orig.$name$targ.$name$filesCnt$bytesCnt);
      }
      return 
    true;
    }
    Or if you want a ZIP, read http://hk2.php.net/manual/en/refs.compression.php.
  3. mrfoxvolt
    Offline

    mrfoxvolt Active Member Plugin Developer

    Joined:
    Jan 26, 2014
    Posts:
    52
    Plugins:
    1
    Minecraft User:
    rirititi
    thank your very very much

Share This Page

Advertisement