Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
  #1  
Old 07-09-2010, 06:11 PM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
GBall Creator (Automated Server Backup)

GBall Creator is a script to generate a "GBall package". This package can contain multiple files. Essentially, it works as a ZIP file but can be created serverside.

An example of why this is useful is that with one line of code you can backup a server's folder structure (selectively) — it can even be used to backup the accounts folder.

PHP Code:
function onCreated() {
  
this.version "1.0";     // eventually used in expander?
  
  
temp.mb 1048576;
  
this.maxWrite = (mb 2); // max to write at once; I find mb*9 is the absolute limit
                            // but below that is usually better to avoid flood alert
  
  
this.loopLimit 5000;    // the number of loops before a sleep -- must be less than
                            // this.maxlooplimit -- 5000 is good but don't expect
                            // the server to be playable while the files are added to
                            // the "to write" list (the first part which takes very little
                            // time, but will lock up the server)
  
  
this.delay 60 6;      // delay between trying to write, default 60*6 -- this seems not to
                            // cause flood alert, which, while protected against, can prevent
                            // other scripts on the server from functioning
}

/*
    The way folders work is that if I were to compress
    only "weapons/*", there would be a folder called
    "weapons" with all of the contents inside.
    
    If I were to compress "weapons/*" and "levels/world/*",
    there would be two folders: "weapons" and "world"
*/

public function GBall(descriptionoutputNamefolders) {
  
temp.lines null;
  
  for (
temp.folderName folders) {
    
temp.folder.loadFolder(folderName[0], folderName[1]);
    
temp.folderName[0].positions("/");
    
temp.pathToFolder folderName[0].substring(0p[p.size() - 1]);
    
temp.tokens pathToFolder.tokenize("/");
    
temp.bottomFolderName tokens[tokens.size() - 1];
    
    for (
temp.file folder) {
      
// add it to the package
      
temp.str.loadString(pathToFolder "/" file);
      
str "  " base64encode(bottomFolderName "/" file) @ " " base64encode(str);
      
lines.add(str);
      
      if (
this.loop()) {
        echo(
"GBall: Building file cache...");
      }
    }
    
    
this.loop();
  }
  
  
// don't save it all at once to save resources + avoid flood alert
  
temp.line 0;
  
temp.0;
  
  while (
line lines.size()) {
    
temp.chars 0;
    
temp.toWrite = {format("GBALL v%s (%s): %s"this.versionbase64encode(description), int(timevar2))};
    
    while (
chars this.maxWrite && line lines.size()) {
      
toWrite.add(lines[line]);
      
chars += lines[line].length();
      
line ++;
      
this.loop();
    }
    
    
temp.fileName outputName ".arc";
    
temp.hasTried false;
    
    while (! (
fileExists(fileName))) {
      if (
hasTried) {
        echo(
"GBall: Unable to write '" fileName "'; flood alert or bad permissions. Trying again after delay...");
        
sleep(this.delay);
      }
      
      
toWrite.saveLines(outputName ".arc"false);
      echo(
"GBall: Writing...");
      
      
hasTried true;
      
sleep(1); // sometimes it takes a minute for the file to "exist"
    
}
    
    
// sleep to avoid flood alert
    
if (line lines.size()) {
      
sleep(this.delay);
    }
    
    
++;
  }
  
  echo(
"GBall: Saved file (" outputName "x.arc)!");
}

function 
loop() {
  
this.count ++;
  
  if (
this.count >= this.loopLimit) {
    
this.count 0;
    
sleep(1);
    return 
true;
  }

An example of using this:
PHP Code:
// description, file prefix, list of folders
GBall("Backup of accounts""data/accountsbk_", {{"accounts/*"true}}); 
This will create a backup of the accounts folder, including the folder structure, and save it in several files (depending on how many accounts you have on the server) in the "data" folder. The reason for saving it in several files is that the max file size is 10 MB for script writing.

The best part of the script is that it is optimized to work on populated servers. Initially the script will load all files needed into memory. This is the only part that will cause the server to lock up. After this, the files will be written every few minutes to evade the flood alert. There is protection so that if the flood alert kicks in and prevents a file from being written it will wait and then attempt to write it after the flood alert has subsided. This stage (file writing) can take hours, depending on the settings you specify at the top of the script, but it will not lag the server.

Once the script has finished you will have files with a ".arc" extension. Download all of these files, then open the GBallExpander Java application included at the end of this post and select the first file. The application will expand the GBall files into the actual folder structure. This should not take long at all. You can also store the files in their GBall form and expand them later (you may wish to ZIP them to save file space).

The Java application which expands the GBall files works fine on Mac and Windows, and probably any other system with the JRE installed.




I should also mention that this script works fine to backup any type of file I've been able to throw at it, from text files to levels to images. Essentially you could backup the entire levels/* folder on your server.

I used this on Era last night to backup the accounts folder and it worked flawlessly, so it should work just about anywhere.

I feel like I've done a ****ty job explaining in this post, so feel free to ask questions.

I haven't yet received permission to post a link to the source code for the Java application so forum PM me if you want it (or decompile the JAR).
Attached Files
File Type: zip GBallExpander.jar.zip (4.1 KB, 876 views)
__________________

Last edited by cbk1994; 07-09-2010 at 07:33 PM..
Reply With Quote
 

Tags
archive, backup

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 11:50 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.