summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--contrib/mysql_import_combined_log.pl57
1 files changed, 27 insertions, 30 deletions
diff --git a/contrib/mysql_import_combined_log.pl b/contrib/mysql_import_combined_log.pl
index cc5b8de..bffec20 100644
--- a/contrib/mysql_import_combined_log.pl
+++ b/contrib/mysql_import_combined_log.pl
@@ -1,5 +1,5 @@
1#!/usr/bin/perl -w 1#!/usr/bin/perl -w
2 2# $Id: mysql_import_combined_log.pl,v 1.4 2004/02/21 18:09:50 urkle Exp $
3# Written by Aaron Jenson. 3# Written by Aaron Jenson.
4# Original source: http://www.visualprose.com/software.php 4# Original source: http://www.visualprose.com/software.php
5# Updated to work under Perl 5.6.1 by Edward Rudd 5# Updated to work under Perl 5.6.1 by Edward Rudd
@@ -37,47 +37,44 @@ my @cols = (
37); 37);
38my $col = ''; 38my $col = '';
39 39
40%options = (
41 "version" => sub { VERSION_MESSAGE(); exit 0; },
42 "help|?" => sub { HELP_MESSAGE(); exit 0; },
43 );
44
45GetOptions (\%options, 40GetOptions (\%options,
46 "h|host=s", 41 "version" => sub { VERSION_MESSAGE(); exit 0; },
47 "d|database=s", 42 "help|?" => sub { HELP_MESSAGE(); exit 0; },
48 "t|table=s", 43 "host|h=s",
49 "u|username=s", 44 "database|d=s",
50 "p|password=s", 45 "table|t=s",
51 "f|logfile=s"); 46 "username|u=s",
52 47 "password|p=s",
53$options{h} ||= 'localhost'; 48 "logfile|f=s");
54$options{d} ||= ''; 49
55$options{u} ||= ''; 50$options{host} ||= 'localhost';
56$options{p} ||= ''; 51$options{database} ||= '';
57$options{f} ||= ''; 52$options{username} ||= '';
58 53$options{password} ||= '';
59if( ! $options{d} ) 54$options{logfile} ||= '';
55
56if( ! $options{database} )
60{ 57{
61 HELP_MESSAGE(); 58 HELP_MESSAGE();
62 print "Must supply a database to connect to.\n"; 59 print "Must supply a database to connect to.\n";
63 exit 1; 60 exit 1;
64} 61}
65 62
66if( ! $options{t} ) 63if( ! $options{table} )
67{ 64{
68 HELP_MESSAGE(); 65 HELP_MESSAGE();
69 print "Must supply table name.\n"; 66 print "Must supply table name.\n";
70 exit 1; 67 exit 1;
71} 68}
72 69
73if( $options{f} ) 70if( $options{logfile} )
74{ 71{
75 if( ! -e $options{f} ) 72 if( ! -e $options{logfile} )
76 { 73 {
77 print "File '$options{f}' doesn't exist.\n"; 74 print "File '$options{logfile}' doesn't exist.\n";
78 exit 1; 75 exit 1;
79 } 76 }
80 open(STDIN, "<$options{f}") || die "Can't open $options{f} for reading."; 77 open(STDIN, "<$options{logfile}") || die "Can't open $options{logfile} for reading.";
81} 78}
82 79
83$dbh = Connect(); 80$dbh = Connect();
@@ -85,7 +82,7 @@ if (! $dbh) {
85 exit 1; 82 exit 1;
86} 83}
87 84
88$sql = "INSERT INTO $options{t} ("; 85$sql = "INSERT INTO $options{table} (";
89foreach $col (@cols) 86foreach $col (@cols)
90{ 87{
91 $sql .= "$col," if( $col ); 88 $sql .= "$col," if( $col );
@@ -118,15 +115,15 @@ while($line = <STDIN>)
118} 115}
119print "Parsed $linecount Log lines\n"; 116print "Parsed $linecount Log lines\n";
120print "Inserted $insertcount records\n"; 117print "Inserted $insertcount records\n";
121print "to table '$options{t}' in database '$options{d}' on '$options{h}'\n"; 118print "to table '$options{table}' in database '$options{database}' on '$options{host}'\n";
122 119
123# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 120# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
124# Connects to a MySQL database and returns the connection. 121# Connects to a MySQL database and returns the connection.
125# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 122# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
126sub Connect 123sub Connect
127{ 124{
128 my $dsn = "DBI:mysql:$options{d};hostname=$options{h}"; 125 my $dsn = "DBI:mysql:$options{database};hostname=$options{host}";
129 return DBI->connect( $dsn, $options{u}, $options{p} ); 126 return DBI->connect( $dsn, $options{username}, $options{password} );
130} 127}
131 128
132 129
@@ -219,7 +216,7 @@ EOF
219# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 216# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
220sub VERSION_MESSAGE 217sub VERSION_MESSAGE
221{ 218{
222 print "mysql_import_combined_log.pl version 1.1\n"; 219 print "mysql_import_combined_log.pl version 1.2\n";
223 print "Version 1.0 Written by Aaron Jenson.\n"; 220 print "Version 1.0 Written by Aaron Jenson.\n";
224 print "Update to work with perl 5.6.1 by Edward Rudd\n"; 221 print "Update to work with perl 5.6.1 by Edward Rudd\n";
225} 222}