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