stuff
[php-bacula-disk-changer.git] / bin / pbdc.php
1 <?php
2
3 $BASE_LIB=$_SERVER["BASE_LIB"];
4 $BASE_DATA=$_SERVER["BASE_DATA"];
5 $BACULA_USER=$_SERVER["BACULA_USER"];
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                 bacula_config();
29                 break;
30         case "add-disk":
31                 add_disk();
32                 break;
33         case "disks":
34                 list_disks();
35                 break;
36         case "status":
37                 pbdc_status();
38                 break;
39         default:
40                 echo "invalid command\n";
41 }
42
43 function bacula_config()
44 {
45         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
46         
47         check_init_and_fail($argv[1]);
48         $changer = $argv[1];
49
50         $db = db_getDB($argv[1]);
51         
52         $ntapes = conf_getVal($changer, "ndrives");
53         $tapeloc = conf_getVal($changer, "drivelocation");
54         
55         // TODO: do this bit
56         // bacula-sd.conf
57         ?>
58 Autochanger {
59   Name = <?php echo "$changer\n"?>
60   <?php
61   for($i=0; $i<$ntapes; $i++) {
62         echo "Device = \"$changer-drive$i\"\n";
63   } 
64   ?>
65   Changer Command = "/usr/local/bin/vchanger %c %o %S %a %d"
66   Changer Device = "<?php echo $changer ?>"
67 }
68
69 <?php 
70         for($i=0; $i < $ntapes; $i++) {
71 ?>
72 Device {
73   Name = <?php echo "\"$changer-drive$i\"\n" ?>
74   DriveIndex = <?php echo "$i\n" ?>
75   Autochanger = yes;
76   DeviceType = File
77   MediaType = File
78   ArchiveDevice = <?php echo "\"$tapeloc/$changer-drive$i\"\n" ?>
79   RemovableMedia = no;
80   RandomAccess = yes;
81 }
82 <?php
83         }
84 }
85
86 function pbdc_status()
87 {
88         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
89         
90         check_init_and_fail($argv[1]);
91         $changer = $argv[1];
92
93         $db = db_getDB($argv[1]);
94         
95         $res = $db->query("select * from disk_list");
96         
97         $tapeloc = conf_getVal($changer, "drivelocation");
98         $amloc = conf_getVal($changer, "automountdir");
99         
100         $ret = false;
101         foreach($res as $row) {
102                 echo "Listing tapes for disk ".$row["disk_id"].", ".$row["disk_name"]."\n";
103                 //echo "dir: $amloc/".$row["disk_name"]."/pbdc/".$argv[1]."/tapes/\n";
104                 //exit(0);
105                 $dh = opendir("$amloc/".$row["disk_name"]."/pbdc/".$argv[1]."/tapes/");
106                 while(($file = readdir($dh)) !== false) {
107                         if(ereg("d[0-9]+_vol[0-9]+", $file)!=false) {
108                                 $sql = "select slot_no from slots where tape_name=='$file'";
109                                 $res2 = $db->query($sql);
110                                 if($res2) {
111                                         foreach($res2 as $row2)
112                                         $slot = $row2["slot_no"];
113                                 } else $slot = "none";
114                                 echo "Tape: $file (slot $sln)\n";
115                         } else {
116                                 //echo "didnt match: $file\n";
117                         }
118                         //exit(0);
119                 }
120                 closedir($dh);
121         }
122         
123         $nt = (int)(conf_getVal($changer, "ndrives"));
124         for($i = 0; $i < $nt; $i++) {
125                 if(file_exists("$tapeloc/$changer-drive$i")) {
126                         $rl = basename(readlink("$tapeloc/$changer-drive$i"));
127                         echo "drive $i points has tape $rl loaded\n";
128                 } else {
129                         echo "drive $i is unloaded\n";
130                 }
131         }
132 }
133
134 function list_disks()
135 {
136         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
137         
138         check_init_and_fail($argv[1]);
139         
140         $db = db_getDB($argv[1]);
141         
142         $res = $db->query("select * from disk_list");
143         
144         $ret = false;
145         foreach($res as $row) {
146                 echo "Disk ".$row["disk_id"].": ".$row["disk_name"]."\n";
147                 $ret = true;    
148         }
149         
150         if(!$ret) {
151                 echo "No disks defined yet for this changer\n";
152         }
153         
154         
155         return;
156 }
157
158 function init()
159 {
160         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
161         
162         // check if we are the bacula user
163         $user = posix_getlogin();
164         $user = $_SERVER["USER"];
165         if($user != $BACULA_USER) {
166                 echo "This command must be run as the bacula user ($BACULA_USER not $user)\n";
167                 exit(0);
168         }
169         
170         $changer_name = $argv[1];
171         
172         echo "This command will init the data for $changer_name\n";
173         
174         if(file_exists("$BASE_DATA/$changer_name.db")&&check_init($changer_name)) {
175                 //echo "Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:";
176                 $r = readline("Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:");
177                 if($r == "y") {
178                         echo "Ok, but its your disaster, waiting 5 seconds prior to init'ing the database (hit ctrl-c to exit)\n";
179                         sleep(5);
180                         unlink("$BASE_DATA/$changer_name.db");
181                 } else {
182                         echo "Ok, not doing it, so long chum\n";
183                         exit(0);
184                 }
185         }
186         
187         echo "Creating database\n";
188         $ra = readline("Directory where automount occurs [/changer/]:");
189         $rb = readline("Number of drives [1]:");
190         $rc = readline("Size of tapes [20] (in gb):");
191         $rd = readline("Where to store drive pointers [/var/run/bacula/]:");
192         
193         if($ra == "") $ra = "/changer/";
194         if($rb == "") $rb = 1;
195         if($rc == "") $rc = 20;
196         if($rd == "") $rd = "/var/run/bacula/";
197         
198         conf_setVal($changer_name, "automountdir", "$ra");
199         conf_setVal($changer_name, "ndrives", "$rb");
200         conf_setVal($changer_name, "tapesize", "$rc");
201         conf_setVal($changer_name, "drivelocation", "$rd");
202         
203         // check if they were set
204         echo "got: ".conf_getVal($changer_name, "automountdir").", and ".conf_getVal($changer_name, "tapesize").", and ".conf_getVal($changer_name, "ndrives")."\n";
205 }
206
207 function check_init_and_fail($changer)
208 {
209         $lk = conf_getVal($changer, "automountdir");
210         if(!$lk) {
211                 echo "DB not init'd yet, please run init first\n";
212                 exit(0);
213         } 
214 }
215
216 function check_init($changer)
217 {
218         $lk = conf_getVal($changer, "automountdir");
219         if(!$lk) {
220                 return false;
221         } 
222         
223         return true;
224 }
225
226 function add_disk()
227 {
228         // disks get inited by going to /changer_dir/disk_uuid/ then:
229         // creating pbdc/changer_name/stuff.
230         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
231         
232         check_init_and_fail($argv[1]);
233         
234         $dh = opendir("/dev/disk/by-uuid");
235         
236         $disk = "";
237         $i = 0;
238         
239         while(($file = readdir($dh)) !== false) {
240                 if($file != "." && $file != "..") {
241                         $st = stat("/dev/disk/by-uuid/".$file);
242                         $realname = basename(readlink("/dev/disk/by-uuid/".$file));
243                         
244                         // now get the size
245                         $fh = fopen("/sys/class/block/$realname/size", "r");
246                         $rl = fgets($fh);
247                         fclose($fh);
248                         $realsize = (int)(((($rl/1024)*512)/1024)/1024);
249                         
250                         echo "disk $i: /dev/$realname or /dev/disk/by-uuid/$file of size ".$realsize."gb\n";
251                         $disk[$i]["real"] = "/dev/$realname";
252                         $disk[$i]["syml"] = "/dev/disk/by-uuid/".$file;
253                         $disk[$i]["size"] = $realsize;
254                         $i++;
255                 }
256         }
257         
258         closedir($dh);
259         
260         $i = readline("Choose a disk from the list above:");
261         if(isset($disk[$i]["real"])) {
262                 echo "you have chosen, ".$disk[$i]["syml"]." (".$disk[$i]["real"].") of size ".$disk[$i]["size"]."gb\n";
263                 $ans = readline("is this correct? [y/n]:");
264                 if($ans != "y") {
265                         echo "you said no, i bail\n";
266                         exit(0);
267                 }
268         } else {
269                 echo "invalid selection\n";
270                 exit(0);
271         }
272
273         // now we try and init the disk
274         // we have to get automount directory config
275         $dir = conf_getVal($argv[1], "automountdir");
276         if(!is_dir($dir)) {
277                 echo "cant find automount directory, $dir\n";
278         }
279         $mkd = "$dir/".basename($disk[$i]["syml"])."/pbdc/".$argv[1]."/tapes/";
280         $k = mkdir($mkd, 0700, true);
281         
282         if(!is_dir($mkd)) {
283                 echo "Error, couldn't create directory, check permissions on $mkd?\n";
284         }
285         
286         $ts = conf_getVal($argv[1], "tapesize");
287         $max = (int)($disk[$i]["size"]/$ts);
288         
289         $kt = (int)(readline("No of tapes to create [max:$max] @ ".$ts."gb each:"));
290         if($kt > $max) {
291                 echo "Sorry, max number of tapes is $max, creating $max instead\n";
292                 $kt = $max;
293         }
294         
295         $did = add_diskToDB($argv[1], basename($disk[$i]["syml"]));
296         
297         if(!$did) {
298                 echo "Disk already exists, doing nothing\n";
299                 exit(0);
300         }
301         
302         // disk names are d.$did_vol0000x
303         //echo "did: $did\n";
304         for($ii=0; $ii < $kt; $ii++) {
305                 $tid = sprintf("d%d_vol%04d", $did, $ii);
306                 //echo "would create $tid\n";
307                 $tp = "$dir/".basename($disk[$i]["syml"])."/pbdc/".$argv[1]."/tapes/$tid";
308                 if(!file_exists($tp)) {
309                         fopen($tp, "w");
310                         echo "created tape $tid\n";
311                 } else {
312                         echo "tape $tid already existed\n";
313                 }
314                 add_to_slot($argv[1], $tid, $did);
315         }
316 }
317
318 function add_to_slot($changer, $tapename, $disk_id)
319 {
320         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
321         
322         $db = db_getDB($changer);
323         
324         $slot = "";
325         $slots = 0;
326         $max_slot = 0;
327         
328         $res = $db->query("select * from slots");
329         foreach($res as $row) {
330                 $slot[$row["slot_no"]]["diskid"] = $row["disk_id"];
331                 $slot[$row["slot_no"]]["tapename"] = $row["tape_name"];
332                 $slots++;
333                 if($row["slot_no"] > $max_slot) $max_slot = $row["slot_no"];
334         }
335         
336         // first tape, straight in with you
337         if($slots == 0) {
338                 $db->query("insert into slots values (NULL, 1, $disk_id, '$tapename')");
339                 return;
340         }
341         
342         // next hunt thru the slots and see if its filled yet.
343         foreach($slot as $lk) {
344                 if($lk["tapename"] == $tapename) {
345                         // tape was already there, move along
346                         return;
347                 }
348         }
349         
350         // now we go from 0 to the end looking for a free slot
351         for($i=1; $i < $max_slot; $i++) {
352                 if(!isset($slot[$i])) {
353                         // we have a free slot, but it shouldnt appear that way like that.. oh well.
354                         $db->query("insert into slots values (NULL, $i, $disk_id, '$tapename')");
355                         return;
356                 }
357                 if($slot[$i]["tapename"] == "") {
358                         // slot is free
359                         $db->query("delete from slots where slot_no='$i'");
360                         $db->query("insert into slots values (NULL, $i, $disk_id, '$tapename')");
361                         return;
362                 }
363         }
364         
365         // So much error checking to do.
366         // if we made it here, we need more slots.
367         $ns = $max_slot + 1;
368         $db->query("insert into slots values (NULL, $ns, $disk_id, '$tapename')");
369         return;
370 }
371 ?>