1+ <?php
2+ namespace phpbu \App \Backup \Source ;
3+
4+ use phpbu \App \Backup \Cli \Binary ;
5+ use phpbu \App \Backup \Cli \Cmd ;
6+ use phpbu \App \Backup \Cli \Exec ;
7+ use phpbu \App \Backup \Source ;
8+ use phpbu \App \Backup \Target ;
9+ use phpbu \App \Exception ;
10+ use phpbu \App \Result ;
11+ use phpbu \App \Util ;
12+
13+ /**
14+ * Arangodump source class.
15+ *
16+ * @package phpbu
17+ * @subpackage Backup
18+ * @author Francis Chuang <francis.chuang@gmail.com>
19+ * @author Sebastian Feldmann <sebastian@phpbu.de>
20+ * @copyright Sebastian Feldmann <sebastian@phpbu.de>
21+ * @license http://www.opensource.org/licenses/BSD-3-Clause The BSD 3-Clause License
22+ * @link http://phpbu.de/
23+ * @since Class available since Release 2.0.0
24+ */
25+ class Arangodump extends Binary implements Source
26+ {
27+ /**
28+ * Show stdErr
29+ *
30+ * @var boolean
31+ */
32+ private $ showStdErr ;
33+
34+ /**
35+ * Endpoint to connect to
36+ * --server.endpoint <endpoint>
37+ *
38+ * @var string
39+ */
40+ private $ endpoint ;
41+
42+ /**
43+ * Username to connect with
44+ * --server.username <username>
45+ *
46+ * @var string
47+ */
48+ private $ username ;
49+
50+ /**
51+ * Password to authenticate with
52+ * --server.password <password>
53+ *
54+ * @var string
55+ */
56+ private $ password ;
57+
58+ /**
59+ * The database to backup
60+ * --server.database <database>
61+ *
62+ * @var string
63+ */
64+ private $ database ;
65+
66+ /**
67+ * Whether the data should be dumped or not
68+ * --dump-data
69+ *
70+ * @var boolean
71+ */
72+ private $ dumpData ;
73+
74+ /**
75+ * Include system collections
76+ * --include-system-collections
77+ *
78+ * @var boolean
79+ */
80+ private $ includeSystemCollections ;
81+
82+ /**
83+ * Restrict the dump to these collections
84+ * --collection
85+ *
86+ * @var array
87+ */
88+ private $ collections ;
89+
90+ /**
91+ * Do not ask for the username and password when connecting to the server.
92+ * This does not control whether the server requires authentication.
93+ * -- disable-authentication
94+ *
95+ * @var boolean
96+ */
97+ private $ disableAuthentication ;
98+
99+ /**
100+ * Tar source to compress ArangoDB dump directory
101+ *
102+ * @var \phpbu\App\Backup\Source\Tar
103+ */
104+ private $ tar ;
105+
106+ /**
107+ * Setup.
108+ *
109+ * @see \phpbu\App\Backup\Source
110+ * @param array $conf
111+ * @throws \phpbu\App\Exception
112+ */
113+ public function setup (array $ conf = array ())
114+ {
115+ $ this ->setupArangodump ($ conf );
116+ $ this ->setupSourceData ($ conf );
117+
118+ $ this ->endpoint = Util \Arr::getValue ($ conf , 'endpoint ' );
119+ $ this ->username = Util \Arr::getValue ($ conf , 'username ' );
120+ $ this ->password = Util \Arr::getValue ($ conf , 'password ' );
121+ $ this ->database = Util \Arr::getValue ($ conf , 'database ' );
122+ $ this ->showStdErr = Util \Str::toBoolean (Util \Arr::getValue ($ conf , 'showStdErr ' ), false );
123+ $ this ->disableAuthentication = Util \Str::toBoolean (Util \Arr::getValue ($ conf , 'disableAuthentication ' ), false );
124+ }
125+
126+ /**
127+ * Search for arangodump command.
128+ *
129+ * @param array $conf
130+ */
131+ protected function setupArangodump (array $ conf )
132+ {
133+ if (empty ($ this ->binary )) {
134+ $ this ->binary = $ this ->detectCommand ('arangodump ' , Util \Arr::getValue ($ conf , 'pathToArangodump ' ));
135+ }
136+ }
137+
138+ /**
139+ * Get collections and data to backup.
140+ *
141+ * @param array $conf
142+ */
143+ protected function setupSourceData (array $ conf )
144+ {
145+ $ this ->dumpData = Util \Str::toBoolean (Util \Arr::getValue ($ conf , 'dumpData ' ), false );
146+ $ this ->includeSystemCollections = Util \Str::toBoolean (Util \Arr::getValue ($ conf , 'includeSystemCollections ' ), false );
147+ $ this ->collections = Util \Str::toList (Util \Arr::getValue ($ conf , 'collections ' ));
148+ }
149+
150+ /**
151+ * (non-PHPDoc)
152+ *
153+ * @see \phpbu\App\Backup\Source
154+ * @param \phpbu\App\Backup\Target $target
155+ * @param \phpbu\App\Result $result
156+ * @return \phpbu\App\Backup\Source\Status
157+ * @throws \phpbu\App\Exception
158+ */
159+ public function backup (Target $ target , Result $ result )
160+ {
161+ $ exec = $ this ->getExec ($ target );
162+ $ arangodump = $ this ->execute ($ exec );
163+
164+ $ result ->debug ($ arangodump ->getCmd ());
165+
166+ if (!$ arangodump ->wasSuccessful ()) {
167+ throw new Exception ('arangodump failed ' );
168+ }
169+
170+ return Status::create ()->uncompressed ()->dataPath ($ this ->getDumpDir ($ target ));
171+ }
172+
173+ /**
174+ * Create the Exec to run the mysqldump command.
175+ *
176+ * @param \phpbu\App\Backup\Target $target
177+ * @return \phpbu\App\Backup\Cli\Exec
178+ */
179+ public function getExec (Target $ target )
180+ {
181+ if (null == $ this ->exec ) {
182+ $ dump = $ this ->getDumpDir ($ target );
183+ $ this ->exec = new Exec ();
184+ $ cmd = new Cmd ($ this ->binary );
185+ $ this ->exec ->addCommand ($ cmd );
186+
187+ // no std error unless it is activated
188+ if (!$ this ->showStdErr ) {
189+ $ cmd ->silence ();
190+ // i kill you
191+ }
192+
193+ $ this ->addOptionIfNotEmpty ($ cmd , '--server.username ' , $ this ->username , true , ' ' );
194+ $ this ->addOptionIfNotEmpty ($ cmd , '--server.password ' , $ this ->password , true , ' ' );
195+ $ this ->addOptionIfNotEmpty ($ cmd , '--server.endpoint ' , $ this ->endpoint , true , ' ' );
196+ $ this ->addOptionIfNotEmpty ($ cmd , '--server.database ' , $ this ->database , true , ' ' );
197+
198+ if (count ($ this ->collections )) {
199+ foreach ($ this ->collections as $ collection ){
200+ $ cmd ->addOption ('--collection ' , $ collection , ' ' );
201+ }
202+ }
203+
204+ if ($ this ->disableAuthentication ){
205+ $ cmd ->addOption ('--server.disable-authentication ' , var_export ($ this ->disableAuthentication , true ), ' ' );
206+ }
207+
208+ if ($ this ->includeSystemCollections ) {
209+ $ cmd ->addOption ('--include-system-collections ' , var_export ($ this ->includeSystemCollections , true ), ' ' );
210+ }
211+
212+ if ($ this ->dumpData ) {
213+ $ cmd ->addOption ('--dump-data ' , var_export ($ this ->dumpData , true ), ' ' );
214+ }
215+
216+ $ cmd ->addOption ('--output-directory ' , $ dump , ' ' );
217+ }
218+
219+ return $ this ->exec ;
220+ }
221+
222+ /**
223+ * Get the ArangoDB dump directory.
224+ *
225+ * @param \phpbu\App\Backup\Target $target
226+ * @return string
227+ */
228+ public function getDumpDir (Target $ target )
229+ {
230+ return $ target ->getPath () . '/dump ' ;
231+ }
232+ }
0 commit comments