7 global $BASE_DATA, $BASE_LIB, $BACULA_USER;
9 require_once "$BASE_LIB/lib.php";
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";
40 echo "invalid command\n";
43 function bacula_config()
45 global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
47 check_init_and_fail($argv[1]);
50 $db = db_getDB($argv[1]);
52 $ntapes = conf_getVal($changer, "ndrives");
53 $tapeloc = conf_getVal($changer, "drivelocation");
59 Name = <?php echo "$changer\n"?>
61 for($i=0; $i<$ntapes; $i++) {
62 echo "Device = \"$changer-drive$i\"\n";
65 Changer Command = "/usr/local/bin/vchanger %c %o %S %a %d"
66 Changer Device = "<?php echo $changer ?>"
70 for($i=0; $i < $ntapes; $i++) {
73 Name = <?php echo "\"$changer-drive$i\"\n" ?>
74 DriveIndex = <?php echo $i ?>
78 ArchiveDevice = <?php echo "\"$tapeloc/$changer-drive$i\"\n" ?>
86 function pbdc_status()
88 global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
90 check_init_and_fail($argv[1]);
93 $db = db_getDB($argv[1]);
95 $res = $db->query("select * from disk_list");
97 $tapeloc = conf_getVal($changer, "drivelocation");
98 $amloc = conf_getVal($changer, "automountdir");
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";
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";
110 //echo "didnt match: $file\n";
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";
123 echo "drive $i is unloaded\n";
128 function list_disks()
130 global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
132 check_init_and_fail($argv[1]);
134 $db = db_getDB($argv[1]);
136 $res = $db->query("select * from disk_list");
139 foreach($res as $row) {
140 echo "Disk ".$row["disk_id"].": ".$row["disk_name"]."\n";
145 echo "No disks defined yet for this changer\n";
154 global $BASE_DATA, $BASE_LIB, $BACULA_USER, $argv;
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";
163 $changer_name = $argv[1];
165 echo "This command will init the data for $changer_name\n";
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]:");
171 echo "Ok, but its your disaster, waiting 5 seconds prior to init'ing the database (hit ctrl-c to exit)\n";
174 echo "Ok, not doing it, so long chum\n";
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/]:");
185 if($ra == "") $ra = "/changer/";
186 if($rb == "") $rb = 1;
187 if($rc == "") $rc = 20;
188 if($rd == "") $rd = "/var/run/bacula/";
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");
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";
199 function check_init_and_fail($changer)
201 $lk = conf_getVal($changer, "automountdir");
203 echo "DB not init'd yet, please run init first\n";
208 function check_init($changer)
210 $lk = conf_getVal($changer, "automountdir");
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;
224 check_init_and_fail($argv[1]);
226 $dh = opendir("/dev/disk/by-uuid");
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));
237 $fh = fopen("/sys/class/block/$realname/size", "r");
240 $realsize = (int)(((($rl/1024)*512)/1024)/1024);
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;
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]:");
257 echo "you said no, i bail\n";
261 echo "invalid selection\n";
265 // now we try and init the disk
266 // we have to get automount directory config
267 $dir = conf_getVal($argv[1], "automountdir");
269 echo "cant find automount directory, $dir\n";
271 $mkd = "$dir/".basename($disk[$i]["syml"])."/pbdc/".$argv[1]."/tapes/";
272 $k = mkdir($mkd, 0700, true);
275 echo "Error, couldn't create directory, check permissions on $mkd?\n";
278 $ts = conf_getVal($argv[1], "tapesize");
279 $max = (int)($disk[$i]["size"]/$ts);
281 $kt = (int)(readline("No of tapes to create [max:$max] @ ".$ts."gb each:"));
283 echo "Sorry, max number of tapes is $max, creating $max instead\n";
287 $did = add_diskToDB($argv[1], basename($disk[$i]["syml"]));
290 echo "Disk already exists, doing nothing\n";
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)) {
302 echo "created tape $tid\n";
304 echo "tape $tid already existed\n";