]> git.andy128k.dev Git - ipf.git/commitdiff
dbrestore & unpack commands
authorAndrey Kutejko <andy128k@gmail.com>
Sat, 22 Jun 2013 17:26:29 +0000 (20:26 +0300)
committerAndrey Kutejko <andy128k@gmail.com>
Sat, 22 Jun 2013 17:26:29 +0000 (20:26 +0300)
ipf/cli.php
ipf/command/dbdump.php
ipf/command/dbrestore.php [new file with mode: 0644]
ipf/command/pack.php
ipf/command/unpack.php [new file with mode: 0644]
ipf/shell.php

index dd0efea31df01cf8f90fa102f06bdadbb34b9adf..7aad2985de7dc0ede6a90ae1d879115b47d60b85 100644 (file)
@@ -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,
         );
index 04d8d927c08b0215a7ccfa7d38951d75ff4fd01c..9bbb95540cd68ae54c1862301894d4dd43a16ffe 100644 (file)
@@ -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 (file)
index 0000000..a05a46e
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+class IPF_Command_DBRestore
+{
+    public $command = 'dbrestore';
+    public $description = 'Restores database from a file';
+
+    public function run($args=null)
+    {
+        $input = 'dump.sql';
+
+        if (!in_array('--quiet', $args))
+            print "Restoring database from file $input\n";
+
+        $db = IPF_ORM_Manager::getInstance()->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.';
+        }
+    }
+}
+
index 74cc899ae1377a0d4ac0f4557880bdac2c656625..b5a1e27cd0fe9b0d293288f4c20a525eada48350 100644 (file)
@@ -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 (file)
index 0000000..7d4453a
--- /dev/null
@@ -0,0 +1,30 @@
+<?php
+
+class IPF_Command_Unpack
+{
+    public $command = 'unpack';
+    public $description = 'Unpack database dump and uploaded files from an archive';
+
+    public function run($args=null)
+    {
+        $inputFileName = 'working-data.tar';
+
+        if (is_dir($inputFileName)) {
+            print 'Error. File "'.$inputFileName.'" was not found.';
+            return;
+        }
+
+        IPF_Shell::unlink('upload.tar');
+        IPF_Shell::unlink('dump.sql');
+
+        (new Archive_Tar($inputFileName))->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');
+    }
+}
+
index 5ce5e2f968bf24cf0cac9bb1895cb08442bc8da7..c543e821d87d61732ccaa24ab35a835491bac669 100644 (file)
@@ -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;