From: paulr Date: Sun, 11 Jul 2010 06:24:59 +0000 (+1000) Subject: some more code. X-Git-Url: http://git.pjr.cc/?a=commitdiff_plain;h=754af6334eb9dc27818f355409f5528c714dce83;p=php-bacula-disk-changer.git some more code. --- diff --git a/bin/pbdc.php b/bin/pbdc.php index 8e61a23..b0424bb 100644 --- a/bin/pbdc.php +++ b/bin/pbdc.php @@ -2,20 +2,119 @@ $BASE_LIB="../lib"; $BASE_DATA="../db"; -$BASE_BIN="/usr/bin"; +$BACULA_USER="paulr"; + +global $BASE_DATA, $BASE_LIB, $BACULA_USER; require_once "$BASE_LIB/lib.php"; -if(!isset($argv[1])) { - echo "Usage: ".$argv[0]." command\n"; +if(!isset($argv[2])) { + echo "Usage: ".$argv[0]." changer_name command\n"; echo "\tbacula-config - outputs bacula config\n"; echo "\tinit - inits the bacula php changer stuff\n"; echo "\tadd-disk - adds a disk\n"; - echo "\tdisks - lists current disks\n"; + echo "\tdisks - lists currently (known) disks\n"; echo "\tstatus - prints status (whats where in which slot, drive, etc)\n"; exit(0); } -$command = $argv[1]; +$command = $argv[2]; + +switch($command) { + case "init": + init(); + break; + case "bacula-config": + break; + case "add-disk": + add_disk(); + break; + case "disks": + break; + case "status": + break; + default: + echo "invalid command\n"; +} +function init() +{ + global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv; + + // check if we are the bacula user + $user = posix_getlogin(); + if($user != $BACULA_USER) { + echo "This command must be run as the bacula user\n"; + exit(0); + } + + $changer_name = $argv[1]; + + echo "This command will init the data for $changer_name\n"; + + if(file_exists("$BASE_DATA/$changer_name.db")) { + //echo "Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:"; + $r = readline("Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:"); + 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); + } else { + echo "Ok, not doing it, so long chum\n"; + exit(0); + } + } + + echo "Creating database\n"; + $r = readline("Directory where automount occurs [/changer/]:"); + $r = readline("Number of drives [1]:"); + $r = readline("Size of tapes [20] (in gb):"); +} + +function add_disk() +{ + // disks get inited by going to /changer_dir/disk_uuid/ then: + // creating pbdc/changer_name/stuff. + global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv; + + $dh = opendir("/dev/disk/by-uuid"); + + $disk = ""; + $i = 0; + + while(($file = readdir($dh)) !== false) { + if($file != "." && $file != "..") { + $st = stat("/dev/disk/by-uuid/".$file); + $realname = basename(readlink("/dev/disk/by-uuid/".$file)); + + // now get the size + $fh = fopen("/sys/class/block/$realname/size", "r"); + $rl = fgets($fh); + fclose($fh); + $realsize = (int)(((($rl/1024)*512)/1024)/1024); + + echo "disk $i: /dev/$realname or /dev/disk/by-uuid/$file of size ".$realsize."gb\n"; + $disk[$i]["real"] = "/dev/$realname"; + $disk[$i]["syml"] = "/dev/disk/by-uuid/".$file; + $disk[$i]["size"] = $realsize; + $i++; + } + } + + closedir($dh); + + $i = readline("Choose a disk from the list above:"); + if(isset($disk[$i]["real"])) { + echo "you have chosen, ".$disk[$i]["syml"]." (".$disk[$i]["real"].") of size ".$disk[$i]["size"]."gb\n"; + $ans = readline("is this correct? [y/n]:"); + if($ans != "y") { + echo "you said no, i bail\n"; + exit(0); + } + } else { + echo "invalid selection\n"; + exit(0); + } + + // now we try and init the disk +} ?> \ No newline at end of file diff --git a/db/pbdc.db b/db/pbdc.db new file mode 100644 index 0000000..e69de29 diff --git a/db/pbdc_created.db b/db/pbdc_created.db new file mode 100644 index 0000000..60ea221 Binary files /dev/null and b/db/pbdc_created.db differ