46
46
use OCA \Backup \Exceptions \RestoringPointNotInitiatedException ;
47
47
use OCA \Backup \Exceptions \SqlDumpException ;
48
48
use OCA \Backup \Exceptions \SqlImportException ;
49
+ use OCA \Backup \Exceptions \SqlParamsException ;
49
50
use OCA \Backup \ISqlDump ;
50
51
use OCA \Backup \Model \ChangedFile ;
51
52
use OCA \Backup \Model \RestoringData ;
@@ -147,6 +148,8 @@ protected function configure() {
147
148
->setDescription ('Restore a restoring point ' )
148
149
->addArgument ('pointId ' , InputArgument::REQUIRED , 'Id of the restoring point ' )
149
150
->addOption ('force ' , '' , InputOption::VALUE_NONE , 'Force the restoring process ' )
151
+ ->addOption ('do-not-ask-data ' , '' , InputOption::VALUE_NONE , 'Do not ask for path on data ' )
152
+ ->addOption ('do-not-ask-sql ' , '' , InputOption::VALUE_NONE , 'Do not ask for params on sqldump ' )
150
153
->addOption ('file ' , '' , InputOption::VALUE_REQUIRED , 'restore only a specific file ' )
151
154
->addOption ('chunk ' , '' , InputOption::VALUE_REQUIRED , 'location of the file ' )
152
155
->addOption ('data ' , '' , InputOption::VALUE_REQUIRED , 'location of the file ' );
@@ -261,8 +264,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
261
264
$ output ->writeln ('> <info>maintenance mode</info> disabled ' );
262
265
$ this ->restoreService ->finalizeFullRestore ();
263
266
264
- // $this->configService->maintenanceMode(false);
265
-
266
267
$ this ->activityService ->newActivity (
267
268
ActivityService::RESTORE ,
268
269
[
@@ -291,7 +292,7 @@ public function restorePointComplete(RestoringPoint $point): void {
291
292
$ this ->output ->writeln (' > Found data pack: <info> ' . $ data ->getName () . '</info> ' );
292
293
293
294
if ($ data ->getType () === RestoringData::INTERNAL_DATA ) {
294
- $ this ->output ->writeln (' * ignoring ' );
295
+ $ this ->output ->writeln (' * ignoring ' );
295
296
continue ;
296
297
}
297
298
@@ -312,7 +313,13 @@ public function restorePointComplete(RestoringPoint $point): void {
312
313
* @throws RestoringPointNotInitiatedException
313
314
*/
314
315
private function restorePointData (RestoringPoint $ point , RestoringData $ data ): void {
315
- $ root = $ this ->requestDataRoot ($ data );
316
+ try {
317
+ $ root = $ this ->requestDataRoot ($ data );
318
+ } catch (RestoringDataNotFoundException $ e ) {
319
+ $ this ->output ->writeln (' * ignoring data pack ' );
320
+
321
+ return ;
322
+ }
316
323
317
324
$ this ->output ->writeln (' > extracting data to <info> ' . $ root . '</info> ' );
318
325
foreach ($ data ->getChunks () as $ chunk ) {
@@ -345,16 +352,29 @@ private function restorePointData(RestoringPoint $point, RestoringData $data): v
345
352
* @throws SqlDumpException
346
353
*/
347
354
private function restorePointSqlDump (RestoringPoint $ point , RestoringData $ data ): void {
348
- $ sqlParams = $ this ->requestSqlParams ();
355
+ while (true ) {
356
+ try {
357
+ $ sqlParams = $ this ->requestSqlParams ();
358
+ } catch (RestoringDataNotFoundException $ e ) {
359
+ $ this ->output ->writeln (' * ignoring sqldump ' );
349
360
350
- $ this ->output ->write (
351
- ' > importing sqldump in <info> ' . $ this ->displaySqlParams ($ sqlParams , true ) . '</info>: '
352
- );
353
- try {
354
- $ this ->importSqlDump ($ point , $ data , $ sqlParams );
355
- $ this ->output ->writeln ('<info>ok</info> ' );
356
- } catch (SqlImportException $ e ) {
357
- $ this ->output ->writeln ('<error> ' . $ e ->getMessage () . '</error> ' );
361
+ return ;
362
+ }
363
+
364
+ $ this ->output ->write (
365
+ ' > importing sqldump in <info> ' . $ this ->displaySqlParams ($ sqlParams , true ) . '</info>: '
366
+ );
367
+ try {
368
+ $ this ->importSqlDump ($ point , $ data , $ sqlParams );
369
+ $ this ->output ->writeln ('<info>ok</info> ' );
370
+ } catch (SqlParamsException $ e ) {
371
+ $ this ->output ->writeln ('<error> ' . $ e ->getMessage () . '</error> ' );
372
+ continue ;
373
+ } catch (SqlImportException $ e ) {
374
+ $ this ->output ->writeln ('<error> ' . $ e ->getMessage () . '</error> ' );
375
+ }
376
+
377
+ break ;
358
378
}
359
379
360
380
$ data ->setRestoredRoot (json_encode ($ sqlParams ));
@@ -365,18 +385,37 @@ private function restorePointSqlDump(RestoringPoint $point, RestoringData $data)
365
385
* @param RestoringData $data
366
386
*
367
387
* @return string
388
+ * @throws RestoringDataNotFoundException
368
389
*/
369
390
private function requestDataRoot (RestoringData $ data ): string {
370
391
$ root = $ data ->getAbsolutePath ();
392
+ if ($ this ->input ->getOption ('do-not-ask-data ' )) {
393
+ return $ root ;
394
+ }
395
+
371
396
while (true ) {
372
397
$ this ->output ->writeln (' > will be extracted in <info> ' . $ root . '</info> ' );
373
398
$ helper = $ this ->getHelper ('question ' );
374
399
$ question = new Question (
375
- ' - <comment>enter a new absolute path</comment> or press \'<info>enter</info> \' to use this location: ' ,
376
- $ root
400
+ ' - <comment>enter a new absolute path</comment>, or type <info>yes</info> to confirm '
401
+ . 'this location or <info>no</info> to ignore this part of the backup: ' ,
402
+ ''
377
403
);
378
- $ question ->setAutocompleterValues ([$ data ->getAbsolutePath ()]);
404
+ $ question ->setAutocompleterValues ([$ data ->getAbsolutePath (), ' yes ' , ' no ' ]);
379
405
$ newRoot = trim ($ helper ->ask ($ this ->input , $ this ->output , $ question ));
406
+
407
+ if ($ newRoot === '' ) {
408
+ continue ;
409
+ }
410
+
411
+ if ($ newRoot === 'yes ' ) {
412
+ break ;
413
+ }
414
+
415
+ if ($ newRoot === 'no ' ) {
416
+ throw new RestoringDataNotFoundException ('ignoring ' );
417
+ }
418
+
380
419
$ newRoot = rtrim ($ newRoot , '/ ' ) . '/ ' ;
381
420
if ($ newRoot === $ root ) {
382
421
break ;
@@ -391,38 +430,51 @@ private function requestDataRoot(RestoringData $data): string {
391
430
392
431
/**
393
432
* @return array
433
+ * @throws RestoringDataNotFoundException
394
434
*/
395
435
private function requestSqlParams (): array {
396
436
$ sqlParams = $ this ->pointService ->getSqlParams ();
437
+ if ($ this ->input ->getOption ('do-not-ask-sql ' )) {
438
+ return $ sqlParams ;
439
+ }
397
440
398
441
while (true ) {
399
442
$ this ->output ->writeln (' > will be imported in ' . $ this ->displaySqlParams ($ sqlParams , true ));
400
443
401
444
$ helper = $ this ->getHelper ('question ' );
402
- $ question = new ConfirmationQuestion (
403
- '<comment> - Do you want to import the dump in another SQL server or database ?</comment> (y/N) ' ,
404
- false ,
405
- '/^(y|Y)/i '
445
+ $ question = new Question (
446
+ '<comment> - Do you want to import the dump in the current database, or cancel the import ?</comment> (yes/No/cancel) ' ,
447
+ 'no ' ,
406
448
);
449
+ $ question ->setAutocompleterValues (['cancel ' , 'yes ' , 'no ' ]);
407
450
408
- if (!$ helper ->ask ($ this ->input , $ this ->output , $ question )) {
409
- return $ sqlParams ;
451
+ switch (strtolower ($ helper ->ask ($ this ->input , $ this ->output , $ question ))) {
452
+ case 'yes ' :
453
+ return $ sqlParams ;
454
+ case 'cancel ' :
455
+ throw new RestoringDataNotFoundException ();
410
456
}
411
457
412
458
$ this ->output ->writeln (' - current configuration: ' );
413
459
$ this ->displaySqlParams ($ sqlParams );
414
460
415
- $ this ->output ->writeln (' - edit configuration: ' );
461
+ $ this ->output ->writeln (' - edit configuration: (enter \' . \' to skip this step) ' );
416
462
while (true ) {
417
463
$ question = new Question (' . Host: ' , '' );
418
464
$ newHost = trim ($ helper ->ask ($ this ->input , $ this ->output , $ question ));
419
465
if ($ newHost !== '' ) {
420
466
break ;
421
467
}
422
468
}
469
+ if ($ newHost === '. ' ) {
470
+ continue ;
471
+ }
423
472
424
473
$ question = new Question (' . Port: ' , '' );
425
474
$ newPort = trim ($ helper ->ask ($ this ->input , $ this ->output , $ question ));
475
+ if ($ newPort === '. ' ) {
476
+ continue ;
477
+ }
426
478
427
479
while (true ) {
428
480
$ question = new Question (' . Database: ' , '' );
@@ -431,6 +483,9 @@ private function requestSqlParams(): array {
431
483
break ;
432
484
}
433
485
}
486
+ if ($ newName === '. ' ) {
487
+ continue ;
488
+ }
434
489
435
490
while (true ) {
436
491
$ question = new Question (' . Username: ' , '' );
@@ -439,6 +494,9 @@ private function requestSqlParams(): array {
439
494
break ;
440
495
}
441
496
}
497
+ if ($ newUser === '. ' ) {
498
+ continue ;
499
+ }
442
500
443
501
while (true ) {
444
502
$ question = new Question (' . Password: ' , '' );
@@ -448,6 +506,9 @@ private function requestSqlParams(): array {
448
506
break ;
449
507
}
450
508
}
509
+ if ($ newPass === '. ' ) {
510
+ continue ;
511
+ }
451
512
452
513
$ newParams = [
453
514
ISqlDump::DB_NAME => $ newName ,
@@ -483,14 +544,28 @@ private function updateConfig(RestoringPoint $point): void {
483
544
$ dataRoot = $ configRoot = '' ;
484
545
foreach ($ point ->getRestoringData () as $ data ) {
485
546
if ($ data ->getType () === RestoringData::ROOT_DATA ) {
547
+ if ($ data ->getRestoredRoot () === '' ) {
548
+ continue ;
549
+ }
486
550
$ dataRoot = $ data ->getRestoredRoot ();
487
551
}
488
552
489
553
if ($ data ->getType () === RestoringData::FILE_CONFIG ) {
554
+ if ($ data ->getRestoredRoot () === '' ) {
555
+ $ this ->output ->writeln (
556
+ ' * do not refresh as <info>config/config.php</info> were not restored '
557
+ );
558
+ $ this ->configService ->maintenanceMode (false );
559
+
560
+ return ;
561
+ }
490
562
$ configRoot = $ data ->getRestoredRoot ();
491
563
}
492
564
493
565
if ($ data ->getType () === RestoringData::FILE_SQL_DUMP ) {
566
+ if ($ data ->getRestoredRoot () === '' ) {
567
+ continue ;
568
+ }
494
569
$ sqlParams = json_decode ($ data ->getRestoredRoot (), true );
495
570
if (!is_array ($ sqlParams )) {
496
571
$ sqlParams = [];
@@ -502,17 +577,23 @@ private function updateConfig(RestoringPoint $point): void {
502
577
$ configFile = rtrim ($ configRoot , '/ ' ) . '/config.php ' ;
503
578
include $ configFile ;
504
579
505
- $ this ->compareConfigDataRoot ($ CONFIG , $ dataRoot );
506
- $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_HOST );
507
- $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_PORT );
508
- $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_NAME );
509
- $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_USER );
510
- $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_PASS );
580
+ if ($ dataRoot !== '' ) {
581
+ $ this ->compareConfigDataRoot ($ CONFIG , $ dataRoot );
582
+ }
583
+ if (!empty ($ sqlParams )) {
584
+ $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_HOST );
585
+ $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_PORT );
586
+ $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_NAME );
587
+ $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_USER );
588
+ $ this ->compareConfigSqlParams ($ sqlParams , $ CONFIG , ISqlDump::DB_PASS );
589
+ }
511
590
512
591
$ CONFIG ['maintenance ' ] = false ;
513
592
$ this ->output ->writeln (' > Updating <info>config.php</info> ' );
514
593
$ this ->output ->writeln ('' );
515
- file_put_contents ($ configFile , '<?php ' . "\n" . '$CONFIG = ' . var_export ($ CONFIG , true ) . '; ' . "\n" );
594
+ file_put_contents (
595
+ $ configFile , '<?php ' . "\n" . '$CONFIG = ' . var_export ($ CONFIG , true ) . '; ' . "\n"
596
+ );
516
597
}
517
598
518
599
@@ -593,6 +674,7 @@ private function compareConfigSqlParams(array $sqlParams, array &$CONFIG, string
593
674
*
594
675
* @throws SqlDumpException
595
676
* @throws SqlImportException
677
+ * @throws SqlParamsException
596
678
*/
597
679
private function importSqlDump (RestoringPoint $ point , RestoringData $ data , array $ sqlParams ): void {
598
680
$ chunks = $ data ->getChunks ();
@@ -617,6 +699,8 @@ private function importSqlDump(RestoringPoint $point, RestoringData $data, array
617
699
618
700
// $config = $this->extractDatabaseConfig();
619
701
$ sqlDump = $ this ->pointService ->getSqlDump ();
702
+ $ sqlDump ->setup ($ sqlParams );
703
+
620
704
$ sqlDump ->import ($ sqlParams , $ read );
621
705
}
622
706
0 commit comments