lots of code changes...
[php-bacula-disk-changer.git] / bin / pbdc.php
1 <?php
2
3 $BASE_LIB="../lib";
4 $BASE_DATA="../db";
5 $BACULA_USER="paulr";
6
7 global $BASE_DATA, $BASE_LIB, $BACULA_USER;
8
9 require_once "$BASE_LIB/lib.php";
10
11 if(!isset($argv[2])) {
12         echo "Usage: ".$argv[0]." changer_name command\n";
13         echo "\tbacula-config - outputs bacula config\n";
14         echo "\tinit - inits the bacula php changer stuff\n";
15         echo "\tadd-disk - adds a disk\n";
16         echo "\tdisks - lists currently (known) disks\n";
17         echo "\tstatus - prints status (whats where in which slot, drive, etc)\n";
18         exit(0);
19 }
20
21 $command = $argv[2];
22
23 switch($command) {
24         case "init":
25                 init();
26                 break;
27         case "bacula-config":
28                 break;
29         case "add-disk":
30                 add_disk();
31                 break;
32         case "disks":
33                 break;
34         case "status":
35                 break;
36         default:
37                 echo "invalid command\n";
38 }
39
40 function init()
41 {
42         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
43         
44         // check if we are the bacula user
45         $user = posix_getlogin();
46         if($user != $BACULA_USER) {
47                 echo "This command must be run as the bacula user\n";
48                 exit(0);
49         }
50         
51         $changer_name = $argv[1];
52         
53         echo "This command will init the data for $changer_name\n";
54         
55         if(file_exists("$BASE_DATA/$changer_name.db")&&check_init($changer_name)) {
56                 //echo "Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:";
57                 $r = readline("Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:");
58                 if($r == "y") {
59                         echo "Ok, but its your disaster, waiting 5 seconds prior to init'ing the database (hit ctrl-c to exit)\n";
60                         sleep(5);
61                 } else {
62                         echo "Ok, not doing it, so long chum\n";
63                         exit(0);
64                 }
65         }
66         
67         echo "Creating database\n";
68         $ra = readline("Directory where automount occurs [/changer/]:");
69         $rb = readline("Number of drives [1]:");
70         $rc = readline("Size of tapes [20] (in gb):");
71         
72         if($ra == "") $ra = "/changer/";
73         if($rb == "") $rb = 1;
74         if($rc == "") $rc = 20;
75         
76         conf_setVal($changer_name, "automountdir", "$ra");
77         conf_setVal($changer_name, "ndrives", "$rb");
78         conf_setVal($changer_name, "tapesize", "$rc");
79         
80         // check if they were set
81         echo "got: ".conf_getVal($changer_name, "automountdir").", and ".conf_getVal($changer_name, "tapesize").", and ".conf_getVal($changer_name, "ndrives")."\n";
82 }
83
84 function check_init_and_fail($changer)
85 {
86         $lk = conf_getVal($changer, "automountdir");
87         if(!$lk) {
88                 echo "DB not init'd yet, please run init first\n";
89                 exit(0);
90         } 
91 }
92
93 function check_init($changer)
94 {
95         $lk = conf_getVal($changer, "automountdir");
96         if(!$lk) {
97                 return false;
98         } 
99         
100         return true;
101 }
102
103 function add_disk()
104 {
105         // disks get inited by going to /changer_dir/disk_uuid/ then:
106         // creating pbdc/changer_name/stuff.
107         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
108         
109         check_init_and_fail($argv[1]);
110         
111         $dh = opendir("/dev/disk/by-uuid");
112         
113         $disk = "";
114         $i = 0;
115         
116         while(($file = readdir($dh)) !== false) {
117                 if($file != "." && $file != "..") {
118                         $st = stat("/dev/disk/by-uuid/".$file);
119                         $realname = basename(readlink("/dev/disk/by-uuid/".$file));
120                         
121                         // now get the size
122                         $fh = fopen("/sys/class/block/$realname/size", "r");
123                         $rl = fgets($fh);
124                         fclose($fh);
125                         $realsize = (int)(((($rl/1024)*512)/1024)/1024);
126                         
127                         echo "disk $i: /dev/$realname or /dev/disk/by-uuid/$file of size ".$realsize."gb\n";
128                         $disk[$i]["real"] = "/dev/$realname";
129                         $disk[$i]["syml"] = "/dev/disk/by-uuid/".$file;
130                         $disk[$i]["size"] = $realsize;
131                         $i++;
132                 }
133         }
134         
135         closedir($dh);
136         
137         $i = readline("Choose a disk from the list above:");
138         if(isset($disk[$i]["real"])) {
139                 echo "you have chosen, ".$disk[$i]["syml"]." (".$disk[$i]["real"].") of size ".$disk[$i]["size"]."gb\n";
140                 $ans = readline("is this correct? [y/n]:");
141                 if($ans != "y") {
142                         echo "you said no, i bail\n";
143                         exit(0);
144                 }
145         } else {
146                 echo "invalid selection\n";
147                 exit(0);
148         }
149
150         // now we try and init the disk
151         // we have to get automount directory config
152         $dir = conf_getVal($argv[1], "automountdir");
153         if(!is_dir($dir)) {
154                 echo "cant find automount directory, $dir\n";
155         }
156         $mkd = "$dir/".basename($disk[$i]["syml"])."/pbdc/".$argv[1]."/tapes/";
157         $k = mkdir($mkd, 0700, true);
158         
159         if(!is_dir($mkd)) {
160                 echo "Error, couldn't create directory, check permissions on $mkd?\n";
161         }
162         
163         $ts = conf_getVal($argv[1], "tapesize");
164         $max = (int)($disk[$i]["size"]/$ts);
165         
166         $k = (int)(readline("No of tapes to create [max:$max] @ ".$ts."gb each:"));
167         if($k > $max) {
168                 echo "Sorry, max number of tapes is $max, creating $max instead\n";
169         }
170         
171         $did = add_diskToDB($argv[1], basename($disk[$i]["syml"]));
172         
173         if(!$did) {
174                 echo "Disk already exists, doing nothing\n";
175                 exit(0);
176         }
177         
178         //echo "did: $did\n";
179 }
180 ?>