Browse Source

[FEATURE] Add backup commands and library

Adam Bukowski 3 years ago
parent
commit
220a9f6409

+ 2
- 1
composer.json View File

@@ -4,7 +4,8 @@
4 4
         "twig/twig": "^1.24",
5 5
         "symfony/console": "^2.7",
6 6
         "symfony/http-foundation": "^2.7",
7
-        "phpdocumentor/reflection-docblock": "^2.0.4"
7
+        "phpdocumentor/reflection-docblock": "^2.0.4",
8
+        "ifsnop/mysqldump-php": "dev-master"
8 9
     },
9 10
     "scripts": {
10 11
         "post-install-cmd": [

+ 178
- 19
composer.lock View File

@@ -4,10 +4,62 @@
4 4
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
5 5
         "This file is @generated automatically"
6 6
     ],
7
-    "hash": "7cd00f28e2bbb91494283f0a36e59011",
8
-    "content-hash": "3d82e3f1b1f0912e248a46bf24027027",
7
+    "hash": "f12042cde6cb5c0fd6e20ca44c78c120",
8
+    "content-hash": "2b34029e9942b85f5b24fc42bfdbbd3f",
9 9
     "packages": [
10 10
         {
11
+            "name": "ifsnop/mysqldump-php",
12
+            "version": "dev-master",
13
+            "source": {
14
+                "type": "git",
15
+                "url": "https://github.com/ifsnop/mysqldump-php.git",
16
+                "reference": "c225512de8a2dd9aa6bbadf603d05a4e027550c1"
17
+            },
18
+            "dist": {
19
+                "type": "zip",
20
+                "url": "https://api.github.com/repos/ifsnop/mysqldump-php/zipball/c225512de8a2dd9aa6bbadf603d05a4e027550c1",
21
+                "reference": "c225512de8a2dd9aa6bbadf603d05a4e027550c1",
22
+                "shasum": ""
23
+            },
24
+            "require": {
25
+                "php": ">=5.3.0"
26
+            },
27
+            "require-dev": {
28
+                "phpunit/phpunit": "3.7.*",
29
+                "squizlabs/php_codesniffer": "1.*"
30
+            },
31
+            "type": "library",
32
+            "autoload": {
33
+                "psr-4": {
34
+                    "Ifsnop\\": "src/Ifsnop/"
35
+                }
36
+            },
37
+            "notification-url": "https://packagist.org/downloads/",
38
+            "license": [
39
+                "MIT"
40
+            ],
41
+            "authors": [
42
+                {
43
+                    "name": "Diego Torres",
44
+                    "homepage": "https://github.com/ifsnop",
45
+                    "role": "Developer"
46
+                }
47
+            ],
48
+            "description": "This is a php version of linux's mysqldump in terminal \"$ mysqldump -u username -p...\"",
49
+            "homepage": "https://github.com/ifsnop/mysqldump-php",
50
+            "keywords": [
51
+                "backup",
52
+                "database",
53
+                "dump",
54
+                "export",
55
+                "mysql",
56
+                "mysqldump",
57
+                "pdo",
58
+                "sqlite"
59
+            ],
60
+            "time": "2016-10-13 21:44:06"
61
+        },
62
+        {
11 63
             "name": "ircmaxell/password-compat",
12 64
             "version": "v1.0.4",
13 65
             "source": {
@@ -145,21 +197,69 @@
145 197
             "time": "2015-09-11 15:10:35"
146 198
         },
147 199
         {
200
+            "name": "psr/log",
201
+            "version": "1.0.2",
202
+            "source": {
203
+                "type": "git",
204
+                "url": "https://github.com/php-fig/log.git",
205
+                "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d"
206
+            },
207
+            "dist": {
208
+                "type": "zip",
209
+                "url": "https://api.github.com/repos/php-fig/log/zipball/4ebe3a8bf773a19edfe0a84b6585ba3d401b724d",
210
+                "reference": "4ebe3a8bf773a19edfe0a84b6585ba3d401b724d",
211
+                "shasum": ""
212
+            },
213
+            "require": {
214
+                "php": ">=5.3.0"
215
+            },
216
+            "type": "library",
217
+            "extra": {
218
+                "branch-alias": {
219
+                    "dev-master": "1.0.x-dev"
220
+                }
221
+            },
222
+            "autoload": {
223
+                "psr-4": {
224
+                    "Psr\\Log\\": "Psr/Log/"
225
+                }
226
+            },
227
+            "notification-url": "https://packagist.org/downloads/",
228
+            "license": [
229
+                "MIT"
230
+            ],
231
+            "authors": [
232
+                {
233
+                    "name": "PHP-FIG",
234
+                    "homepage": "http://www.php-fig.org/"
235
+                }
236
+            ],
237
+            "description": "Common interface for logging libraries",
238
+            "homepage": "https://github.com/php-fig/log",
239
+            "keywords": [
240
+                "log",
241
+                "psr",
242
+                "psr-3"
243
+            ],
244
+            "time": "2016-10-10 12:19:37"
245
+        },
246
+        {
148 247
             "name": "symfony/console",
149
-            "version": "v2.8.11",
248
+            "version": "v2.8.12",
150 249
             "source": {
151 250
                 "type": "git",
152 251
                 "url": "https://github.com/symfony/console.git",
153
-                "reference": "3d3e4fa5f0614c8e45220e5de80332322e33bd90"
252
+                "reference": "d7a5a88178f94dcc29531ea4028ea614e35452d4"
154 253
             },
155 254
             "dist": {
156 255
                 "type": "zip",
157
-                "url": "https://api.github.com/repos/symfony/console/zipball/3d3e4fa5f0614c8e45220e5de80332322e33bd90",
158
-                "reference": "3d3e4fa5f0614c8e45220e5de80332322e33bd90",
256
+                "url": "https://api.github.com/repos/symfony/console/zipball/d7a5a88178f94dcc29531ea4028ea614e35452d4",
257
+                "reference": "d7a5a88178f94dcc29531ea4028ea614e35452d4",
159 258
                 "shasum": ""
160 259
             },
161 260
             "require": {
162 261
                 "php": ">=5.3.9",
262
+                "symfony/debug": "~2.7,>=2.7.2|~3.0.0",
163 263
                 "symfony/polyfill-mbstring": "~1.0"
164 264
             },
165 265
             "require-dev": {
@@ -202,20 +302,77 @@
202 302
             ],
203 303
             "description": "Symfony Console Component",
204 304
             "homepage": "https://symfony.com",
205
-            "time": "2016-09-06 10:55:00"
305
+            "time": "2016-09-28 00:10:16"
306
+        },
307
+        {
308
+            "name": "symfony/debug",
309
+            "version": "v3.0.9",
310
+            "source": {
311
+                "type": "git",
312
+                "url": "https://github.com/symfony/debug.git",
313
+                "reference": "697c527acd9ea1b2d3efac34d9806bf255278b0a"
314
+            },
315
+            "dist": {
316
+                "type": "zip",
317
+                "url": "https://api.github.com/repos/symfony/debug/zipball/697c527acd9ea1b2d3efac34d9806bf255278b0a",
318
+                "reference": "697c527acd9ea1b2d3efac34d9806bf255278b0a",
319
+                "shasum": ""
320
+            },
321
+            "require": {
322
+                "php": ">=5.5.9",
323
+                "psr/log": "~1.0"
324
+            },
325
+            "conflict": {
326
+                "symfony/http-kernel": ">=2.3,<2.3.24|~2.4.0|>=2.5,<2.5.9|>=2.6,<2.6.2"
327
+            },
328
+            "require-dev": {
329
+                "symfony/class-loader": "~2.8|~3.0",
330
+                "symfony/http-kernel": "~2.8|~3.0"
331
+            },
332
+            "type": "library",
333
+            "extra": {
334
+                "branch-alias": {
335
+                    "dev-master": "3.0-dev"
336
+                }
337
+            },
338
+            "autoload": {
339
+                "psr-4": {
340
+                    "Symfony\\Component\\Debug\\": ""
341
+                },
342
+                "exclude-from-classmap": [
343
+                    "/Tests/"
344
+                ]
345
+            },
346
+            "notification-url": "https://packagist.org/downloads/",
347
+            "license": [
348
+                "MIT"
349
+            ],
350
+            "authors": [
351
+                {
352
+                    "name": "Fabien Potencier",
353
+                    "email": "fabien@symfony.com"
354
+                },
355
+                {
356
+                    "name": "Symfony Community",
357
+                    "homepage": "https://symfony.com/contributors"
358
+                }
359
+            ],
360
+            "description": "Symfony Debug Component",
361
+            "homepage": "https://symfony.com",
362
+            "time": "2016-07-30 07:22:48"
206 363
         },
207 364
         {
208 365
             "name": "symfony/http-foundation",
209
-            "version": "v2.8.11",
366
+            "version": "v2.8.12",
210 367
             "source": {
211 368
                 "type": "git",
212 369
                 "url": "https://github.com/symfony/http-foundation.git",
213
-                "reference": "1d4ab8de2215e44e57fddc1e6b5d122546769e7d"
370
+                "reference": "91f87d27e9fe99435278c337375b0dce292fe0e2"
214 371
             },
215 372
             "dist": {
216 373
                 "type": "zip",
217
-                "url": "https://api.github.com/repos/symfony/http-foundation/zipball/1d4ab8de2215e44e57fddc1e6b5d122546769e7d",
218
-                "reference": "1d4ab8de2215e44e57fddc1e6b5d122546769e7d",
374
+                "url": "https://api.github.com/repos/symfony/http-foundation/zipball/91f87d27e9fe99435278c337375b0dce292fe0e2",
375
+                "reference": "91f87d27e9fe99435278c337375b0dce292fe0e2",
219 376
                 "shasum": ""
220 377
             },
221 378
             "require": {
@@ -257,7 +414,7 @@
257 414
             ],
258 415
             "description": "Symfony HttpFoundation Component",
259 416
             "homepage": "https://symfony.com",
260
-            "time": "2016-09-06 10:55:00"
417
+            "time": "2016-09-21 19:04:07"
261 418
         },
262 419
         {
263 420
             "name": "symfony/polyfill-mbstring",
@@ -434,16 +591,16 @@
434 591
         },
435 592
         {
436 593
             "name": "twig/twig",
437
-            "version": "v1.24.2",
594
+            "version": "v1.26.1",
438 595
             "source": {
439 596
                 "type": "git",
440 597
                 "url": "https://github.com/twigphp/Twig.git",
441
-                "reference": "33093f6e310e6976baeac7b14f3a6ec02f2d79b7"
598
+                "reference": "a09d8ee17ac1cfea29ed60c83960ad685c6a898d"
442 599
             },
443 600
             "dist": {
444 601
                 "type": "zip",
445
-                "url": "https://api.github.com/repos/twigphp/Twig/zipball/33093f6e310e6976baeac7b14f3a6ec02f2d79b7",
446
-                "reference": "33093f6e310e6976baeac7b14f3a6ec02f2d79b7",
602
+                "url": "https://api.github.com/repos/twigphp/Twig/zipball/a09d8ee17ac1cfea29ed60c83960ad685c6a898d",
603
+                "reference": "a09d8ee17ac1cfea29ed60c83960ad685c6a898d",
447 604
                 "shasum": ""
448 605
             },
449 606
             "require": {
@@ -456,7 +613,7 @@
456 613
             "type": "library",
457 614
             "extra": {
458 615
                 "branch-alias": {
459
-                    "dev-master": "1.24-dev"
616
+                    "dev-master": "1.26-dev"
460 617
                 }
461 618
             },
462 619
             "autoload": {
@@ -491,13 +648,15 @@
491 648
             "keywords": [
492 649
                 "templating"
493 650
             ],
494
-            "time": "2016-09-01 17:50:53"
651
+            "time": "2016-10-05 18:57:41"
495 652
         }
496 653
     ],
497 654
     "packages-dev": [],
498 655
     "aliases": [],
499 656
     "minimum-stability": "stable",
500
-    "stability-flags": [],
657
+    "stability-flags": {
658
+        "ifsnop/mysqldump-php": 20
659
+    },
501 660
     "prefer-stable": false,
502 661
     "prefer-lowest": false,
503 662
     "platform": [],

+ 4
- 0
console.php View File

@@ -22,4 +22,8 @@ $application->add(new \Epesi\Console\Maintenance\MaintenanceOnCommand());
22 22
 $application->add(new \Epesi\Console\Maintenance\MaintenanceOffCommand());
23 23
 $application->add(new \Epesi\Console\SearchClearCommand());
24 24
 $application->add(new \Epesi\Console\SearchIndexCommand());
25
+$application->add(new \Epesi\Console\Backup\BackupDbCommand());
26
+$application->add(new \Epesi\Console\Backup\BackupFilesCommand());
27
+$application->add(new \Epesi\Console\Backup\BackupAllCommand());
28
+$application->add(new \Epesi\Console\Backup\ListBackupsCommand());
25 29
 $application->run();

+ 57
- 0
console/Backup/BackupAllCommand.php View File

@@ -0,0 +1,57 @@
1
+<?php
2
+
3
+namespace Epesi\Console\Backup;
4
+
5
+use Symfony\Component\Console\Command\Command;
6
+use Symfony\Component\Console\Input\InputArgument;
7
+use Symfony\Component\Console\Input\InputInterface;
8
+use Symfony\Component\Console\Input\InputOption;
9
+use Symfony\Component\Console\Output\OutputInterface;
10
+use Symfony\Component\Console\Style\SymfonyStyle;
11
+
12
+class BackupAllCommand extends Command
13
+{
14
+    protected function configure()
15
+    {
16
+        $this
17
+            ->setName('backup:all')
18
+            ->setDescription('Backup EPESI files with database snapshot')
19
+            ->addArgument(
20
+                'file',
21
+                InputArgument::OPTIONAL,
22
+                'file or directory to backup'
23
+            )
24
+            ->addOption(
25
+                'output', 'o',
26
+                InputOption::VALUE_REQUIRED,
27
+                'output file'
28
+            )
29
+            ->addOption(
30
+                'force', 'f',
31
+                InputOption::VALUE_NONE,
32
+                'force overwrite backup file'
33
+            )
34
+        ;
35
+    }
36
+
37
+    protected function execute(InputInterface $input, OutputInterface $output)
38
+    {
39
+        $file = $input->getArgument('file');
40
+        $file_description = 'Only ' . $file;
41
+        if (!$file) {
42
+            $file = '.';
43
+            $file_description = 'All files';
44
+        }
45
+        $output_file = $input->getOption('output');
46
+        $overwrite = $input->getOption('force') ? true : false;
47
+        $st = new SymfonyStyle($input, $output);
48
+
49
+        require_once 'include/backups.php';
50
+        $util = \BackupUtil::default_instance();
51
+        $description = "EPESI ver " . EPESI_VERSION . " rev " . EPESI_REVISION . ' - ' . $file_description . ' with database snapshot';
52
+        $backup = $util->create_backup($file, $description, $output_file, $overwrite);
53
+        $util->addDbBackup($backup);
54
+        $st->writeln('Created backup:');
55
+        $st->writeln(sprintf("<fg=yellow>[%s]</> <fg=green>%s</> (File: %s)", $backup->get_date('Y-m-d H:i:s'), $backup->get_description(), $backup->get_file()));
56
+    }
57
+}

+ 33
- 0
console/Backup/BackupDbCommand.php View File

@@ -0,0 +1,33 @@
1
+<?php
2
+
3
+namespace Epesi\Console\Backup;
4
+
5
+use Symfony\Component\Console\Command\Command;
6
+use Symfony\Component\Console\Input\InputArgument;
7
+use Symfony\Component\Console\Input\InputInterface;
8
+use Symfony\Component\Console\Output\OutputInterface;
9
+use Symfony\Component\Console\Style\SymfonyStyle;
10
+
11
+class BackupDbCommand extends Command
12
+{
13
+    protected function configure()
14
+    {
15
+        $this
16
+            ->setName('backup:db')
17
+            ->setDescription('Backup database')
18
+            ->addArgument(
19
+                'file',
20
+                InputArgument::REQUIRED,
21
+                'backup filename'
22
+            );
23
+    }
24
+
25
+    protected function execute(InputInterface $input, OutputInterface $output)
26
+    {
27
+        $st = new SymfonyStyle($input, $output);
28
+        $file = $input->getArgument('file');
29
+        require_once 'include/backups.php';
30
+        \BackupUtil::backup_db($file);
31
+        $st->writeln('done');
32
+    }
33
+}

+ 56
- 0
console/Backup/BackupFilesCommand.php View File

@@ -0,0 +1,56 @@
1
+<?php
2
+
3
+namespace Epesi\Console\Backup;
4
+
5
+use Symfony\Component\Console\Command\Command;
6
+use Symfony\Component\Console\Input\InputArgument;
7
+use Symfony\Component\Console\Input\InputInterface;
8
+use Symfony\Component\Console\Input\InputOption;
9
+use Symfony\Component\Console\Output\OutputInterface;
10
+use Symfony\Component\Console\Style\SymfonyStyle;
11
+
12
+class BackupFilesCommand extends Command
13
+{
14
+    protected function configure()
15
+    {
16
+        $this
17
+            ->setName('backup:files')
18
+            ->setDescription('Backup EPESI files')
19
+            ->addArgument(
20
+                'file',
21
+                InputArgument::OPTIONAL,
22
+                'file or directory to backup'
23
+            )
24
+            ->addOption(
25
+                'output', 'o',
26
+                InputOption::VALUE_REQUIRED,
27
+                'output file'
28
+            )
29
+            ->addOption(
30
+                'force', 'f',
31
+                InputOption::VALUE_NONE,
32
+                'force overwrite backup file'
33
+            )
34
+        ;
35
+    }
36
+
37
+    protected function execute(InputInterface $input, OutputInterface $output)
38
+    {
39
+        $file = $input->getArgument('file');
40
+        $file_description = 'Only ' . $file;
41
+        if (!$file) {
42
+            $file = '.';
43
+            $file_description = 'All files';
44
+        }
45
+        $output_file = $input->getOption('output');
46
+        $overwrite = $input->getOption('force') ? true : false;
47
+        $st = new SymfonyStyle($input, $output);
48
+
49
+        require_once 'include/backups.php';
50
+        $util = \BackupUtil::default_instance();
51
+        $description = "EPESI ver " . EPESI_VERSION . " rev " . EPESI_REVISION . ' - ' . $file_description;
52
+        $backup = $util->create_backup($file, $description, $output_file, $overwrite);
53
+        $st->writeln('Created backup:');
54
+        $st->writeln(sprintf("<fg=yellow>[%s]</> <fg=green>%s</> (File: %s)", $backup->get_date('Y-m-d H:i:s'), $backup->get_description(), $backup->get_file()));
55
+    }
56
+}

+ 30
- 0
console/Backup/ListBackupsCommand.php View File

@@ -0,0 +1,30 @@
1
+<?php
2
+
3
+namespace Epesi\Console\Backup;
4
+
5
+use Symfony\Component\Console\Command\Command;
6
+use Symfony\Component\Console\Input\InputArgument;
7
+use Symfony\Component\Console\Input\InputInterface;
8
+use Symfony\Component\Console\Output\OutputInterface;
9
+use Symfony\Component\Console\Style\SymfonyStyle;
10
+use Ifsnop\Mysqldump\Mysqldump;
11
+
12
+class ListBackupsCommand extends Command
13
+{
14
+    protected function configure()
15
+    {
16
+        $this
17
+            ->setName('backup:list')
18
+            ->setDescription('List backups');
19
+    }
20
+
21
+    protected function execute(InputInterface $input, OutputInterface $output)
22
+    {
23
+        $st = new SymfonyStyle($input, $output);
24
+        require_once 'include/backups.php';
25
+        $util = \BackupUtil::default_instance();
26
+        foreach ($util->list_backups() as $backup) {
27
+            $st->writeln(sprintf("<fg=yellow>[%s]</> <fg=green>%s</> (File: %s)", $backup->get_date('Y-m-d H:i:s'), $backup->get_description(), $backup->get_file()));
28
+        }
29
+    }
30
+}

+ 93
- 28
include/backups.php View File

@@ -1,5 +1,7 @@
1 1
 <?php
2 2
 
3
+use Ifsnop\Mysqldump\Mysqldump;
4
+
3 5
 class BackupUtil {
4 6
 
5 7
     private $_backup_dir;
@@ -13,6 +15,25 @@ class BackupUtil {
13 15
         $this->_backup_store = new BackupStore($backup_dir);
14 16
     }
15 17
 
18
+    public static function backup_db($file = null)
19
+    {
20
+        if (\DB::is_postgresql()) {
21
+            throw new Exception('PostgreSQL is not supported yet');
22
+        }
23
+        $compress = Mysqldump::NONE;
24
+        $extension = strtolower(substr($file, -3));
25
+        if ($extension == '.gz') $compress = Mysqldump::GZIP;
26
+        if ($extension == '.bz') $compress = Mysqldump::BZIP2;
27
+        $options = array(
28
+            'compress' => $compress,
29
+            'add-drop-table' => true,
30
+            'no-data' => array('recordbrowser_search_index', 'session', 'session_client', 'history'),
31
+        );
32
+        $dump = new Mysqldump('mysql:host=' . DATABASE_HOST . ';dbname=' . DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD, $options);
33
+        $dump->start($file);
34
+        return $file;
35
+    }
36
+
16 37
     function list_backups() {
17 38
         $files = $this->_backup_store->list_backup_files();
18 39
         $ret = array();
@@ -23,21 +44,37 @@ class BackupUtil {
23 44
     }
24 45
 
25 46
     function create_backup_of_epesi() {
47
+
26 48
         $description = "EPESI ver " . EPESI_VERSION . " rev " . EPESI_REVISION;
27
-        return $this->_create_backup('.', $description);
49
+        $backup = $this->create_backup('.', $description);
50
+        $this->addDbBackup($backup);
51
+        return $backup;
52
+    }
53
+
54
+    function addDbBackup(Backup $backup)
55
+    {
56
+        $db_backup = 'db_backup_' . time() . '.sql.gz';
57
+        self::backup_db($db_backup);
58
+        if (file_exists($db_backup)) {
59
+            $backup->get_archive()->addSingleFile($db_backup, 'db_backup.sql.gz');
60
+            unlink($db_backup);
61
+        }
28 62
     }
29 63
 
30
-    function create_backup($files, $description = '') {
31
-        return $this->_create_backup($files, $description);
64
+    function create_backup($files, $description = '', $output_file = null, $overwrite = false) {
65
+        if (!$output_file) {
66
+            $output_file = $this->_backup_store->new_backup_file();
67
+        }
68
+        $bkp = new Backup($output_file, $overwrite);
69
+        return $this->_create_backup($files, $description, $bkp);
32 70
     }
33 71
 
34
-    private function _create_backup($files, $description) {
35
-        $file = $this->_backup_store->new_backup_file();
36
-        $bkp = new Backup($file);
72
+    private function _create_backup($files, $description, Backup $backup) {
37 73
         $this->_chdir_to_epesi();
38
-        $success = $bkp->create($files, $description, $this->_backup_dir);
74
+        $exclude = array($this->_backup_dir, DATA_DIR . '/cache');
75
+        $success = $backup->create($files, $description, $exclude);
39 76
         $this->_chdir_back();
40
-        return $success ? $bkp : false;
77
+        return $success ? $backup : false;
41 78
     }
42 79
 
43 80
     private function _chdir_to_epesi() {
@@ -54,14 +91,14 @@ class BackupUtil {
54 91
     }
55 92
 
56 93
     static function default_instance() {
57
-        return new BackupUtil(getcwd(), 'data/backups');
94
+        return new BackupUtil(getcwd(), DATA_DIR . '/backups');
58 95
     }
59 96
 
60 97
 }
61 98
 
62 99
 class BackupStore {
63 100
 
64
-    private $_extension = 'bkp';
101
+    private $_extension = 'bkp.zip';
65 102
     private $_dir;
66 103
 
67 104
     public function __construct($dir) {
@@ -92,8 +129,9 @@ class BackupStore {
92 129
         $ret = array();
93 130
         $dir = new DirectoryIterator($this->_dir);
94 131
         foreach ($dir as $file) {
95
-            if ($file->getExtension() == $this->_extension)
132
+            if (preg_match('/.*' . preg_quote($this->_extension) . '$/', $file->getFilename())) {
96 133
                 $ret[] = $file->getPathname();
134
+            }
97 135
         }
98 136
         return $ret;
99 137
     }
@@ -105,21 +143,21 @@ class Backup {
105 143
     private static $__properties_in_metadata = array('date', 'description');
106 144
     private $date;
107 145
     private $description;
108
-    private $files;
109
-    private $_overwrite;
146
+    private $file;
147
+    private $overwrite;
110 148
 
111 149
     /** @var BackupArchive */
112
-    private $_archive;
150
+    private $archive;
113 151
 
114 152
     public function __construct($backup_file, $overwrite = false) {
115
-        $this->_file = $backup_file;
116
-        $this->_overwrite = $overwrite;
117
-        $this->_archive = new BackupArchive($backup_file);
153
+        $this->file = $backup_file;
154
+        $this->overwrite = $overwrite;
155
+        $this->archive = new BackupArchive($backup_file);
118 156
         $this->_read_metadata();
119 157
     }
120 158
 
121 159
     private function _read_metadata() {
122
-        $data = $this->_archive->get_metadata();
160
+        $data = $this->archive->get_metadata();
123 161
         $this->_set_properties($data);
124 162
     }
125 163
 
@@ -142,41 +180,61 @@ class Backup {
142 180
 
143 181
     public function create($files, $description, $exclude_files = array()) {
144 182
         set_time_limit(0);
145
-        if ($this->_overwrite == false && $this->_archive->exists())
146
-            return false;
183
+        if ($this->overwrite == false && $this->archive->exists()) {
184
+            throw new Exception('Backup archive exists - overwrite is forbidden.');
185
+        }
147 186
 
148
-        $success = $this->_archive->create($files, $exclude_files);
187
+        $success = $this->archive->create($files, $exclude_files);
149 188
         if ($success) {
150 189
             $this->date = time();
151 190
             $this->description = $description;
152
-            $this->_archive->set_metadata($this->_get_properties());
191
+            $this->archive->set_metadata($this->_get_properties());
153 192
         }
154 193
         return $success;
155 194
     }
156 195
 
157 196
     public function restore_to($destination) {
158 197
         set_time_limit(0);
159
-        return $this->_archive->extractTo($destination);
198
+        return $this->archive->extractTo($destination);
160 199
     }
161 200
     
162 201
     public function restore() {
163 202
         return $this->restore_to('.');
164 203
     }
165 204
 
205
+    /**
206
+     * @param string|null $format date function format string
207
+     *
208
+     * @return int|string unix timestamp or formatted date
209
+     */
166 210
     public function get_date($format = null) {
167 211
         if (is_string($format))
168 212
             return date($format, $this->date);
169 213
         return $this->date;
170 214
     }
171 215
 
216
+    /**
217
+     * Backup description
218
+     *
219
+     * @return string
220
+     */
172 221
     public function get_description() {
173 222
         return $this->description;
174 223
     }
175 224
 
176
-    public function get_files() {
177
-        if (!$this->files)
178
-            $this->files = $this->_archive->list_files();
179
-        return $this->files;
225
+    /**
226
+     * @return BackupArchive
227
+     */
228
+    public function get_archive() {
229
+        return $this->archive;
230
+    }
231
+
232
+    /**
233
+     * @return string File path to backup
234
+     */
235
+    public function get_file()
236
+    {
237
+        return $this->file;
180 238
     }
181 239
 
182 240
 }
@@ -262,6 +320,13 @@ class BackupArchive extends ZipArchive {
262 320
             return $this->addFile($path);
263 321
     }
264 322
 
323
+    public function addSingleFile($file, $dest)
324
+    {
325
+        $this->_open();
326
+        $this->addFile($file, $dest);
327
+        $this->close();
328
+    }
329
+
265 330
     public function get_metadata() {
266 331
         if (!file_exists($this->_file))
267 332
             return null;
@@ -298,7 +363,7 @@ class BackupArchive extends ZipArchive {
298 363
 
299 364
     private function _is_excluded($file) {
300 365
         foreach ($this->_exclude as $ex) {
301
-            if (strpos($file, $ex) === 0)
366
+            if (preg_match("#$ex#", $file))
302 367
                 return true;
303 368
         }
304 369
         return false;

Loading…
Cancel
Save