5 * This should allow you to dump a series of data, so it could be restored later...
6 * The format will be an SQL file...
9 * php index.php Admin/Dump --table=Project --where="id=123"
10 * --dump-dir=/directory_to_put_sql+shell files
12 * outputs list of generated files.
15 * {DATE}.sql - the recreate sql including all dependancies, run with mysql DB -f < ....
16 * {DATE}.restore.sh - shell script to recreate files that where removed (excluding thumgs)
19 * {DATE}.copy.sh - shell script that backs up all the related files.
22 * {DATE}.delete.sql - the delete data sql.
23 * {DATE}.delete.sh - shell script to delete the files related to these records
26 * Basically it has to output all the records and their dependants. (parent and children)
28 * Then when deleting it deletes record + children..
31 * Each ouput is simple insert statement..
34 * TODO - handle Images table (or similar) where we use tablename=XXXX, tid=.... etc..
39 * DataObjects->archivePaths() - returns array ( sourcedirectory, remainder of path to dependant file )
40 * DataObjects->listThumbs() - returns array ( list of full path to thumbnail urls. )
49 require_once 'Pman.php';
51 class Pman_Admin_Dump extends Pman {
56 if (!HTML_FlexyFramework::get()->cli) {
57 die("Access only permitted from cli");
62 var $deps = array(); // list of dependants
63 var $out = array(); // list of created sql/shell scripts.
67 $argv = $_SERVER['argv'];
71 $opts = explode(',', 'table==,where==,dump-dir==');
72 require_once 'Console/Getopt.php';
73 $go = Console_Getopt::getopt2($argv, '', $opts );
78 foreach($go[0] as $ar) {
79 $args[substr($ar[0],2)] = $ar[1];
82 foreach($opts as $req) {
83 if (empty($args[substr($req,0, -2)])) {
84 $errs[] = "--".substr($req,0, -2) . ' is required';
88 die(print_R($errs,true));
93 $this->discoverChildren($this->args['table'], $this->args['where'], true);
94 //print_R($this->deletes);
95 //print_r($this->dumps);
98 $this->discover($this->args['table'], $this->args['where'], true);
101 if (!file_exists($args['dump-dir'])) {
102 mkdir($args['dump-dir'], 0777, true);
105 $this->generateInsert();
106 $this->generateDelete();
107 $this->generateShell();
110 foreach($this->deletes as $tbl => $ar) {
111 echo $table . ' -> ' . count(array_keys($ar)) . "Records\n";
114 foreach($this->dumps $tbl => $ar) {
115 echo $table . ' -> ' . count(array_keys($ar)) . "Records\n";
117 echo "GENERATED FILES:";
128 var $deletes = array(); // TABLE => [key] => TRUE|FALSE
129 var $dumps = array(); // TABLE => [key] => TRUE|FALSE - if it's been scanned..
130 var $dscan = array(); // TABLE:COL => [value => TRUE|FALSE] - if its been scanned..
133 * a) what depends on it (eg. child elements) - which will be deleted.
134 * b) what it depends on it (eg. parent elements) - which will be dumped..
137 function discover($table, $where, $is_delete = false )
140 if (!isset($this->dumps[$table])) {
141 $this->dumps[$table] = array();
143 if ($is_delete && !isset($this->deletes[$table])) {
144 $this->deletes[$table] = array();
146 //DB_DataObject::debugLevel(1);
147 $x = DB_DataObject::factory($table);
148 if (PEAR::isError($x)) {
149 if (isset($this->dumps[$table])) {
150 unset($this->dumps[$table]); // links to non-existant tables..
157 if (is_array( $where)) {
158 $x->whereAddIn($keys[0] , $where, 'int');
161 $x->whereAdd($where);
164 // a) id's of elements in this table
165 // b) columns which point to other tables..
166 $links = $x->links();
167 $cols = array_keys($links);
169 array_push($cols, $keys[0]);
173 $x->selectAdd('`'. implode('`,`', $cols) . '`');
175 //DB_DataObject::debugLevel(0);
176 while ($x->fetch()) {
177 foreach($cols as $k) {
178 if (empty($x->$k)) { // skip blanks.
181 if (isset($links[$k])) {
183 $kv = explode(':', $links[$k]);
184 if (!isset($this->dumps[$kv[0]])) {
185 $this->dumps[$kv[0]] = array();
187 if (!isset($this->dumps[$kv[0]][$x->$k])) {
188 $this->dumps[$kv[0]][$x->$k] = 0; // not checked yet..
192 // assume it's the key..
193 if (empty($this->dumps[$table][$x->$k])) {
194 $this->dumps[$table][$x->$k] = 1; // we have checked this one...
196 //if ($is_delete && !isset($this->deletes[$table][$x->$k])) {
198 // $this->deletes[$table][$x->$k] = 0 ; // not checked yet..
205 // flag as checked if we where given an array.. - as some links might have been broken.
206 if (is_array($where)) {
207 foreach($where as $k) {
208 $this->dumps[$table][$k] = 1;
213 // itterate through dumps to find what needs discovering
214 foreach($this->dumps as $k=>$v) {
216 foreach($v as $id => $fetched) {
222 $this->discover($k, $ar,false);
234 function discoverChildren($table, $where, $col=false )
236 global $_DB_DATAOBJECT;
237 $do = DB_DataObject::factory($table);
238 if (PEAR::isError($do)) {
239 if (isset($this->dumps[$table])) {
240 unset($this->dumps[$table]); // links to non-existant tables..
244 if (!isset($this->dumps[$table])) {
245 $this->dumps[$table] = array();
247 if (!isset($this->deletes[$table])) {
248 $this->deletes[$table] = array();
254 if (is_array( $where)) {
255 $do->whereAddIn($col ? $col : $keys[0] , $where, 'int');
258 $do->whereAdd($where);
261 static $children = array();
263 if (!isset($children[$table])) {
265 // force load of linsk
267 foreach($_DB_DATAOBJECT['LINKS'][$do->database()] as $tbl => $links) {
268 // hack.. - we should get rid of this hack..
269 if ($tbl == 'database__render') {
272 //if ($tbl == $tn) { // skip same table
275 foreach ($links as $tk => $kv) {
278 list($k,$v) = explode(':', $kv);
282 $add = implode(':', array($tbl, $tk));
283 //echo "ADD $tbl $tk=>$kv : $add\n";
284 $children[$table][$add] = true;
290 if (empty($children[$table])) {
294 // DB_DataObject::debugLevel(1);
297 $do->selectAdd($key);
299 while ($do->fetch()) {
300 $this->dumps[$table][$do->$key] = 0;
301 if (!isset($this->deletes[$table][$do->$key])) {
302 $this->deletes[$table][$do->$key] = 0;
305 foreach($children[$table] as $kv=>$t) {
306 if (!isset($this->dscan[$kv])) {
307 $this->dscan[$kv] = array();
309 if (!isset($this->dscan[$kv][$do->$key])) {
310 $this->dscan[$kv][$do->$key]= 0; // unscanned.
316 // now iterate throught dependants. and scan them.
319 foreach($this->dscan as $kv => $ids) {
321 foreach($ids as $id => $checked) {
323 $this->dscan[$kv][$id] = 1; // flag it as checked.
332 list($k, $v) = explode(':', $kv);
333 $this->discoverChildren($k, $ar, $v);
342 function generateDelete() {
343 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.delete.sql';
344 $this->out[] = $target;
345 $fh = fopen($target, 'w');
346 foreach($this->deletes as $tbl=>$ar) {
347 $do = DB_DataObject::factory($tbl);
350 foreach($ar as $id => $deleted) {
351 fwrite($fh, "DELETE FROM `$tbl` WHERE `$key` = $id;\n"); // we assume id's and nice column names...
356 function generateShell() {
358 if (empty($this->childfiles)) {
361 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.copy.sh';
362 $this->out[] = $target;
363 $fh = fopen($target, 'w');
365 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.delete.sh';
366 $this->out[] = $target;
367 $fh2 = fopen($target, 'w');
369 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.restore.sh';
370 $this->out[] = $target;
371 $fh3 = fopen($target, 'w');
374 foreach($this->childfiles as $s=>$v) {
375 fwrite($fh,"mkdir -p " . escapeshellarg(dirname($args['dump-dir'] .'/'.$v[1])) ."\n" );
376 fwrite($fh,"cp " . escapeshellarg($v[0].'/'.$v[1]) . ' ' . escapeshellarg($args['dump-dir'] .'/'.$v[1]) ."\n" );
378 fwrite($fh3,"mkdir -p " . escapeshellarg(dirname($v[0].'/'.$v[1])) ."\n" );
379 fwrite($fh3,"cp " . escapeshellarg($args['dump-dir'] .'/'.$v[1]) . ' ' . escapeshellarg($v[0].'/'.$v[1]) . "\n" );
381 fwrite($fh2,"rm " . escapeshellarg($v[0].'/'.$v[1]) ."\n" );
384 fclose($fh3); // restore does not need to bother with thumbnails.
388 foreach($this->childthumbs as $s=>$v) {
390 fwrite($fh2,"rm " . escapeshellarg($vv). "\n");
396 function generateInsert()
398 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.sql';
399 $this->out[] = $target;
400 $fh = fopen($target,'w');
404 foreach($this->dumps as $tbl => $ar) {
408 $do = DB_DataObject::factory($tbl);
412 $do->whereAddIn($keys[0] , array_keys($ar), 'int');
414 while ($do->fetch()) {
415 fwrite($fh,$this->toInsert($do));
428 * toInsert - does not handle NULLS...
430 function toInsert($do)
432 $kcol = array_shift($do->keys());
434 // for auto_inc column we need to use a 'set argument'...
435 $items = $do->table();
442 $table = $do->tableName();
445 foreach( $items as $k=>$v)
452 $leftq .= '`' . $k . '`';
457 if ($v & DB_DATAOBJECT_STR) {
458 $rightq .= $do->_quote((string) (
459 ($v & DB_DATAOBJECT_BOOL) ?
460 // this is thanks to the braindead idea of postgres to
461 // use t/f for boolean.
462 (($do->$k === 'f') ? 0 : (int)(bool) $do->$k) :
467 if (is_numeric($do->$k)) {
468 $rightq .=" {$do->$k} ";
471 $rightq .= ' ' . intval($do->$k) . ' ';
474 return "INSERT INTO `{$table}` ($leftq) VALUES ($rightq);\n";