|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HM\BackUpWordPress; |
|
4
|
|
|
|
|
5
|
|
|
class Site_Backup { |
|
6
|
|
|
|
|
7
|
|
|
private $excludes; |
|
8
|
|
|
public $warnings = array(); |
|
9
|
|
|
public $errors = array(); |
|
10
|
|
|
private $backup_filename; |
|
11
|
|
|
private $database_dump_filename; |
|
12
|
|
|
private $backup_filepath = ''; |
|
13
|
|
|
private $database_dump_filepath = ''; |
|
14
|
|
|
private $status = null; |
|
15
|
|
|
private $type = 'complete'; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct( $backup_filename, $database_dump_filename = null ) { |
|
18
|
|
|
$this->backup_filename = $backup_filename; |
|
19
|
|
|
$this->database_dump_filename = $database_dump_filename; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function set_type( $type ) { |
|
23
|
|
|
$this->type = $type; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public function set_backup_filename( $filename ) { |
|
27
|
|
|
$this->backup_filename = $filename; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
public function set_status( Backup_Status $status ) { |
|
31
|
|
|
$this->status = $status; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
public function set_excludes( Excludes $excludes ) { |
|
35
|
|
|
$this->excludes = $excludes; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function run() { |
|
39
|
|
|
|
|
40
|
|
|
if ( $this->type !== 'file' ) { |
|
41
|
|
|
$this->backup_database(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
if ( $this->type !== 'database' ) { |
|
45
|
|
|
$this->backup_files(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
public function backup_database() { |
|
51
|
|
|
|
|
52
|
|
|
if ( $this->status ) { |
|
53
|
|
|
$this->status->set_status( __( 'Backing up database...', 'backupwordpress' ) ); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
$database_backup_engines = apply_filters( 'hmbkp_database_backup_engines', array( |
|
57
|
|
|
new Mysqldump_Database_Backup_Engine, |
|
58
|
|
|
new IMysqldump_Database_Backup_Engine |
|
59
|
|
|
) ); |
|
60
|
|
|
|
|
61
|
|
|
// Set the file backup engine settings |
|
62
|
|
|
if ( $this->database_dump_filename ) { |
|
63
|
|
|
foreach( $database_backup_engines as &$backup_engine ) { |
|
64
|
|
|
$backup_engine->set_backup_filename( $this->database_dump_filename ); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
// Dump the database |
|
69
|
|
|
$database_dump = $this->perform_backup( $database_backup_engines ); |
|
70
|
|
|
|
|
71
|
|
|
if ( is_a( $database_dump, __NAMESPACE__ . '\\Backup_Engine' ) ) { |
|
72
|
|
|
$this->database_dump_filepath = $database_dump->get_backup_filepath(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Fire up the file backup engines |
|
76
|
|
|
$file_backup_engines = apply_filters( 'hmbkp_file_backup_engines', array( |
|
77
|
|
|
new Zip_File_Backup_Engine, |
|
78
|
|
|
new Zip_Archive_File_Backup_Engine |
|
79
|
|
|
) ); |
|
80
|
|
|
|
|
81
|
|
|
// Set the file backup engine settings |
|
82
|
|
|
foreach( $file_backup_engines as &$backup_engine ) { |
|
83
|
|
|
$backup_engine->set_backup_filename( $this->backup_filename ); |
|
84
|
|
|
$backup_engine->set_excludes( new Excludes( array( '*.zip', 'index.html', '.htaccess', '.*-running' ) ) ); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
// Zip up the database dump |
|
88
|
|
|
$root = Path::get_root(); |
|
89
|
|
|
Path::get_instance()->set_root( Path::get_path() ); |
|
90
|
|
|
$file_backup = $this->perform_backup( $file_backup_engines ); |
|
91
|
|
|
Path::get_instance()->set_root( $root ); |
|
92
|
|
|
|
|
93
|
|
|
if ( is_a( $file_backup, __NAMESPACE__ . '\\Backup_Engine' ) ) { |
|
94
|
|
|
$this->backup_filepath = $file_backup->get_backup_filepath(); |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Delete the Database Backup now that we've zipped it up |
|
98
|
|
|
if ( file_exists( $this->database_dump_filepath ) ) { |
|
99
|
|
|
unlink( $this->database_dump_filepath ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function backup_files() { |
|
105
|
|
|
|
|
106
|
|
|
if ( $this->status ) { |
|
107
|
|
|
$this->status->set_status( __( 'Backing up files...', 'backupwordpress' ) ); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
// Fire up the file backup engines |
|
111
|
|
|
$backup_engines = apply_filters( 'hmbkp_file_backup_engines', array( |
|
112
|
|
|
new Zip_File_Backup_Engine, |
|
113
|
|
|
new Zip_Archive_File_Backup_Engine |
|
114
|
|
|
) ); |
|
115
|
|
|
|
|
116
|
|
|
// Set the file backup engine settings |
|
117
|
|
|
foreach( $backup_engines as &$backup_engine ) { |
|
118
|
|
|
$backup_engine->set_backup_filename( $this->backup_filename ); |
|
119
|
|
|
if ( $this->excludes ) { |
|
120
|
|
|
$backup_engine->set_excludes( $this->excludes ); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
$file_backup = $this->perform_backup( $backup_engines ); |
|
125
|
|
|
|
|
126
|
|
|
if ( is_a( $file_backup, __NAMESPACE__ . '\\Backup_Engine' ) ) { |
|
127
|
|
|
$this->backup_filepath = $file_backup->get_backup_filepath(); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Perform the backup by iterating through each Backup_Engine in turn until |
|
134
|
|
|
* we find one which works. If a backup filename or any excludes have been |
|
135
|
|
|
* set then those are passed to each Backup_Engine. |
|
136
|
|
|
*/ |
|
137
|
|
|
public function perform_backup( Array $backup_engines ) { |
|
|
|
|
|
|
138
|
|
|
|
|
139
|
|
|
foreach ( $backup_engines as $backup_engine ) { |
|
140
|
|
|
|
|
141
|
|
|
if ( $backup_engine->backup() ) { |
|
142
|
|
|
$this->warnings = array_merge( $this->warnings, $backup_engine->get_warnings() ); |
|
143
|
|
|
return $backup_engine; |
|
144
|
|
|
} |
|
145
|
|
|
$this->warnings = array_merge( $this->warnings, $backup_engine->get_warnings() ); |
|
146
|
|
|
$this->errors = array_merge( $this->errors, $backup_engine->get_errors() ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return false; |
|
150
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
public function get_warnings() { |
|
154
|
|
|
return $this->warnings; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
public function get_errors() { |
|
158
|
|
|
return $this->errors; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Add an warning to the errors warnings. |
|
163
|
|
|
* |
|
164
|
|
|
* A warning is always treat as non-fatal and should only be used for recoverable |
|
165
|
|
|
* issues with the backup process. |
|
166
|
|
|
* |
|
167
|
|
|
* @param string $context The context for the warning. |
|
168
|
|
|
* @param string $error The warning that was encountered. |
|
|
|
|
|
|
169
|
|
|
*/ |
|
170
|
|
View Code Duplication |
public function warning( $context, $warning ) { |
|
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
if ( empty( $context ) || empty( $warning ) ) { |
|
173
|
|
|
return; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
// Ensure we don't store duplicate warnings by md5'ing the error as the key |
|
177
|
|
|
$this->warnings[ $context ][ $_key = md5( implode( ':', (array) $warning ) ) ] = $warning; |
|
178
|
|
|
|
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
|
|
public function get_database_backup_filepath() { |
|
182
|
|
|
return $this->database_dump_filepath; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
public function get_backup_filepath() { |
|
186
|
|
|
return $this->backup_filepath; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
} |
|
190
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.