Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • BackupTask
  • CronManager

Interfaces

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