      1: <?php
      2:
      3: /**
      4:  * A cron task for backup up the system database and select folders
      5:  *
      6:  * @package alpha::tasks
      7:  * @since 1.1
      8:  * @author John Collins <dev@alphaframework.org>
      9:  * @version $Id: BackupTask.inc 1672 2013-10-15 20:34:29Z alphadevx $
     10:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     11:  * @copyright Copyright (c) 2013, John Collins (founder of Alpha Framework).
     12:  * All rights reserved.
     13:  *
     14:  * <pre>
     15:  * Redistribution and use in source and binary forms, with or
     16:  * without modification, are permitted provided that the
     17:  * following conditions are met:
     18:  *
     19:  * * Redistributions of source code must retain the above
     20:  *   copyright notice, this list of conditions and the
     21:  *   following disclaimer.
     22:  * * Redistributions in binary form must reproduce the above
     23:  *   copyright notice, this list of conditions and the
     24:  *   following disclaimer in the documentation and/or other
     25:  *   materials provided with the distribution.
     26:  * * Neither the name of the Alpha Framework nor the names
     27:  *   of its contributors may be used to endorse or promote
     28:  *   products derived from this software without specific
     29:  *   prior written permission.
     30:  *
     31:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     32:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     33:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     34:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     35:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     36:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     37:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     39:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     41:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     42:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     43:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     44:  * </pre>
     45:  *
     46:  */
     47: class BackupTask implements AlphaTaskInterface {
     48:     /**
     49:      * Trace logger
     50:      *
     51:      * @var Logger
     52:      */
     53:     private static $logger = null;
     54:
     55:     /**
     56:      * (non-PHPdoc)
     57:      * @see alpha/util/AlphaTaskInterface#doTask()
     58:      */
     59:     public function doTask() {
     60:         global $config;
     61:
     62:         self::$logger = new Logger('BackupTask');
     63:         self::$logger->setLogFile($config->get('app.file.store.dir').'logs/tasks.log');
     64:
     65:         if(!file_exists($config->get('backup.dir')))
     66:             mkdir($config->get('backup.dir'));
     67:
     68:         $targetDir = $config->get('backup.dir').date("Y-m-d").'/';
     69:
     70:         if(file_exists($targetDir))
     71:             AlphaFileUtils::deleteDirectoryContents($targetDir);
     72:
     73:         if(!file_exists($targetDir))
     74:             mkdir($targetDir);
     75:
     76:         $back = new AlphaBackupUtils();
     77:         $back->backUpAttachmentsAndLogs($targetDir);
     78:         $back->backUpDatabase($targetDir);
     79:
     80:         $additionalDirectories = explode(',', $config->get('backup.include.dirs'));
     81:
     82:         if(count($additionalDirectories) > 0) {
     83:             foreach($additionalDirectories as $additionalDirectory)
     84:                 AlphaFileUtils::copy($additionalDirectory, $targetDir.basename($additionalDirectory));
     85:         }
     86:
     87:         if($config->get('backup.compress')) {
     88:             AlphaFileUtils::zip($targetDir, $targetDir.date("Y-m-d").'.zip');
     89:
     90:             // we can safely remove the uncompressed files now to save space...
     91:             AlphaFileUtils::deleteDirectoryContents($targetDir.'logs');
     92:             rmdir($targetDir.'logs');
     93:
     94:             AlphaFileUtils::deleteDirectoryContents($targetDir.'attachments');
     95:             rmdir($targetDir.'attachments');
     96:
     97:             unlink($targetDir.$config->get('db.name').'_'.date("Y-m-d").'.sql');
     98:
     99:             if(count($additionalDirectories) > 0) {
    100:                 foreach($additionalDirectories as $additionalDirectory) {
    101:                     AlphaFileUtils::deleteDirectoryContents($targetDir.basename($additionalDirectory));
    102:                     rmdir($targetDir.basename($additionalDirectory));
    103:                 }
    104:             }
    105:         }
    106:     }
    107:
    108:     /**
    109:      * (non-PHPdoc)
    110:      * @see alpha/util/AlphaTaskInterface#getMaxRunTime()
    111:      */
    112:     public function getMaxRunTime() {
    113:         return 180;
    114:     }
    115: }
    116:
    117: ?>