From: Andrey Kutejko Date: Sat, 22 Jun 2013 17:26:29 +0000 (+0300) Subject: dbrestore & unpack commands X-Git-Tag: 0.5~211 X-Git-Url: https://git.andy128k.dev/?a=commitdiff_plain;h=e0605582613a3f3c9b867f1e68203f4a117ea62b;p=ipf.git dbrestore & unpack commands --- diff --git a/ipf/cli.php b/ipf/cli.php index dd0efea..7aad298 100644 --- a/ipf/cli.php +++ b/ipf/cli.php @@ -16,7 +16,9 @@ class IPF_Cli new IPF_Command_Fixtures, new IPF_Command_DB, new IPF_Command_DBDump, + new IPF_Command_DBRestore, new IPF_Command_Pack, + new IPF_Command_Unpack, new IPF_Command_CreateSuperUser, new IPF_Command_SyncPerms, ); diff --git a/ipf/command/dbdump.php b/ipf/command/dbdump.php index 04d8d92..9bbb955 100644 --- a/ipf/command/dbdump.php +++ b/ipf/command/dbdump.php @@ -9,7 +9,7 @@ class IPF_Command_DBDump { $output = 'dump.sql'; - if (!in_array('quiet', $args)) + if (!in_array('--quiet', $args)) print "Dumping database to file $output\n"; $db = IPF_ORM_Manager::getInstance()->connectionParameters(IPF::get('database', IPF::get('dsn'))); diff --git a/ipf/command/dbrestore.php b/ipf/command/dbrestore.php new file mode 100644 index 0000000..a05a46e --- /dev/null +++ b/ipf/command/dbrestore.php @@ -0,0 +1,30 @@ +connectionParameters(IPF::get('database', IPF::get('dsn'))); + + if ($db['scheme'] === 'mysql') { + IPF_Shell::call('mysql', + '-h'.$db['host'], + '-u'.$db['username'], + '-p'.$db['password'], + '-e', + 'source '.$input, + $db['database']); + } else { + print 'Do not know how to connect to "'.$db['scheme'].'" database.'; + } + } +} + diff --git a/ipf/command/pack.php b/ipf/command/pack.php index 74cc899..b5a1e27 100644 --- a/ipf/command/pack.php +++ b/ipf/command/pack.php @@ -3,7 +3,7 @@ class IPF_Command_Pack { public $command = 'pack'; - public $description = 'Pack database dump and uploaded files to a single archive.'; + public $description = 'Pack database dump and uploaded files to a single archive'; public function run($args=null) { @@ -14,21 +14,21 @@ class IPF_Command_Pack $workingDirectory = getcwd(); chdir($uploadsDir . '/..'); $tar_object = new Archive_Tar($workingDirectory . '/upload.tar'); - $tar_object->setErrorHandling(PEAR_ERROR_PRINT); + $tar_object->setErrorHandling(PEAR_ERROR_PRINT); $tar_object->create('upload'); chdir($workingDirectory); } - (new IPF_Command_DBDump)->run(array('quiet')); + (new IPF_Command_DBDump)->run(array('--quiet')); - unlink($outputFileName); + IPF_Shell::unlink($outputFileName); $tar_object = new Archive_Tar($outputFileName); - $tar_object->setErrorHandling(PEAR_ERROR_PRINT); + $tar_object->setErrorHandling(PEAR_ERROR_PRINT); $tar_object->create('upload.tar dump.sql'); - unlink('upload.tar'); - unlink('dump.sql'); + IPF_Shell::unlink('upload.tar'); + IPF_Shell::unlink('dump.sql'); } } diff --git a/ipf/command/unpack.php b/ipf/command/unpack.php new file mode 100644 index 0000000..7d4453a --- /dev/null +++ b/ipf/command/unpack.php @@ -0,0 +1,30 @@ +extract('.'); + + (new IPF_Command_DBRestore)->run(array('--quiet')); + IPF_Shell::unlink('dump.sql'); + + $uploadsDir = IPF::get('document_root') . IPF::getUploadUrl(); + (new Archive_Tar('upload.tar'))->extract($uploadsDir . '/..'); + IPF_Shell::unlink('upload.tar'); + } +} + diff --git a/ipf/shell.php b/ipf/shell.php index 5ce5e2f..c543e82 100644 --- a/ipf/shell.php +++ b/ipf/shell.php @@ -22,6 +22,12 @@ class IPF_Shell proc_close($process); } + public static function unlink($filename) + { + if (is_file($filename)) + unlink($filename); + } + public static function displayTwoColumns($rows, $firstColumnMin=7, $firstColumnMax=47) { $firstColumnSize = $firstColumnMin;