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_Export extends Pman_Admin_Dump {
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.
69 ini_set('memory_limit', '256M'); // we need alot of memory
72 $argv = $_SERVER['argv'];
76 $opts = explode(',', 'table==,where==,dump-dir==,debug=');
77 require_once 'Console/Getopt.php';
78 $go = Console_Getopt::getopt2($argv, '', $opts );
83 foreach($go[0] as $ar) {
84 $args[substr($ar[0],2)] = $ar[1];
87 foreach($opts as $req) {
88 if (substr($req,-2, 2) != '==') { // skip optional arguments
91 if (empty($args[substr($req,0, -2)])) {
92 $errs[] = "--".substr($req,0, -2) . ' is required';
96 die(print_R($errs,true));
98 if (!empty($args['debug'])) {
99 DB_DataObject::debugLevel($args['debug']);
102 $this->out = array();
112 $this->discoverChildren($this->args['table'], $this->args['where'], true);
113 //print_R($this->deletes);
114 //print_r($this->dumps);
117 $this->discover($this->args['table'], $this->args['where'], true);
120 if (!file_exists($args['dump-dir'])) {
121 mkdir($args['dump-dir'], 0777, true);
131 echo "GENERATED FILES:\n";
133 echo " ". implode("\n ", $this->out). "\n";
142 var $deletes = array(); // TABLE => [key] => TRUE|FALSE
143 var $dumps = array(); // TABLE => [key] => TRUE|FALSE - if it's been scanned..
144 var $dscan = array(); // TABLE:COL => [value => TRUE|FALSE] - if its been scanned..
145 var $childfiles = array(); // array of [ 'sourcedirectory' , 'subdirectory(s) and filename' ]
146 var $childthumbs = array(); // array of [ 'filename', 'filename' ,......]
147 var $filesize = 0; // size of files to be saved. (not total deletd..)
148 var $filetotal = 0; // number of distinct files to be saved (not total deleted)
151 * a) what depends on it (eg. child elements) - which will be deleted.
152 * b) what it depends on it (eg. parent elements) - which will be dumped..
157 function generateDelete() {
158 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.delete.sql';
159 $this->out[] = $target;
160 $fh = fopen($target, 'w');
164 foreach($this->deletes as $tbl=>$ar) {
166 $do = DB_DataObject::factory($tbl);
167 $tbl = $do->tableName();
170 $do->whereAddIn($keys[0] , array_keys($ar), 'int');
172 $archivePaths = method_exists($do,'archivePaths');
173 $listThumbs = method_exists($do,'listThumbs');
174 while ($do->fetch()) {
177 $ct = $do->archivePaths();
179 $this->childfiles[] = $ct;
183 $ct = $do->listThumbs();
185 $this->childthumbs[] = $ct;
190 fwrite($fh, "DELETE FROM `$tbl` WHERE `$key` = $id;\n"); // we assume id's and nice column names...
196 function generateShell() {
198 if (empty($this->childfiles) && empty($this->childthumbs)) {
201 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.copy.sh';
202 $this->out[] = $target;
203 $fh = fopen($target, 'w');
205 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.delete.sh';
206 $this->out[] = $target;
207 $fh2 = fopen($target, 'w');
209 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.restore.sh';
210 $this->out[] = $target;
211 $fh3 = fopen($target, 'w');
216 foreach($this->childfiles as $v) {
218 if (isset($done[$v[1]])) {
224 $this->filesize += filesize($v[0].'/'.$v[1]);
226 $fdir = dirname($this->args['dump-dir'] .'/'.$v[1]);
227 if (!isset($donedir[$fdir])) {
228 fwrite($fh,"mkdir -p " . escapeshellarg(dirname($this->args['dump-dir'] .'/'.$v[1])) ."\n" );
230 fwrite($fh,"cp " . escapeshellarg($v[0].'/'.$v[1]) . ' ' . escapeshellarg($this->args['dump-dir'] .'/'.$v[1]) ."\n" );
231 if (!isset($donedir[$fdir])) {
232 fwrite($fh3,"mkdir -p " . escapeshellarg(dirname($v[0].'/'.$v[1])) ."\n" );
234 $donedir[$fdir] = true;
236 fwrite($fh3,"cp " . escapeshellarg($this->args['dump-dir'] .'/'.$v[1]) . ' ' . escapeshellarg($v[0].'/'.$v[1]) . "\n" );
237 fwrite($fh2,"rm " . escapeshellarg($v[0].'/'.$v[1]) ."\n" );
240 fclose($fh3); // restore does not need to bother with thumbnails.
244 foreach($this->childthumbs as $v) {
246 fwrite($fh2,"rm " . escapeshellarg($vv). "\n");
252 function generateInsert()
254 $target = $this->args['dump-dir'] .'/'. date('Y-m-d').'.sql';
255 $this->out[] = $target;
256 $fh = fopen($target,'w');
260 foreach($this->dumps as $tbl => $ar) {
264 $do = DB_DataObject::factory($tbl);
268 $do->whereAddIn($keys[0] , array_keys($ar), 'int');
270 while ($do->fetch()) {
271 fwrite($fh,$this->toInsert($do));
284 * toInsert - does not handle NULLS...
286 function toInsert($do)
288 $kcol = array_shift($do->keys());
290 // for auto_inc column we need to use a 'set argument'...
291 $items = $do->table();
298 $table = $do->tableName();
301 foreach( $items as $k=>$v)
308 $leftq .= '`' . $k . '`';
313 if ($v & DB_DATAOBJECT_STR) {
314 $rightq .= $do->_quote((string) (
315 ($v & DB_DATAOBJECT_BOOL) ?
316 // this is thanks to the braindead idea of postgres to
317 // use t/f for boolean.
318 (($do->$k === 'f') ? 0 : (int)(bool) $do->$k) :
323 if (is_numeric($do->$k)) {
324 $rightq .=" {$do->$k} ";
327 $rightq .= ' ' . intval($do->$k) . ' ';
330 return "INSERT INTO `{$table}` ($leftq) VALUES ($rightq);\n";