From: paulr Date: Mon, 12 Jul 2010 02:55:56 +0000 (+1000) Subject: stuff X-Git-Url: http://git.pjr.cc/?p=php-bacula-disk-changer.git;a=commitdiff_plain;h=95682a89442421c2ae8bfbf729c255c993874d7c stuff --- diff --git a/bin/pbdc-bacula.php b/bin/pbdc-bacula.php index 5225d49..5a36526 100644 --- a/bin/pbdc-bacula.php +++ b/bin/pbdc-bacula.php @@ -54,7 +54,7 @@ function unload_tape() $changer = $argv[1]; $taped = $argv[5]; - unlink("$tapeloc/$changer-drive$taped"); + if(file_exists("$tapeloc/$changer-drive$taped")) unlink("$tapeloc/$changer-drive$taped"); } function load_tape() @@ -74,29 +74,20 @@ function load_tape() $ret = false; $slot = $argv[3]; - $i=1; + + $res = $db->query("select disk_id,tape_name from slots where slot_no=='$slot'"); 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) { - if($i == $slot) { - unlink("$tapeloc/$changer-drive$taped"); - symlink("$amloc/".$row["disk_name"]."/pbdc/".$argv[1]."/tapes/$file", "$tapeloc/$changer-drive$taped"); - exit(0); - } - $i++; - } else { - //echo "didnt match: $file\n"; - } - //exit(0); - } - closedir($dh); + $did = $row["disk_id"]; + $file = $row["tape_name"]; } - echo "0\n"; + $res = $db->query("select disk_name from disk_list where disk_id=='$did'"); + foreach($res as $row) { + $diskname = $row["disk_name"]; + } + + if(file_exists("$tapeloc/$changer-drive$taped")) unlink("$tapeloc/$changer-drive$taped"); + symlink("$amloc/".$diskname."/pbdc/".$argv[1]."/tapes/$file", "$tapeloc/$changer-drive$taped"); } @@ -155,27 +146,28 @@ function list_tapes() $db = db_getDB($argv[1]); - $res = $db->query("select * from disk_list order by disk_id"); + $res = $db->query("select * from slots order by slot_no"); $amloc = conf_getVal($changer, "automountdir"); - $ret = false; - $i=1; 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 "slot $i: $file\n"; - $i++; - } else { - //echo "didnt match: $file\n"; - } - //exit(0); + $tapename = $row["tape_name"]; + $did = $row["disk_id"]; + $sn = $row["slot_no"]; + $res2 = $db->query("select disk_name from disk_list where disk_id=='$did'"); + $dn = ""; + foreach($res2 as $row2) { + $dn = $row2[0]; + } + $tn = "$amloc/".$dn."/pbdc/".$argv[1]."/tapes/$tapename"; + if(file_exists($tn)) { + echo "slot $sn: $tapename\n"; + } else { + echo "slot $sn:\n"; } - closedir($dh); } } @@ -188,30 +180,36 @@ function num_slots() $db = db_getDB($argv[1]); - $res = $db->query("select * from disk_list order by disk_id"); - - $amloc = conf_getVal($changer, "automountdir"); + $res = $db->query("select max(slot_no) from slots"); - $ret = false; - $i=1; 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) { - $i++; - } else { - //echo "didnt match: $file\n"; - } - //exit(0); - } - closedir($dh); + $mslot = $row[0]; } - $i--; - echo "$i\n"; + return $mslot; + +// $amloc = conf_getVal($changer, "automountdir"); + +// $ret = false; +// $i=1; +// 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) { +// $i++; +// } else { +// //echo "didnt match: $file\n"; +// } +// //exit(0); +// } +// closedir($dh); +// } +// $i--; + +// echo "$i\n"; } diff --git a/bin/pbdc.php b/bin/pbdc.php index 896ae8d..8c67315 100644 --- a/bin/pbdc.php +++ b/bin/pbdc.php @@ -105,7 +105,13 @@ function pbdc_status() $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"; + $sql = "select slot_no from slots where tape_name=='$file'"; + $res2 = $db->query($sql); + if($res2) { + foreach($res2 as $row2) + $slot = $row2["slot_no"]; + } else $slot = "none"; + echo "Tape: $file (slot $sln)\n"; } else { //echo "didnt match: $file\n"; } @@ -155,8 +161,9 @@ function init() // check if we are the bacula user $user = posix_getlogin(); + $user = $_SERVER["USER"]; if($user != $BACULA_USER) { - echo "This command must be run as the bacula user\n"; + echo "This command must be run as the bacula user ($BACULA_USER not $user)\n"; exit(0); } @@ -170,6 +177,7 @@ function init() if($r == "y") { echo "Ok, but its your disaster, waiting 5 seconds prior to init'ing the database (hit ctrl-c to exit)\n"; sleep(5); + unlink("$BASE_DATA/$changer_name.db"); } else { echo "Ok, not doing it, so long chum\n"; exit(0); @@ -303,6 +311,61 @@ function add_disk() } else { echo "tape $tid already existed\n"; } + add_to_slot($argv[1], $tid, $did); } } + +function add_to_slot($changer, $tapename, $disk_id) +{ + global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv; + + $db = db_getDB($changer); + + $slot = ""; + $slots = 0; + $max_slot = 0; + + $res = $db->query("select * from slots"); + foreach($res as $row) { + $slot[$row["slot_no"]]["diskid"] = $row["disk_id"]; + $slot[$row["slot_no"]]["tapename"] = $row["tape_name"]; + $slots++; + if($row["slot_no"] > $max_slot) $max_slot = $row["slot_no"]; + } + + // first tape, straight in with you + if($slots == 0) { + $db->query("insert into slots values (NULL, 1, $disk_id, '$tapename')"); + return; + } + + // next hunt thru the slots and see if its filled yet. + foreach($slot as $lk) { + if($lk["tapename"] == $tapename) { + // tape was already there, move along + return; + } + } + + // now we go from 0 to the end looking for a free slot + for($i=1; $i < $max_slot; $i++) { + if(!isset($slot[$i])) { + // we have a free slot, but it shouldnt appear that way like that.. oh well. + $db->query("insert into slots values (NULL, $i, $disk_id, '$tapename')"); + return; + } + if($slot[$i]["tapename"] == "") { + // slot is free + $db->query("delete from slots where slot_no='$i'"); + $db->query("insert into slots values (NULL, $i, $disk_id, '$tapename')"); + return; + } + } + + // So much error checking to do. + // if we made it here, we need more slots. + $ns = $max_slot + 1; + $db->query("insert into slots values (NULL, $ns, $disk_id, '$tapename')"); + return; +} ?> \ No newline at end of file diff --git a/db/db.sql b/db/db.sql index 7c8d722..c5a8a70 100644 --- a/db/db.sql +++ b/db/db.sql @@ -7,3 +7,10 @@ CREATE TABLE "config" ( "config_name" TEXT NOT NULL, "config_value" TEXT NOT NULL ); + +create table "slots" ( + "slots_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + "slot_no" INTEGER, + "disk_id" INTEGER, + "tape_name" TEXT +); \ No newline at end of file diff --git a/install.sh b/install.sh old mode 100755 new mode 100644 index 2d56826..e4e2d29 --- a/install.sh +++ b/install.sh @@ -5,7 +5,7 @@ read -p "Bacula User ID [bacula]:" user_id if [ "x$install_base" == "x" ] then - install_base="/opt/pdbc" + install_base="/opt/pbdc" fi if [ "x$user_id" == "x" ] @@ -19,6 +19,7 @@ mkdir -p $install_base/db export install_base user_id +rm -f /etc/pbdc.conf cat >> /etc/pbdc.conf << EOF BASE_DIR=$install_base BASE_LIB=$install_base/lib/ diff --git a/lib/lib.php b/lib/lib.php index 26ca7ed..de8e152 100644 --- a/lib/lib.php +++ b/lib/lib.php @@ -27,6 +27,7 @@ function db_createDB($dbobject) $dbobject->query('CREATE TABLE "disk_list" ("disk_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"disk_name" TEXT NOT NULL);'); $dbobject->query('CREATE TABLE sqlite_sequence(name,seq)'); $dbobject->query('CREATE TABLE "config" ("config_name" TEXT NOT NULL,"config_value" TEXT NOT NULL)'); + $dbobject->query('CREATE TABLE "slots" ("slots_id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,"slot_no" INTEGER,"disk_id" INTEGER,"tape_name" TEXT)'); // TODO create action group here