forked from famsf/4d-mysqldump
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4d-mysqldump.php
More file actions
executable file
·213 lines (195 loc) · 5.47 KB
/
4d-mysqldump.php
File metadata and controls
executable file
·213 lines (195 loc) · 5.47 KB
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
#!/usr/bin/env php
<?php
/**
* 4d-mysqldump: 4D Database Dump to MySQL
* Copyright (C) 2013 Fine Arts Museums of San Francisco
* Authored by Brad Erickson <eosrei at gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Command line script to dump a 4D database to a compliant MySQL dump file.
* The PDO_4D PHP extension is required: http://www.php.net/manual/en/ref.pdo-4d.php
*
* @see http://doc.4d.com/4D-Language-Reference-12.4/Subrecords/Get-subrecord-key.301-977448.en.html
* @see http://doc.4d.com/4D-SQL-Reference-12.1/Using-SQL-in-4D/Principles-for-integrating-4D-and-the-4D-SQL-engine.300-494388.en.html
*/
include_once "FourD.php";
include_once "FourDDump.php";
/**
* Print command line help
*/
function help() {
print<<<EOD
4d-mysqldump V1.0
Copyright 2013 Fine Arts Museums of San Francisco.
Dumps structure and contents of 4D databases and tables to MySQL.
Usage: 4d-mysqldump.php [OPTIONS]
-H, --help Display this help and exit.
-h, --host=name 4D server hostname or IP (required.)
-u, --user=name 4D username (required.)
-p, --password=password
4D password (required.)
-r, --retries=count Number of connection attempt retries (default 3, for a
total of 4.)
-l, --list List all tables and exit.
-t, --table=name Dumps a specific table instead of all tables.
-b, --ignore-binary Ignore picture/blob columns. They contain binary data
which may significantly increase the export size, but may
not be needed.
-s, --skip-structure
Only print data, don't print table structure.
-o, --offset=count The offset to use during the export.
-c, --limit=count The limit to use during the export.
-w, --allow-spaces Don't removed spaces from column names.
EOD;
exit();
}
function print_error($msg) {
fwrite(STDERR, $msg . PHP_EOL);
}
// Create an array of command line options
$options = array(
array(
'short' => 'H',
'long' => 'help',
'bool' => TRUE,
),
array(
'short' => 'h',
'long' => 'host',
'bool' => FALSE,
),
array(
'short' => 'u',
'long' => 'user',
'bool' => FALSE,
),
array(
'short' => 'p',
'long' => 'password',
'bool' => FALSE,
),
array(
'short' => 'r',
'long' => 'retries',
'bool' => FALSE,
),
array(
'short' => 'i',
'long' => 'test',
'bool' => TRUE,
),
array(
'short' => 't',
'long' => 'table',
'bool' => FALSE,
),
array(
'short' => 'b',
'long' => 'ignore-binary',
'bool' => TRUE,
),
array(
'short' => 's',
'long' => 'skip-structure',
'bool' => TRUE,
),
array(
'short' => 'o',
'long' => 'offset',
'bool' => FALSE,
),
array(
'short' => 'c',
'long' => 'limit',
'bool' => FALSE,
),
array(
'short' => 'l',
'long' => 'list',
'bool' => TRUE,
),
array(
'short' => 'w',
'long' => 'allow-spaces',
'bool' => TRUE,
),
// Internal use only to manage threads to avoid excessive memory use.
array(
'short' => 'z',
'long' => 'internal-thread',
'bool' => TRUE,
),
);
$opts = parseopt($options);
// Check for help
if ($opts['help']) {
help();
}
// Check required options
if (!$opts['host']) {
print_error('ERROR: 4D hostname is required.');
help();
}
if (!$opts['user']) {
print_error('ERROR: 4D username is required.');
help();
}
// Technically it *should* be required, but apparently 4D doesn't check this!
if (!$opts['password']) {
print_error('ERROR: 4D password is required.');
help();
}
// Set retries default as needed.
if (!$opts['retries']) {
$opts['retries'] = 3;
}
$fourd_dump = new FourDDump($opts);
//Done!
function parseopt($options) {
$short_opts = '';
$long_opts = array();
// Build short/long option arrays
foreach ($options as $option) {
$short_opts .= $option['short'];
$long_name = $option['long'];
// If not boolean collect values. Values are always optional here.
if(!$option['bool']) {
$short_opts .= '::';
$long_name .= '::';
}
$long_opts[] = $long_name;
}
// Get the options
$opt = getopt($short_opts, $long_opts);
foreach ($options as $option) {
$short = $option['short'];
$long = $option['long'];
// If short is set and long isn't, move short into long values.
if (isset($opt[$short]) && !isset($opt[$long])) {
// Move the value into the long name.
$opt[$long] = $opt[$short];
}
// Remove the short name either way (if both are set, short is ignored.)
unset($opt[$short]);
// Make all booleans values exist as real booleans.
if ($option['bool']) {
$opt[$long] = isset($opt[$long]) ? TRUE : FALSE;
}
// If not a bool and long is not set, set to FALSE
elseif (!isset($opt[$long])) {
$opt[$long] = FALSE;
}
}
//var_dump($opt);exit();
// Return the cleaned up array
return $opt;
}