· 9 years ago · Jun 19, 2017, 02:58 PM
1#/usr/bin/perl
2use Data::Dumper;
3use File::Find;
4
5my @aDatabases = ('LargeDatabase','MediumDatabase','SmallDatabase');
6
7#Loop through each Database we are going to load
8foreach my $sDatabase (@aDatabases){
9 my @aFiles = ();
10
11 ## Look in each Database directory for the table and data files.
12 find( \&findSQLFiles, "/mnt-temp/$sDatabase");
13}
14
15sub findSQLFiles{
16 print "\n\n\nTesting File:" . $_;
17
18 ## Check if the file is a table structure
19 if ( $_ =~ /\.sql/ ){
20 my $sDatabase = $File::Find::dir;
21 $sDatabase =~ s/\/mnt-temp\///;
22 my $data = $File::Find::name;
23
24 my $sDBUser ='myUser';
25 my $sDBPassword = 'myPassword';
26 my $sDBHost = 'myDBHost'
27
28
29 print "\nCreating Database: $sDatabase";
30 my $str = qq~mysql -u $sDBuser -p$sDBPassword -h $sDBHost -e 'Create Database if not exists $sDatabase'~;
31 `$str`;
32
33
34 print "\nCreating Table: $data";
35 $str = qq~\ncat $data | mysql -u $sDBuser -p$sDBPassword -h $sDBHost $sDatabase~;
36 `$str`;
37
38 ## Table structure is loaded so now load the data, but switching the file extension.
39 $data =~ s/\.sql/\.txt/;
40
41 print "\nImporting Data: $data";
42 ## use the "local" option to indicate that the file is on this server not on the host.
43 $str = qq~\n mysqlimport --local -u $sDBuser -p$sDBPassword -h $sDBHost $sDatabase $data~;
44 `$str`;
45
46 }
47
48}