code code code
authorpaulr <me@pjr.cc>
Sun, 11 Jul 2010 11:02:02 +0000 (21:02 +1000)
committerpaulr <me@pjr.cc>
Sun, 11 Jul 2010 11:02:02 +0000 (21:02 +1000)
bin/pbdc.php

index c1ea23d..0e66657 100644 (file)
@@ -25,18 +25,99 @@ switch($command) {
                init();
                break;
        case "bacula-config":
+               bacula_config();
                break;
        case "add-disk":
                add_disk();
                break;
        case "disks":
+               list_disks();
                break;
        case "status":
+               pbdc_status();
                break;
        default:
                echo "invalid command\n";
 }
 
+function bacula_config()
+{
+       global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
+       
+       check_init_and_fail($argv[1]);
+       $changer = $argv[1];
+
+       $db = db_getDB($argv[1]);
+       
+       // TODO: do this bit
+}
+
+function pbdc_status()
+{
+       global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
+       
+       check_init_and_fail($argv[1]);
+       $changer = $argv[1];
+
+       $db = db_getDB($argv[1]);
+       
+       $res = $db->query("select * from disk_list");
+       
+       $tapeloc = conf_getVal($changer, "drivelocation");
+       $amloc = conf_getVal($changer, "automountdir");
+       
+       $ret = false;
+       foreach($res as $row) {
+               echo "Listing tapes for disk ".$row["disk_id"].", ".$row["disk_name"]."\n";
+               //echo "dir: $amloc/".$row["disk_name"]."/pbdc/".$argv[1]."/tapes/\n";
+               //exit(0);
+               $dh = opendir("$amloc/".$row["disk_name"]."/pbdc/".$argv[1]."/tapes/");
+               while(($file = readdir($dh)) !== false) {
+                       if(ereg("d[0-9]+_vol[0-9]+", $file)!=false) {
+                               echo "Tape: $file\n";
+                       } else {
+                               //echo "didnt match: $file\n";
+                       }
+                       //exit(0);
+               }
+               closedir($dh);
+       }
+       
+       $nt = (int)(conf_getVal($changer, "ndrives"));
+       for($i = 0; $i < $nt; $i++) {
+               if(file_exists("$tapeloc/$changer-drive$i")) {
+                       $rl = basename(readlink("$tapeloc/$changer-drive$i"));
+                       echo "drive $i points has tape $rl loaded\n";
+               } else {
+                       echo "drive $i is unloaded\n";
+               }
+       }
+}
+
+function list_disks()
+{
+       global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
+       
+       check_init_and_fail($argv[1]);
+       
+       $db = db_getDB($argv[1]);
+       
+       $res = $db->query("select * from disk_list");
+       
+       $ret = false;
+       foreach($res as $row) {
+               echo "Disk ".$row["disk_id"].": ".$row["disk_name"]."\n";
+               $ret = true;    
+       }
+       
+       if(!$ret) {
+               echo "No disks defined yet for this changer\n";
+       }
+       
+       
+       return;
+}
+
 function init()
 {
        global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
@@ -68,14 +149,17 @@ function init()
        $ra = readline("Directory where automount occurs [/changer/]:");
        $rb = readline("Number of drives [1]:");
        $rc = readline("Size of tapes [20] (in gb):");
+       $rd = readline("Where to store drive pointers [/var/run/bacula/]:");
        
        if($ra == "") $ra = "/changer/";
        if($rb == "") $rb = 1;
        if($rc == "") $rc = 20;
+       if($rd == "") $rd = "/var/run/bacula/";
        
        conf_setVal($changer_name, "automountdir", "$ra");
        conf_setVal($changer_name, "ndrives", "$rb");
        conf_setVal($changer_name, "tapesize", "$rc");
+       conf_setVal($changer_name, "drivelocation", "$rd");
        
        // check if they were set
        echo "got: ".conf_getVal($changer_name, "automountdir").", and ".conf_getVal($changer_name, "tapesize").", and ".conf_getVal($changer_name, "ndrives")."\n";
@@ -163,9 +247,10 @@ function add_disk()
        $ts = conf_getVal($argv[1], "tapesize");
        $max = (int)($disk[$i]["size"]/$ts);
        
-       $k = (int)(readline("No of tapes to create [max:$max] @ ".$ts."gb each:"));
-       if($k > $max) {
+       $kt = (int)(readline("No of tapes to create [max:$max] @ ".$ts."gb each:"));
+       if($kt > $max) {
                echo "Sorry, max number of tapes is $max, creating $max instead\n";
+               $kt = $max;
        }
        
        $did = add_diskToDB($argv[1], basename($disk[$i]["syml"]));
@@ -175,6 +260,18 @@ function add_disk()
                exit(0);
        }
        
+       // disk names are d.$did_vol0000x
        //echo "did: $did\n";
+       for($ii=0; $ii < $kt; $ii++) {
+               $tid = sprintf("d%d_vol%04d", $did, $ii);
+               //echo "would create $tid\n";
+               $tp = "$dir/".basename($disk[$i]["syml"])."/pbdc/".$argv[1]."/tapes/$tid";
+               if(!file_exists($tp)) {
+                       fopen($tp, "w");
+                       echo "created tape $tid\n";
+               } else {
+                       echo "tape $tid already existed\n";
+               }
+       }
 }
 ?>
\ No newline at end of file