-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdumpa.drush.inc
248 lines (222 loc) · 8.2 KB
/
dumpa.drush.inc
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
<?php
/**
* @file dumpa.drush.inc
*/
use Drush\Log\LogLevel;
/**
* Implements hook_drush_command().
*/
function dumpa_drush_command() {
$items = array();
$items['dumpa-dump'] = array(
'aliases' => ['dumpa'],
'callback' => 'dumpa_dump',
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
'description' => 'Dump DB to single files.',
'options' => _dumpa_common_options(),
);
$items['dumpa-restore'] = array(
'aliases' => ['restora'],
'callback' => 'dumpa_restore',
'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_DATABASE,
'description' => 'Restore DB from single files.',
'options' => _dumpa_common_options(),
);
return $items;
}
function _dumpa_common_options() {
// Copy of drush_sql_get_table_selection_options().
return array(
'drop' => 'Use --no-drop to suppress dropping of additional files / tables.',
'result-dir' => 'Result directory. Defaults to private://_dumpa/$site.',
'skip-tables-key' => 'A key in the $skip_tables array. @see example.drushrc.php. Optional.',
'structure-tables-key' => 'A key in the $structure_tables array. @see example.drushrc.php. Optional.',
'tables-key' => 'A key in the $tables array. Optional.',
'skip-tables-list' => 'A comma-separated list of tables to exclude completely. Optional.',
'structure-tables-list' => 'A comma-separated list of tables to include for structure, but not data. Optional.',
'tables-list' => 'A comma-separated list of tables to transfer. Optional.',
);
}
/**
* Dump DB to single files.
*
* The mysqldump option --tab dumps tables to single files and
* even splits them into sql schema and csv data. Great, but to
* work the DB needs the permission to access and modify any files
* on the server so a sane admin won't give this to mortal users
* in a shared hosting environment. Sigh.
* So we simulate this in userland, by dumping tables to separate
* files, and using sql for data too.
*
* @link https://stackoverflow.com/a/6300583/606859
* @link https://stackoverflow.com/a/12041241/606859
*/
function dumpa_dump() {
// @see drush_sql_dump(), @see \Drush\Sql\SqlBase::dump()
$sql = drush_sql_get_class();
if (!$sql instanceof \Drush\Sql\Sqlmysql) {
return drush_set_error('DRUSH_DUMPA_FAIL', dt('Can only work with mysql currently.'));
}
$tmp = drush_tempdir();
if (!$dir = _dumpa_dir()) {
return drush_set_error('DUMPA', dt('Dumpa needs a --result-dir or configured private files.'));
}
// Calculate $tables and $data_tables.
$table_selection = $sql->get_expanded_table_selection();
$tables = $table_selection['tables']
?: $sql->listTables();
$tables = array_diff($tables, $table_selection['skip']);
$data_tables = array_diff($tables, $table_selection['structure']);
foreach ($tables as $table) {
$dump_data_too = in_array($table, $data_tables);
if (!_dumpa_dump_table($sql, $tmp, $table, $dump_data_too)) {
return FALSE;
}
}
// Move files and keep track of them.
$source_files = glob("$tmp/*.sql");
$moved_files = [];
foreach ($source_files as $source_file) {
$base_filename = basename($source_file);
$destination_file = "$dir/" . $base_filename;
// Looks like drush_move_dir() also works for files.
$success = drush_move_dir($source_file, $destination_file, TRUE);
if (!$success) {
return drush_set_error('DRUSH_DUMPA_FAIL', dt('Can not copy file: !file', ['!file' => $base_filename]));
}
$moved_files[] = $destination_file;
}
// Drop other files.
if (drush_get_option('drop', TRUE)) {
$destination_files = glob("$dir/*.sql");
foreach (array_diff($destination_files, $moved_files) as $destination_file) {
// Use drush_op to respect --simulate.
$success = drush_op('unlink', $destination_file);
if (!$success) {
return drush_set_error('DRUSH_DUMPA_FAIL', dt('Can not delete file: !file', ['!file' => $destination_file]));
}
}
}
return TRUE;
}
function _dumpa_dump_table(Drush\Sql\SqlBase $sql, $dir, $table, $dump_data_too) {
return _dumpa_dump_component($sql, $dir, $table, FALSE)
&& ($dump_data_too ? _dumpa_dump_component($sql, $dir, $table, TRUE) : TRUE);
}
function _dumpa_dump_component(Drush\Sql\SqlBase $sql, $dir, $table, $dump_data) {
$cmd = _dumpa_cmd($sql, $table, $dump_data);
$component = $dump_data ? 'data' : 'structure';
$cmd .= ' >' . drush_escapeshellarg("$dir/$table.{$component}.sql");
$t_args = array('!table' => $table, '!component' => $component);
// No need to capture output.
// Mind that drush_op_system return code is 0 on success.
$success = !drush_op_system($cmd);
if (!$success) {
return drush_set_error('DRUSH_DUMPA_FAIL', dt('Table !component dump failed: !table', $t_args));
}
else {
drush_log(dt('Table !component dumped: !table', $t_args), LogLevel::SUCCESS);
}
return TRUE;
}
function _dumpa_cmd(Drush\Sql\SqlBase $sql, $table, $dump_data) {
// @see \Drush\Sql\Sqlmysql::dumpCmd
$cmd = 'mysqldump ';
// DB name.
$cmd .= str_replace('--database=', ' ', $sql->creds());
if ($dump_data) {
$cmd .= ' --no-create-info';
}
else {
$cmd .= " --no-data";
}
$cmd .= ' --no-autocommit --single-transaction --opt -Q';
// Ordered dump.
$cmd .= ' --skip-extended-insert --order-by-primary';
$cmd .= ' --skip-comments --skip-dump-date';
$cmd .= " $table";
return $cmd;
}
function dumpa_restore() {
$sql = drush_sql_get_class();
if (!$sql instanceof \Drush\Sql\Sqlmysql) {
return drush_set_error('DRUSH_DUMPA_FAIL', dt('Can only work with mysql currently.'));
}
if (!$dir = _dumpa_dir()) {
return drush_set_error('DUMPA', dt('Dumpa needs a --result-dir or configured private files.'));
}
$structure_files = _dumpa_files($dir, '.structure.sql');
$data_files = _dumpa_files($dir, '.data.sql');
if (!$structure_files && !$data_files) {
return drush_set_error('DRUSH_DUMPA_FAIL', dt('Dumpa directory is empty.'));
}
// Calculate $tables and $data_tables.
$table_selection = $sql->get_expanded_table_selection();
$tables = $table_selection['tables']
?: $sql->listTables();
$skip_tables = $table_selection['skip'];
$tables = array_diff($tables, $skip_tables);
$data_tables = array_diff($tables, $table_selection['structure']);
// Lock.
$sql->query(sprintf('LOCK TABLES %s WRITE', implode(', ', $tables)));
$restored_tables = [];
foreach (array_intersect_key($structure_files, array_flip($tables)) as $table => $file) {
drush_log(dt('Restoring structure: !table', ['!table' => $table]));
$success = $sql->query(NULL, $file);
if (!$success) {
return drush_set_error('DRUSH_DUMPA_FAIL', dt('Could not import structure: !table.', ['!table' => $table]));
}
$restored_tables[] = $table;
}
foreach (array_intersect_key($data_files, array_flip($tables), array_flip($data_tables)) as $table => $file) {
if (!in_array($table, $restored_tables)) {
return drush_set_error('DRUSH_DUMPA_FAIL', dt('Found data without structure: !table.', ['!table' => $table]));
}
drush_log(dt('Restoring data: !table', ['!table' => $table]));
$success = $sql->query(NULL, $file);
if (!$success) {
return drush_set_error('DRUSH_DUMPA_FAIL', dt('Could not import data: !table.', ['!table' => $table]));
}
}
// Unlock
$sql->query('UNLOCK TABLES');
// Drop tables not restored (and not excluded).
$tables_to_drop = array_diff($tables, $restored_tables);
if ($tables_to_drop && drush_get_option('drop', TRUE)) {
drush_log(dt('Dropping tables: !tables', ['!tables' => implode(', ', $tables_to_drop)]));
$sql->drop($tables_to_drop);
}
return TRUE;
}
function _dumpa_files($dir, $suffix) {
$files = glob("$dir/*$suffix");
$indexed_files = [];
foreach ($files as $file) {
$basename = basename($file);
$index = substr($basename, 0, -strlen($suffix));
$indexed_files[$index] = $file;
}
return $indexed_files;
}
/**
* Figure out dir to use.
*
* @return string|null
*/
function _dumpa_dir() {
$dir = drush_get_option('result-dir', _dumpa_default_dir());
return $dir;
}
/**
* Get default dir if private files are configured.
*
* @return string
*/
function _dumpa_default_dir() {
if (
drush_bootstrap(DRUSH_BOOTSTRAP_DRUPAL_CONFIGURATION)
&& ($private = drush_file_get_private())
) {
return $private . "/_dumpa";
}
}