1: <?php
2:
3: namespace Alpha\Util\Backup;
4:
5: use Alpha\Util\Config\ConfigProvider;
6: use Alpha\Util\File\FileUtils;
7:
8: /**
9: * A utility class for carrying out various backup tasks.
10: *
11: * @since 1.1
12: *
13: * @author John Collins <dev@alphaframework.org>
14: * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
15: * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
16: * All rights reserved.
17: *
18: * <pre>
19: * Redistribution and use in source and binary forms, with or
20: * without modification, are permitted provided that the
21: * following conditions are met:
22: *
23: * * Redistributions of source code must retain the above
24: * copyright notice, this list of conditions and the
25: * following disclaimer.
26: * * Redistributions in binary form must reproduce the above
27: * copyright notice, this list of conditions and the
28: * following disclaimer in the documentation and/or other
29: * materials provided with the distribution.
30: * * Neither the name of the Alpha Framework nor the names
31: * of its contributors may be used to endorse or promote
32: * products derived from this software without specific
33: * prior written permission.
34: *
35: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
36: * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
37: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
38: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39: * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
40: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
46: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
47: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
48: * </pre>
49: */
50: class BackupUtils
51: {
52: /**
53: * Backs up the attachments and logs directories to the destination backup directory.
54: *
55: * @param string $backupDir
56: *
57: * @since 1.1
58: */
59: public static function backUpAttachmentsAndLogs($backupDir)
60: {
61: $config = ConfigProvider::getInstance();
62:
63: FileUtils::copy($config->get('app.file.store.dir').'attachments', $backupDir.'attachments');
64: FileUtils::copy($config->get('app.file.store.dir').'logs', $backupDir.'logs');
65: }
66:
67: /**
68: * Uses the mysqldump command line program to back-up the system database into an .sql file in the supplied target directory.
69: *
70: * @param string $backupDir The directory where we will write the .sql back-up file
71: *
72: * @since 1.1
73: */
74: public static function backUpDatabase($backupDir)
75: {
76: $config = ConfigProvider::getInstance();
77:
78: $targetFileName = $backupDir.$config->get('db.name').'_'.date('Y-m-d').'.sql';
79:
80: exec('mysqldump --host="'.$config->get('db.hostname').'" --user="'.$config->get('db.username').'" --password="'.$config->get('db.password').'" --opt '.$config->get('db.name').' 2>&1 >'.$targetFileName);
81: }
82: }
83: