|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace HM\BackUpWordPress; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Implement backup command |
|
7
|
|
|
* |
|
8
|
|
|
* @todo fix |
|
9
|
|
|
* @package wp-cli |
|
10
|
|
|
* @subpackage commands/third-party |
|
11
|
|
|
*/ |
|
12
|
|
|
class CLI extends \WP_CLI_Command { |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Perform a Backup. |
|
16
|
|
|
* |
|
17
|
|
|
* ## OPTIONS |
|
18
|
|
|
* |
|
19
|
|
|
* [--files_only] |
|
20
|
|
|
* : Backup files only, default to off |
|
21
|
|
|
* |
|
22
|
|
|
* [--database_only] |
|
23
|
|
|
* : Backup database only, defaults to off |
|
24
|
|
|
* |
|
25
|
|
|
* [--destination] |
|
26
|
|
|
* : dir that the backup should be save in, defaults to your existing backups directory |
|
27
|
|
|
* |
|
28
|
|
|
* [--root] |
|
29
|
|
|
* : dir that should be backed up, defaults to site root. |
|
30
|
|
|
* |
|
31
|
|
|
* [--archive_filename] |
|
32
|
|
|
* : filename for the resulting zip file |
|
33
|
|
|
* |
|
34
|
|
|
* [--excludes] |
|
35
|
|
|
* : list of paths you'd like to exclude |
|
36
|
|
|
* |
|
37
|
|
|
* ## Usage |
|
38
|
|
|
* |
|
39
|
|
|
* wp backupwordpress backup [--files_only] [--database_only] [--path<dir>] [--root<dir>] [--zip_command_path=<path>] [--mysqldump_command_path=<path>] |
|
40
|
|
|
* |
|
41
|
|
|
* @todo errors should be bubbled from Backup, Scheduled_Backup and the like instead of being repeated. |
|
42
|
|
|
*/ |
|
43
|
|
|
public function backup( $args, $assoc_args ) { |
|
44
|
|
|
|
|
45
|
|
|
add_action( 'hmbkp_mysqldump_started', function () { |
|
46
|
|
|
\WP_CLI::line( __( 'Backup: Dumping database...', 'backupwordpress' ) ); |
|
47
|
|
|
} ); |
|
48
|
|
|
|
|
49
|
|
|
add_action( 'hmbkp_archive_started', function () { |
|
50
|
|
|
\WP_CLI::line( __( 'Backup: Zipping everything up...', 'backupwordpress' ) ); |
|
51
|
|
|
} ); |
|
52
|
|
|
|
|
53
|
|
|
if ( ! empty( $assoc_args['destination'] ) ) { |
|
54
|
|
|
Path::get_instance()->set_path( $assoc_args['destination'] ); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
Path::get_instance()->cleanup(); |
|
58
|
|
|
|
|
59
|
|
|
if ( ! empty( $assoc_args['root'] ) ) { |
|
60
|
|
|
Path::get_instance()->set_root( $assoc_args['root'] ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
if ( ( ! is_dir( Path::get_path() ) ) ) { |
|
64
|
|
|
\WP_CLI::error( __( 'Invalid backup path', 'backupwordpress' ) ); |
|
65
|
|
|
return false; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ( ! is_dir( Path::get_root() ) || ! is_readable( Path::get_root() ) ) { |
|
69
|
|
|
\WP_CLI::error( __( 'Invalid root path', 'backupwordpress' ) ); |
|
70
|
|
|
return false; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
$filename = 'backup.zip'; |
|
74
|
|
|
|
|
75
|
|
|
if ( isset( $assoc_args['archive_filename'] ) ) { |
|
76
|
|
|
$filename = $assoc_args['archive_filename']; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$hm_backup = new Backup( $filename ); |
|
80
|
|
|
|
|
81
|
|
|
if ( ! empty( $assoc_args['files_only'] ) ) { |
|
82
|
|
|
$hm_backup->set_type( 'file' ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ( ! empty( $assoc_args['database_only'] ) ) { |
|
86
|
|
|
$hm_backup->set_type( 'database' ); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
if ( ! empty( $assoc_args['excludes'] ) ) { |
|
90
|
|
|
$hm_backup->set_excludes( new Excludes( $assoc_args['excludes'] ) ); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
$hm_backup->run(); |
|
94
|
|
|
|
|
95
|
|
|
if ( file_exists( $hm_backup->get_backup_filepath() ) ) { |
|
96
|
|
|
\WP_CLI::success( __( 'Backup Complete: ', 'backupwordpress' ) . $hm_backup->get_backup_filepath() ); |
|
97
|
|
|
} else { |
|
98
|
|
|
\WP_CLI::error( __( 'Backup Failed', 'backupwordpress' ) ); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
\WP_CLI::add_command( 'backupwordpress', 'HM\BackUpWordPress\CLI' ); |
|
105
|
|
|
|