Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
/**
18
 * This script migrates data from current database to another
19
 *
20
 * This script is not intended for beginners!
21
 * Potential problems:
22
 * - su to apache account or sudo before execution
23
 * - already broken DB scheme or invalid data
24
 *
25
 * @package    tool_dbtransfer
26
 * @copyright  2012 Petr Skoda {@link http://skodak.org/}
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
29
 
30
define('CLI_SCRIPT', true);
31
 
32
require(__DIR__.'/../../../../config.php');
33
require_once($CFG->libdir.'/clilib.php');
34
require_once(__DIR__.'/../locallib.php');
35
 
36
$help =
37
    "Database migration script.
38
 
39
It is strongly recommended to turn off the web server
40
or enable CLI maintenance mode before starting the migration.
41
 
42
Options:
43
--dbtype=TYPE         Database type.
44
--dblibrary=TYPE      Database library. Defaults to 'native'.
45
--dbhost=HOST         Database host.
46
--dbname=NAME         Database name.
47
--dbuser=USERNAME     Database user.
48
--dbpass=PASSWORD     Database password.
49
--dbport=NUMBER       Database port.
50
--prefix=STRING       Table prefix for above database tables.
51
--dbsocket=PATH       Use database sockets. Available for some databases only.
52
-h, --help            Print out this help.
53
 
54
Example:
55
\$ sudo -u www-data /usr/bin/php admin/tool/dbtransfer/cli/migrate.php
56
";
57
 
58
// Now get cli options.
59
list($options, $unrecognized) = cli_get_params(
60
    array(
61
        'dbtype'            => null,
62
        'dblibrary'         => 'native',
63
        'dbhost'            => null,
64
        'dbname'            => null,
65
        'dbuser'            => null,
66
        'dbpass'            => null,
67
        'dbport'            => null,
68
        'prefix'            => null,
69
        'dbsocket'          => null,
70
        'maintenance'       => null,
71
        'list'              => false,
72
        'help'              => false,
73
    ),
74
    array(
75
        'm' => 'maintenance',
76
        'l' => 'list',
77
        'h' => 'help',
78
    )
79
);
80
 
81
if ($options['help']) {
82
    echo $help;
83
    exit(0);
84
}
85
 
86
if (empty($CFG->version)) {
87
    cli_error(get_string('missingconfigversion', 'debug'));
88
}
89
 
90
echo "\n".get_string('cliheading', 'tool_dbtransfer')."\n\n";
91
 
92
$drivers = tool_dbtransfer_get_drivers();
93
 
94
if (!isset($options['dbtype'])) {
95
    $choose = array();
96
    foreach ($drivers as $driver => $name) {
97
        list($dbtype, $dblibrary) = explode('/', $driver);
98
        $choose[$dbtype] = $dbtype;
99
    }
100
    $optionsstr = implode(', ', $choose);
101
    cli_heading(get_string('databasetypehead', 'install')." ($optionsstr)");
102
    $options['dbtype'] = cli_input(get_string('clitypevalue', 'admin'), '', $choose, true);
103
}
104
 
105
$choose = array();
106
foreach ($drivers as $driver => $name) {
107
    list($dbtype, $dblibrary) = explode('/', $driver);
108
    if ($dbtype === $options['dbtype']) {
109
        $choose[$dblibrary] = $dblibrary;
110
    }
111
}
112
if (!isset($options['dblibrary']) or !isset($choose[$options['dblibrary']])) {
113
    $optionsstr = implode(', ', $choose);
114
    cli_heading('Database library'." ($optionsstr)"); // Note: no need to localise unless we add real PDO drivers.
115
    $options['dblibrary'] = cli_input(get_string('clitypevalue', 'admin'), '', $choose, true);
116
}
117
 
118
if (!isset($options['dbhost'])) {
119
    cli_heading(get_string('databasehost', 'install'));
120
    $options['dbhost'] = cli_input(get_string('clitypevalue', 'admin'));
121
}
122
 
123
if (!isset($options['dbname'])) {
124
    cli_heading(get_string('databasename', 'install'));
125
    $options['dbname'] = cli_input(get_string('clitypevalue', 'admin'));
126
}
127
 
128
if (!isset($options['dbuser'])) {
129
    cli_heading(get_string('databaseuser', 'install'));
130
    $options['dbuser'] = cli_input(get_string('clitypevalue', 'admin'));
131
}
132
 
133
if (!isset($options['dbpass'])) {
134
    cli_heading(get_string('databasepass', 'install'));
135
    $options['dbpass'] = cli_input(get_string('clitypevalue', 'admin'));
136
}
137
 
138
if (!isset($options['prefix'])) {
139
    cli_heading(get_string('dbprefix', 'install'));
140
    $options['prefix'] = cli_input(get_string('clitypevalue', 'admin'));
141
}
142
 
143
if (!isset($options['dbport'])) {
144
    cli_heading(get_string('dbport', 'install'));
145
    $options['dbport'] = cli_input(get_string('clitypevalue', 'admin'));
146
}
147
 
148
if ($CFG->ostype !== 'WINDOWS') {
149
    if (!isset($options['dbsocket'])) {
150
        cli_heading(get_string('databasesocket', 'install'));
151
        $options['dbsocket'] = cli_input(get_string('clitypevalue', 'admin'));
152
    }
153
}
154
 
155
$a = (object)array('dbtypefrom' => $CFG->dbtype, 'dbtype' => $options['dbtype'],
156
    'dbname' => $options['dbname'], 'dbhost' => $options['dbhost']);
157
cli_heading(get_string('transferringdbto', 'tool_dbtransfer', $a));
158
 
159
// Try target DB connection.
160
$problem = '';
161
 
162
$targetdb = moodle_database::get_driver_instance($options['dbtype'], $options['dblibrary']);
163
$dboptions = array();
164
if ($options['dbport']) {
165
    $dboptions['dbport'] = $options['dbport'];
166
}
167
if ($options['dbsocket']) {
168
    $dboptions['dbsocket'] = $options['dbsocket'];
169
}
170
try {
171
    $targetdb->connect($options['dbhost'], $options['dbuser'], $options['dbpass'], $options['dbname'],
172
        $options['prefix'], $dboptions);
173
    if ($targetdb->get_tables()) {
174
        $problem .= get_string('targetdatabasenotempty', 'tool_dbtransfer');
175
    }
176
} catch (moodle_exception $e) {
177
    $problem .= $e->debuginfo."\n\n";
178
    $problem .= get_string('notargetconectexception', 'tool_dbtransfer');
179
}
180
 
181
if ($problem !== '') {
182
    echo $problem."\n\n";
183
    exit(1);
184
}
185
 
186
$feedback = new text_progress_trace();
187
tool_dbtransfer_transfer_database($DB, $targetdb, $feedback);
188
$feedback->finished();
189
 
190
cli_heading(get_string('success'));
191
exit(0);