just cleaning up some output
[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                 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                                 echo "Tape: $file\n";
109                         } else {
110                                 //echo "didnt match: $file\n";
111                         }
112                         //exit(0);
113                 }
114                 closedir($dh);
115         }
116         
117         $nt = (int)(conf_getVal($changer, "ndrives"));
118         for($i = 0; $i < $nt; $i++) {
119                 if(file_exists("$tapeloc/$changer-drive$i")) {
120                         $rl = basename(readlink("$tapeloc/$changer-drive$i"));
121                         echo "drive $i points has tape $rl loaded\n";
122                 } else {
123                         echo "drive $i is unloaded\n";
124                 }
125         }
126 }
127
128 function list_disks()
129 {
130         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
131         
132         check_init_and_fail($argv[1]);
133         
134         $db = db_getDB($argv[1]);
135         
136         $res = $db->query("select * from disk_list");
137         
138         $ret = false;
139         foreach($res as $row) {
140                 echo "Disk ".$row["disk_id"].": ".$row["disk_name"]."\n";
141                 $ret = true;    
142         }
143         
144         if(!$ret) {
145                 echo "No disks defined yet for this changer\n";
146         }
147         
148         
149         return;
150 }
151
152 function init()
153 {
154         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
155         
156         // check if we are the bacula user
157         $user = posix_getlogin();
158         if($user != $BACULA_USER) {
159                 echo "This command must be run as the bacula user\n";
160                 exit(0);
161         }
162         
163         $changer_name = $argv[1];
164         
165         echo "This command will init the data for $changer_name\n";
166         
167         if(file_exists("$BASE_DATA/$changer_name.db")&&check_init($changer_name)) {
168                 //echo "Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:";
169                 $r = readline("Daemon is already init'd, are you sure you wish to do this, it will loose all config [y/n]:");
170                 if($r == "y") {
171                         echo "Ok, but its your disaster, waiting 5 seconds prior to init'ing the database (hit ctrl-c to exit)\n";
172                         sleep(5);
173                 } else {
174                         echo "Ok, not doing it, so long chum\n";
175                         exit(0);
176                 }
177         }
178         
179         echo "Creating database\n";
180         $ra = readline("Directory where automount occurs [/changer/]:");
181         $rb = readline("Number of drives [1]:");
182         $rc = readline("Size of tapes [20] (in gb):");
183         $rd = readline("Where to store drive pointers [/var/run/bacula/]:");
184         
185         if($ra == "") $ra = "/changer/";
186         if($rb == "") $rb = 1;
187         if($rc == "") $rc = 20;
188         if($rd == "") $rd = "/var/run/bacula/";
189         
190         conf_setVal($changer_name, "automountdir", "$ra");
191         conf_setVal($changer_name, "ndrives", "$rb");
192         conf_setVal($changer_name, "tapesize", "$rc");
193         conf_setVal($changer_name, "drivelocation", "$rd");
194         
195         // check if they were set
196         echo "got: ".conf_getVal($changer_name, "automountdir").", and ".conf_getVal($changer_name, "tapesize").", and ".conf_getVal($changer_name, "ndrives")."\n";
197 }
198
199 function check_init_and_fail($changer)
200 {
201         $lk = conf_getVal($changer, "automountdir");
202         if(!$lk) {
203                 echo "DB not init'd yet, please run init first\n";
204                 exit(0);
205         } 
206 }
207
208 function check_init($changer)
209 {
210         $lk = conf_getVal($changer, "automountdir");
211         if(!$lk) {
212                 return false;
213         } 
214         
215         return true;
216 }
217
218 function add_disk()
219 {
220         // disks get inited by going to /changer_dir/disk_uuid/ then:
221         // creating pbdc/changer_name/stuff.
222         global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
223         
224         check_init_and_fail($argv[1]);
225         
226         $dh = opendir("/dev/disk/by-uuid");
227         
228         $disk = "";
229         $i = 0;
230         
231         while(($file = readdir($dh)) !== false) {
232                 if($file != "." && $file != "..") {
233                         $st = stat("/dev/disk/by-uuid/".$file);
234                         $realname = basename(readlink("/dev/disk/by-uuid/".$file));
235                         
236                         // now get the size
237                         $fh = fopen("/sys/class/block/$realname/size", "r");
238                         $rl = fgets($fh);
239                         fclose($fh);
240                         $realsize = (int)(((($rl/1024)*512)/1024)/1024);
241                         
242                         echo "disk $i: /dev/$realname or /dev/disk/by-uuid/$file of size ".$realsize."gb\n";
243                         $disk[$i]["real"] = "/dev/$realname";
244                         $disk[$i]["syml"] = "/dev/disk/by-uuid/".$file;
245                         $disk[$i]["size"] = $realsize;
246                         $i++;
247                 }
248         }
249         
250         closedir($dh);
251         
252         $i = readline("Choose a disk from the list above:");
253         if(isset($disk[$i]["real"])) {
254                 echo "you have chosen, ".$disk[$i]["syml"]." (".$disk[$i]["real"].") of size ".$disk[$i]["size"]."gb\n";
255                 $ans = readline("is this correct? [y/n]:");
256                 if($ans != "y") {
257                         echo "you said no, i bail\n";
258                         exit(0);
259                 }
260         } else {
261                 echo "invalid selection\n";
262                 exit(0);
263         }
264
265         // now we try and init the disk
266         // we have to get automount directory config
267         $dir = conf_getVal($argv[1], "automountdir");
268         if(!is_dir($dir)) {
269                 echo "cant find automount directory, $dir\n";
270         }
271         $mkd = "$dir/".basename($disk[$i]["syml"])."/pbdc/".$argv[1]."/tapes/";
272         $k = mkdir($mkd, 0700, true);
273         
274         if(!is_dir($mkd)) {
275                 echo "Error, couldn't create directory, check permissions on $mkd?\n";
276         }
277         
278         $ts = conf_getVal($argv[1], "tapesize");
279         $max = (int)($disk[$i]["size"]/$ts);
280         
281         $kt = (int)(readline("No of tapes to create [max:$max] @ ".$ts."gb each:"));
282         if($kt > $max) {
283                 echo "Sorry, max number of tapes is $max, creating $max instead\n";
284                 $kt = $max;
285         }
286         
287         $did = add_diskToDB($argv[1], basename($disk[$i]["syml"]));
288         
289         if(!$did) {
290                 echo "Disk already exists, doing nothing\n";
291                 exit(0);
292         }
293         
294         // disk names are d.$did_vol0000x
295         //echo "did: $did\n";
296         for($ii=0; $ii < $kt; $ii++) {
297                 $tid = sprintf("d%d_vol%04d", $did, $ii);
298                 //echo "would create $tid\n";
299                 $tp = "$dir/".basename($disk[$i]["syml"])."/pbdc/".$argv[1]."/tapes/$tid";
300                 if(!file_exists($tp)) {
301                         fopen($tp, "w");
302                         echo "created tape $tid\n";
303                 } else {
304                         echo "tape $tid already existed\n";
305                 }
306         }
307 }
308 ?>