2 # See the NOTICE file distributed with this work for additional information
3 # regarding copyright ownership.
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
22 # download and import the database
31 my $force = undef; #
if set ignore checksum dies just wrtie warnings.
32 my $mysqltmpdir = undef;
35 GetOptions (
'root=s' => \$root,
36 'database=s' => \$database,
37 'new_database=s' => \$new_database,
40 'cleanup' => \$cleanup,
44 'mysqltempdir=s' => \$mysqltmpdir,
46 'help' => sub {
usage(); exit(0);}
50 if(defined($database)){
52 #query database to try and guess root;
53 $database =~ /\S+_\S+_\S+_(\d+)_/;
55 if(defined($release)){
56 $root =
"//ftp.ensembl.org/ensembl/pub/release-".$release.
"/mysql";
57 print
"Using $root as the root obtained from the database name\n" unless $quiet;
60 die
"No root given i.e. ftp.ensembl.org/pub/release-54/mysql and could not guess from the database name $database";
66 die
"No root given i.e. ftp.ensembl.org/pub/release-54/mysql and no database name given to try and guess root from";
69 if(!defined($new_database)){
70 $new_database = $ENV{
"USER"}.
"_".$database;
71 print
"will create new database $new_database\n" unless $quiet;
74 if(!defined $user or !defined $pass or !defined $host){
75 die
"Need user, password and host for mysql instance to create new database on\n";
79 my $mysql_options =
"-h$host -u$user -p$pass";
81 $mysql_options .=
" -P$port";
84 print
"rsync --recursive rsync:$root/$database .\n" unless $quiet;
88 $line = `rsync --recursive --verbose rsync:$root/$database .`;
91 $line = `rsync --recursive --quiet rsync:$root/$database .`;
94 print $line unless $quiet;
97 #if it does snot exist then so be it just ignore error code
98 #my $com = "mysql $mysql_options -e'drop database ".$new_database."'";
100 # no need to check here as if the databae does not exist it should get an error
101 # just done to delete if it exists already
105 ## generate error to test
107 #$mysql_options =~ s/-uensadmin/-uensro/g;
109 my $com =
"mysql $mysql_options -e'create database $new_database'";
111 if($? or $line =~ /Error/ or $line =~ /ERROR/){
113 die
"Error during mysql\n";
116 print
"Created new database $new_database on host $host\n" unless $quiet;
120 $mysql_options .=
" $new_database";
123 #get the database schema and load it.
124 print
"now creating the schema\n" unless $quiet;
125 system(
"gunzip -f $database/$database.sql.gz");
126 system(
"mysql $mysql_options < $database/$database.sql");
127 system(
"gzip $database/$database.sql");
129 system(
"gunzip -f $database/CHECKSUMS.gz");
130 print
"now parse the checksum\n" unless $quiet;
132 if(defined($mysqltmpdir)){
133 $mysql_options =
" --tmpdir $mysqltmpdir ".$mysql_options;
136 open(CHK,
"<$database/CHECKSUMS") or die "COuld not open CHECKSUMS for reading???\n";
139 my ($e1, $e2, $file) = split;
142 if($file =~ /(\S+)(.*\d*).txt.gz/){
147 print
"ignore $file\n" unless $quiet;
150 if(!-e
"$database/$file"){
151 print STDERR
"$database/$file does not exist. It is specified in the CHECKSUM file but cannot be found?";
154 $com =
"sum $database/$file";
157 print STDERR
"$com failed\n";
158 print STDERR
"with output:".$line.
"\n";
159 print STDERR
"and error code $?\n";
160 print STDERR
"Ending as no checksum could be obtained";
163 my ($s1, $s2, @junk) = split (/\s+/,$line);
164 if($s1 != $e1 or $s2 != $e2){
165 print STDERR
"Warning: checksums do not match for file $database/$file\n" unless $quiet;
166 print STDERR
" from checksum we have $e1 and $e2\n" unless $quiet;
167 print STDERR
" but from sum we have $s1 and $s2\n" unless $quiet;
169 print
" Force set so carrying on\n" unless $quiet;
172 print STDERR
"Checksums do not match which can be a problem.\n";
173 print STDERR
"But the CHECKSUM file can sometimes be wrong as the database may have been\n";
174 print STDERR
"updated without the CHECKSUM file being updated\n";
175 print STDERR
"To continue with just warning use the -force flag in the options\n";
180 system(
"gunzip -f $database/$file");
182 my $str=
"mysqlimport --fields_escaped_by=\\\\ $mysql_options ".$ENV{
"PWD"}.
"/$database/$table$index.txt";
183 print
"$str\n" unless $quiet;
185 if($line =~ /Error/ or $?){
187 print STDERR
"error code $?\n";
188 print STDERR
"Error during mysqlimport\n";
191 print $line unless $quiet;
192 system(
"gzip $database/$table$index.txt");
193 print
"\n\n" unless $quiet;
205 if(defined($died) and $died){
206 system(
"gzip $database/CHECKSUMS");
209 if(defined($cleanup)){
210 system(
"rm -Rf $database");
218 This perl script will download (rsync) the necesary ftp files and load them into a
new local
219 ensembl mysql database. It will check that the checksums match and
do all the zipping and
220 unzipping of the files.
223 load_database_from_ftp.pl -root {root} -database {database} -new_database {database2}
225 -host {host} -port {port} -user {user} -pass {password}
228 -root Root directory
for ftp files
230 -database Database name to get data
for
232 -new_database Name of the
new database
234 -user User name to access database. Must allow writing.
236 -pass Password
for user.
242 -force
import data even
if the checksums
do not match
244 -
cleanup remove the downloaded files at the end
246 -quiet No output except
for serous error message
248 -mysqltmpdir Mysql may not have enough tmp space so
this can be set to another directory
250 -help print
this help text
256 1) perl load_database_from_ftp_site.pl -database homo_sapiens_core_54_36p -host mysqlhostname
257 -user mysqluser -pass mysqlpassword -force
259 This will download the ftp files
for the 54 release of the human core database and create a database
260 called <userid>_homo_sapiens_core_59_36p where userid is the login name of the user. To choose you
261 own database name use the -new_database option.
264 2) load_database_from_ftp_site.pl -databases homo_sapiens_core_57_37d -new_database homo_sapiens_core_59_37d
265 -host mysqlhostname -user mysqluser -pass mysqlpassword -quiet -
cleanup -mysqltmpdir /scratch/
267 Will load the human core database into the mysql instance on mysqlhostname and use the directory
268 /scratch/ to use as the tmp directory
for mysql.