1+ <?php
2+
3+ namespace BM \BackupManagerBundle \Command ;
4+
5+
6+ use BackupManager \Config \Config ;
7+ use BackupManager \Filesystems \Destination ;
8+ use BackupManager \Filesystems \FilesystemProvider ;
9+ use BackupManager \Manager ;
10+ use Symfony \Component \Console \Command \Command ;
11+ use Symfony \Component \Console \Input \InputArgument ;
12+ use Symfony \Component \Console \Input \InputInterface ;
13+ use Symfony \Component \Console \Input \InputOption ;
14+ use Symfony \Component \Console \Output \OutputInterface ;
15+
16+ /**
17+ * Backup database.
18+ *
19+ * @author Tobias Nyholm <tobias.nyholm@gmail.com>
20+ */
21+ class BackupCommand extends Command
22+ {
23+ protected static $ defaultName = 'backup-manager:backup ' ;
24+
25+ /**
26+ * @var Manager
27+ */
28+ private $ manager ;
29+
30+ /**
31+ * @var string
32+ */
33+ private $ filePrefix ;
34+
35+ /**
36+ *
37+ * @param Manager $manager
38+ * @param string $filePrefix
39+ */
40+ public function __construct (Manager $ manager , $ filePrefix )
41+ {
42+ $ this ->manager = $ manager ;
43+ $ this ->filePrefix = $ filePrefix ;
44+ parent ::__construct ();
45+ }
46+
47+
48+ protected function configure ()
49+ {
50+ $ this
51+ ->setDescription ('Starts a new backup. ' )
52+ ->addArgument ('database ' , InputArgument::REQUIRED , 'What database configuration do you want to backup? ' )
53+ ->addArgument ('destinations ' , InputArgument::IS_ARRAY , 'What storages do you want to upload the backup to? ' )
54+ ->addOption ('compression ' , 'c ' , InputOption::VALUE_OPTIONAL , 'How do you want to compress the file? ' , 'null ' )
55+ ->addOption ('filename ' , 'name ' , InputOption::VALUE_OPTIONAL , 'A customized filename ' , null )
56+ ;
57+ }
58+
59+ protected function execute (InputInterface $ input , OutputInterface $ output )
60+ {
61+ if (null === $ filename = $ input ->getOption ('filename ' )) {
62+ $ filename = $ this ->filePrefix .(new \DateTime ())->format ('Y-m-d_H-i-s ' );
63+ }
64+
65+ $ destinations = [];
66+ foreach ($ input ->getArgument ('destinations ' ) as $ name ) {
67+ $ destinations [] = new Destination ($ name , $ filename );
68+ }
69+
70+ $ this ->manager ->makeBackup ()->run ($ input ->getArgument ('database ' ), $ destinations , $ input ->getOption ('compression ' ));
71+ }
72+ }
0 commit comments