-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdb-checkpoint.php
560 lines (490 loc) · 12.8 KB
/
db-checkpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
<?php
/**
* Plugin Name: DB Snapshot
* Plugin URI: https://www.binarygary.com/
* Description: Extends WP-CLI to include a db snapshot for development purposes.
* Version: 0.2.2
* Author: Gary Kovar
* Author URI: https://www.binarygary.com/
* Donate link: https://bethematch.org/
* License: GPLv2
* Text Domain: db-snapshot
* Domain Path: /languages
*
* @link https://www.binarygary.com/
*
* @package DB Snapshot
*/
/**
* Copyright (c) 2016 Gary Kovar (email : plugins@binarygary.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( defined( 'WP_CLI' ) && WP_CLI ) {
if ( ! class_exists( 'DB_CheckPoint' ) ) {
class DB_CheckPoint extends WP_CLI_Command {
public function check_requirements() {
$upload_dir = wp_upload_dir();
if ( ! file_exists( $upload_dir['basedir'] . '/checkpoint-storage' ) ) {
mkdir( $upload_dir['basedir'] . '/checkpoint-storage' );
}
return true;
}
/**
* Returns the array of configuration setup info for dbsnap command.
*
* @author Gary Kovar
*
* @since 0.1.0
*
* @return array
*
*/
public function get_checkpoint_save_args() {
return array(
'shortdesc' => 'Creates a simple checkpoint image of the database.',
'synopsis' => array(
array(
'type' => 'positional',
'name' => 'name',
'optional' => true,
'multiple' => false,
),
),
'when' => 'after_wp_load',
);
}
/**
* Returns the array of configuration setup info for dbsnapback command.
*
* @author Gary Kovar
*
* @since 0.1.0
*
* @return array
*
*/
public function get_checkpoint_restore_args() {
return array(
'shortdesc' => 'Restores the checkpoint image of the database.',
'synopsis' => array(
array(
'type' => 'positional',
'name' => 'name',
'optional' => true,
'multiple' => false,
),
array(
'type' => 'flag',
'name' => 'dumplog',
'optional' => true,
'default' => false,
),
array(
'type' => 'flag',
'name' => 'dev',
'optional' => true,
'default' => false,
),
),
'when' => 'after_wp_load',
);
}
public function get_install_plugin_args() {
return array(
'shortdesc' => 'Installs a plugin for 1-click restore from the admin bar..',
'synopsis' => array(),
'when' => 'after_wp_load',
);
}
/**
* Saves a checkpoint of the db.
*
* @author Gary Kovar
*
* @since 0.1.0
*/
public function checkpoint_save( $args ) {
if ( ! $this->check_requirements() ) {
exit;
}
$snapshot_name = $this->get_snapshot_name( $args );
$this->maybe_nuke_checkpoints( $snapshot_name );
$upload_dir = wp_upload_dir();
$location = $upload_dir['basedir'] . '/checkpoint-storage/' . $snapshot_name . '.' . $this->human_timestamp() . '.sql';
$args[0] = $location;
$db = new DB_Command;
$db->export( $args, null );
WP_CLI::success( "Checkpoint Saved!" );
}
/**
* Restores the most recent checkpoint of the db.
*
* @author Gary Kovar
*
* @since 0.1.0
*/
public function checkpoint_restore( $args, $assoc_args ) {
if ( ! $this->check_requirements() ) {
exit;
}
// Check if the dev flag is set to true OR site is *.dev, otherwise prompt.
if ( ! $this->is_dev() || ( key_exists( 'dev', $assoc_args ) && ! $assoc_args['dev'] ) ) {
WP_CLI::confirm( "This is a destructive operation, are you sure?" );
}
$snapshot_name = $this->get_snapshot_name( $args );
$upload_dir = wp_upload_dir();
if ( $restore_file = $this->get_most_recent_file( $snapshot_name ) ) {
$location = $upload_dir['basedir'] . '/checkpoint-storage/' . $this->get_most_recent_file( $snapshot_name );
} else {
WP_CLI::error( 'No checkpoint found associated with ' . $snapshot_name );
}
$args[0] = $location;
$db = new DB_Command;
$db->reset( $args, null );
$db->import( $args, null );
// If the dumplog flag is set, clear the log file.
if ( key_exists( 'dumplog', $assoc_args ) && $assoc_args['dumplog'] ) {
$this->dump_log();
}
WP_CLI::success( "Checkpoint Restored!" );
}
/**
* Delete the debug.log.
*
* @author Gary Kovar
*
* @since 0.2.2
*
* @return null
*/
public function dump_log() {
if (file_exists(WP_CONTENT_DIR . '/debug.log' ) ){
unlink( WP_CONTENT_DIR . '/debug.log' );
}
WP_CLI::success( "debug.log removed" );
}
/**
* Check if this is a .dev site.
*
* @author Gary Kovar
*
* @since 0.2.2
*/
public function is_dev() {
if ( '.dev' === substr(get_site_url(),-4)){
return true;
}
return false;
}
/**
* Get the name of the most recent backup file.
*
* @author Gary Kovar
*
* @since 0.1.0
*
* @param $backup_name
*
* @return bool
*/
public function get_most_recent_file( $backup_name ) {
$upload_dir = wp_upload_dir();
$backupsdir = scandir( $upload_dir['basedir'] . '/checkpoint-storage/', SCANDIR_SORT_DESCENDING );
foreach ( $backupsdir as $backup ) {
if ( strpos( $backup, $backup_name ) === 0 ) {
return $backup;
}
}
return false;
}
/**
* Figure out what name to use with this file.
*
* @author Gary Kovar
*
* @since 0.1.0
*
* @param $args
*
* @return string
*/
public function get_snapshot_name( $args ) {
if ( key_exists( 0, $args ) ) {
return $args[0];
}
return sanitize_title( get_option( 'blogname', 'shruggy' ) );
}
/**
* Check to see if the checkpoint name matches the site name, if so remove any checkpoints of the same name.
*
* @author Gary Kovar
*
* @since 0.1.0
*
* @param $checkpoint_name
*/
public function maybe_nuke_checkpoints( $checkpoint_name ) {
$this->nuke_checkpoints( $checkpoint_name );
}
/**
* Deletes all previous checkpoints under the same name.
*
* @author Gary Kovar
*
* @since 0.1.0
*
* @param $checkpoint_name
*/
public function nuke_checkpoints( $checkpoint_name ) {
$upload_dir = wp_upload_dir();
$backupsdir = scandir( $upload_dir['basedir'] . '/checkpoint-storage/', SCANDIR_SORT_DESCENDING );
// Make sure we have a list of file before trying to process them.
if ( is_array( $backupsdir ) ) {
foreach ( $backupsdir as $backup ) {
if ( strpos( $backup, $checkpoint_name ) === 0 ) {
unlink( $upload_dir['basedir'] . '/checkpoint-storage/' . $backup );
}
}
}
}
/**
* Return a pretty human readable time.
*
* @author Gary Kovar
*
* @since 0.1.0
*
* @return false|string
*/
public function human_timestamp() {
return date( "Ymd-Hi", time() );
}
public function install_plugin() {
$args = array( 'plugin', 'install', 'db-snapshot' );
$assoc_args = array( 'activate' => 'activate' );
WP_CLI::run_command( $args, $assoc_args );
}
}
/**
* Kick off!
*
* @return DB_CheckPoint
*/
function db_checkpoint() {
return new DB_CheckPoint;
}
$checkpoint = db_checkpoint();
/**
* Add dbsnap as a WP CLI command.
*/
WP_CLI::add_command( 'dbsnap', array(
$checkpoint,
'checkpoint_save',
), $checkpoint->get_checkpoint_save_args() );
/**
* Add dbsnapback as a WP CLI command.
*/
WP_CLI::add_command( 'dbsnapback', array(
$checkpoint,
'checkpoint_restore',
), $checkpoint->get_checkpoint_restore_args() );
/**
* Add dbsnap plugin as a WP CLI command.
*/
WP_CLI::add_command( 'dbsnapplug', array(
$checkpoint,
'install_plugin',
), $checkpoint->get_install_plugin_args() );
}
}
if ( ! defined( 'WP_CLI' ) ) {
if ( ! class_exists( 'DB_CheckPoint_Plugin' ) ) {
class DB_CheckPoint_Plugin {
/**
* The return from wp_upload_dir();
*
* @var string
*/
private $upload_dir;
/**
* Initiate the class and set some of the regular variables.
*
* @author Gary Kovar
*
* @since 0.2.0
*/
public function init() {
$this->upload_dir = wp_upload_dir();
$this->hooks();
}
/**
* Hook to add functions to WP
*
* @author Gary Kovar
*
* @since 0.2.0
*/
public function hooks() {
$this->does_upload_folder_exist();
if ( $this->should_show_dbsnapback_in_admin_menu() ) {
add_action( 'admin_bar_menu', array( $this, 'toolbar_dbsnapback' ), 999 );
add_action( 'admin_bar_menu', array( $this, 'add_dbsnapback_child_nodes' ), 999 );
} else {
add_action( 'admin_bar_menu', array( $this, 'toolbar_dbsnap' ), 999 );
}
if ( key_exists( 'snpackback_restore', $_GET ) ) {
if ( current_user_can( 'manage_options' ) ) {
add_action( 'init', array( $this, 'restore' ) );
}
}
if ( key_exists( 'create_snap', $_GET ) ) {
if ( current_user_can( 'manage_options' ) ) {
add_action( 'init', array( $this, 'backup' ) );
}
}
}
/**
* Check if the upload folder exists and if not create it.
*
* @author Gary Kovar
*
* @since 0.2.0
*/
public function does_upload_folder_exist() {
if ( ! file_exists( $this->upload_dir['basedir'] . '/checkpoint-storage' ) ) {
mkdir( $this->upload_dir['basedir'] . '/checkpoint-storage' );
}
}
/**
* Check and see if we should show the admin bar by counting how many files are in the backup dir.
*
* @author Gary Kovar
*
* @since 0.2.0
*
* @return bool
*/
public function should_show_dbsnapback_in_admin_menu() {
// If we count more than 2 files (. , ..) then we have some backups.
if ( count( scandir( $this->upload_dir['basedir'] . '/checkpoint-storage' ) ) > 2 ) {
return true;
}
return false;
}
/**
* Get the list of snaps and add a node for each.
*
* @author Gary Kovar
*
* @since 0.2.0
*
*/
public function add_dbsnapback_child_nodes( $wp_admin_bar ) {
$files = $this->get_snaps();
foreach ( $files as $file ) {
$args = array(
'id' => $file[0],
'title' => $file[0],
'href' => '?snpackback_restore=' . $file[0] . '.' . $file[1] . '.sql',
'parent' => 'dbsnapback',
'meta' => array(
'class' => 'dbsnapback',
),
);
$wp_admin_bar->add_node( $args );
}
}
/**
* Get the existing snaps.
*
* @author Gary Kovar
*
* @since 0.2.0
*/
public function get_snaps() {
$backupsdir = scandir( $this->upload_dir['basedir'] . '/checkpoint-storage/', SCANDIR_SORT_DESCENDING );
foreach ( $backupsdir as $backup ) {
$file_exploded = explode( '.', $backup );
if ( $file_exploded[0] != '' ) {
$list[] = $file_exploded;
}
}
return $list;
}
/**
* Add restore link to toolbar.
*
* @author Gary Kovar
*
* @since 0.2.0
*/
public function toolbar_dbsnapback( $wp_admin_bar ) {
$args = array(
'id' => 'dbsnapback',
'title' => 'DBSnapBack',
'href' => '#',
'meta' => array(
'class' => 'dbsnapback',
),
);
$wp_admin_bar->add_node( $args );
}
/**
* Add snapshot link to toolbar.
*
* @author Gary Kovar
*
* @since 0.2.0
*/
public function toolbar_dbsnap( $wp_admin_bar ) {
$args = array(
'id' => 'dbsnap',
'title' => 'DBSnap',
'href' => '?create_snap',
'meta' => array(
'class' => 'dbsnap',
),
);
$wp_admin_bar->add_node( $args );
}
public function backup() {
$command = 'wp dbsnap';
exec( $command );
}
/**
* Restores the selected snapshot.
*
* @author Gary Kovar
*
* @since 0.2.0
*/
public function restore() {
$filename = $_GET['snpackback_restore'];
$command = 'wp db import ' . $this->upload_dir['basedir'] . '/checkpoint-storage/' . $filename;
exec( $command );
}
}
/**
* Kick Off!
*
* @return DB_CheckPoint_Plugin
*/
function db_checkpoint_plugin() {
return new DB_CheckPoint_Plugin();
}
add_action( 'plugins_loaded', array( db_checkpoint_plugin(), 'init' ) );
}
}