$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")&&check_init($changer_name)) {
+ //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";
+ $ra = readline("Directory where automount occurs [/changer/]:");
+ $rb = readline("Number of drives [1]:");
+ $rc = readline("Size of tapes [20] (in gb):");
+
+ if($ra == "") $ra = "/changer/";
+ if($rb == "") $rb = 1;
+ if($rc == "") $rc = 20;
+
+ conf_setVal($changer_name, "automountdir", "$ra");
+ conf_setVal($changer_name, "ndrives", "$rb");
+ conf_setVal($changer_name, "tapesize", "$rc");
+
+ // 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";
+}
+function check_init_and_fail($changer)
+{
+ $lk = conf_getVal($changer, "automountdir");
+ if(!$lk) {
+ echo "DB not init'd yet, please run init first\n";
+ exit(0);
+ }
+}
+
+function check_init($changer)
+{
+ $lk = conf_getVal($changer, "automountdir");
+ if(!$lk) {
+ return false;
+ }
+
+ return true;
+}
+
+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;
+
+ check_init_and_fail($argv[1]);
+
+ $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
+ // we have to get automount directory config
+ $dir = conf_getVal($argv[1], "automountdir");
+ if(!is_dir($dir)) {
+ echo "cant find automount directory, $dir\n";
+ }
+ $mkd = "$dir/".basename($disk[$i]["syml"])."/pbdc/".$argv[1]."/tapes/";
+ $k = mkdir($mkd, 0700, true);
+
+ if(!is_dir($mkd)) {
+ echo "Error, couldn't create directory, check permissions on $mkd?\n";
+ }
+
+ $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) {
+ echo "Sorry, max number of tapes is $max, creating $max instead\n";
+ }
+
+ $did = add_diskToDB($argv[1], basename($disk[$i]["syml"]));
+
+ if(!$did) {
+ echo "Disk already exists, doing nothing\n";
+ exit(0);
+ }
+
+ //echo "did: $did\n";
+}
?>
\ No newline at end of file