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
|
|
|
if ( ! empty( $assoc_args['destination'] ) ) { |
46
|
|
|
Path::get_instance()->set_path( $assoc_args['destination'] ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
Path::get_instance()->cleanup(); |
50
|
|
|
|
51
|
|
|
if ( ! empty( $assoc_args['root'] ) ) { |
52
|
|
|
Path::get_instance()->set_root( $assoc_args['root'] ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ( ( ! is_dir( Path::get_path() ) ) ) { |
56
|
|
|
\WP_CLI::error( __( 'Invalid backup path', 'backupwordpress' ) ); |
57
|
|
|
return false; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
if ( ! is_dir( Path::get_root() ) || ! is_readable( Path::get_root() ) ) { |
61
|
|
|
\WP_CLI::error( __( 'Invalid root path', 'backupwordpress' ) ); |
62
|
|
|
return false; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$filename = 'backup.zip'; |
66
|
|
|
|
67
|
|
|
if ( isset( $assoc_args['archive_filename'] ) ) { |
68
|
|
|
$filename = $assoc_args['archive_filename']; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$status = new Backup_Status( 'backup' ); |
72
|
|
|
$status->set_status_callback( function( $message ) { |
73
|
|
|
\WP_CLI::line( $message ); |
74
|
|
|
} ); |
75
|
|
|
|
76
|
|
|
if ( $status->is_running() ) { |
77
|
|
|
\WP_CLI::error( 'There\'s a backup already running' ); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$status->start( $filename, __( 'Starting backup...', 'backupwordpress' ) ); |
81
|
|
|
|
82
|
|
|
$backup = new Backup( $filename ); |
83
|
|
|
$backup->set_status( $status ); |
84
|
|
|
|
85
|
|
|
|
86
|
|
|
if ( ! empty( $assoc_args['files_only'] ) ) { |
87
|
|
|
$backup->set_type( 'file' ); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if ( ! empty( $assoc_args['database_only'] ) ) { |
91
|
|
|
$backup->set_type( 'database' ); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if ( ! empty( $assoc_args['excludes'] ) ) { |
95
|
|
|
$backup->set_excludes( new Excludes( $assoc_args['excludes'] ) ); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$backup->run(); |
99
|
|
|
|
100
|
|
|
if ( file_exists( $backup->get_backup_filepath() ) ) { |
101
|
|
|
\WP_CLI::success( __( 'Backup Complete: ', 'backupwordpress' ) . $backup->get_backup_filepath() ); |
102
|
|
|
} else { |
103
|
|
|
\WP_CLI::error( __( 'Backup Failed', 'backupwordpress' ) ); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$status->finish(); |
107
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
\WP_CLI::add_command( 'backupwordpress', 'HM\BackUpWordPress\CLI' ); |
112
|
|
|
|