From 317671456dabc3b811c4d8281f99acba01efe3ab Mon Sep 17 00:00:00 2001 From: hjp Date: Wed, 13 Nov 2013 00:24:35 +0000 Subject: [PATCH] Added Simba/CA/DBI.pm and convert_mysql_to_pgsql to repository. --- MANIFEST | 3 - lib/Simba/CA/DBI.pm | 16 +++++ scripts/convert_mysql_to_pgsql | 118 +++++++++++++++++++++++++++++++++ 3 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 lib/Simba/CA/DBI.pm create mode 100755 scripts/convert_mysql_to_pgsql diff --git a/MANIFEST b/MANIFEST index 7596c26..8d4f67b 100644 --- a/MANIFEST +++ b/MANIFEST @@ -1,4 +1,3 @@ -backtrace Build.PL doc/.vimrc doc/arch.obj @@ -22,8 +21,6 @@ scripts/convert_db_to_v2 scripts/convert_mysql_to_pgsql scripts/da scripts/simba_export -space_by_link_count -space_by_link_count.out t/00_da.t t/01_ca.t t/02_ca.t diff --git a/lib/Simba/CA/DBI.pm b/lib/Simba/CA/DBI.pm new file mode 100644 index 0000000..ea94a2b --- /dev/null +++ b/lib/Simba/CA/DBI.pm @@ -0,0 +1,16 @@ +=head1 NAME + +Simba::CA::DBI + +=head1 DESCRIPTION + +DBI backend of the Simba Collecting Agent. + +=cut + +package Simba::CA::DBI; +use strict; +use warnings; + +use base 'Simba::CA'; + diff --git a/scripts/convert_mysql_to_pgsql b/scripts/convert_mysql_to_pgsql new file mode 100755 index 0000000..2708f1d --- /dev/null +++ b/scripts/convert_mysql_to_pgsql @@ -0,0 +1,118 @@ +#!/usr/bin/perl + +use warnings; +use strict; + +use Getopt::Long; +use DBI; + +my $mysql_conn; +my $pgsql_conn; +GetOptions( + 'mysql:s' => \$mysql_conn, + 'pgsql:s' => \$pgsql_conn, +); + +die unless ($mysql_conn && $pgsql_conn); + +my $dbh_my = DBI->connect(read_conn($mysql_conn), {RaiseError => 1}); +my $dbh_pg = DBI->connect(read_conn($pgsql_conn), {AutoCommit => 0, RaiseError => 1}); + +print STDERR "deleting sessions\n"; +$dbh_pg->do("delete from sessions"); +print STDERR "deleting files\n"; +$dbh_pg->do("delete from files"); +print STDERR "deleting filesets\n"; +$dbh_pg->do("delete from filesets"); + +print STDERR "copying sessions\n"; +my $sth_my = $dbh_my->prepare("select id, start_date, end_date, prefix from sessions"); +my $sth_pg = $dbh_pg->prepare("insert into sessions(id, start_date, end_date, prefix) values(?, ?, ?, ?)"); +$sth_my->execute(); +while (my @r = $sth_my->fetchrow_array) { + $sth_pg->execute(@r); +} + +print STDERR "copying filesets\n"; +$sth_my = $dbh_my->prepare("select id, host, dir, options from filesets"); +$sth_pg = $dbh_pg->prepare("insert into filesets(id, host, dir, options) values(?, ?, ?, ?)"); +$sth_my->execute(); +while (my @r = $sth_my->fetchrow_array) { + $sth_pg->execute(@r); +} + +my $t0 = time; +my $c = 0; +print STDERR "copying files\n"; +$sth_my = $dbh_my->prepare("select id, path, fileset from files"); +$sth_pg = $dbh_pg->prepare("insert into files(id, path, fileset) values(?, ?, ?)"); +$sth_my->execute(); +while (my @r = $sth_my->fetchrow_array) { + $sth_pg->execute(@r); + if (++$c % 1000 == 0) { + my $dt = time - $t0; + printf "\t%6d %6.1f %6.1f\n", $c, $dt, $c / $dt; + } +} + +$t0 = time; +$c = 0; +print STDERR "copying versions2\n"; +$sth_my = $dbh_my->prepare("select * from versions2"); +$sth_pg + = $dbh_pg->prepare( + "insert into versions2(id, + file_type, file_size, file_mtime, file_owner, file_group, file_acl, file_unix_bits, file_rdev, + checksum, file_linktarget) + values(?, + ?, ?, ?, ?, ?, ?, ?, ?, + ?, ?)"); +$sth_my->execute(); +my %ub_names_to_bits = ( + setuid => '100', + setgid => '010', + sticky => '001', +); +while (my $r = $sth_my->fetchrow_hashref) { + + my $file_unix_bits = '000'; + for (split(/,/, $r->{file_unix_bits})) { + $file_unix_bits |= $ub_names_to_bits{$_}; + } + $r->{file_unix_bits} = $file_unix_bits; + $sth_pg->execute(@{$r}{qw(id file_type file_size file_mtime + file_owner file_group file_acl file_unix_bits file_rdev + checksum file_linktarget)} + ); + if (++$c % 1000 == 0) { + my $dt = time - $t0; + printf "\t%6d %6.1f %6.1f\n", $c, $dt, $c / $dt; + } +} + +$t0 = time; +$c = 0; +print STDERR "copying instances\n"; +$sth_my = $dbh_my->prepare("select id, file, file_id, date, online, session, version from instances"); +$sth_pg = $dbh_pg->prepare("insert into instances(id, file, file_id, date, online, session, version) values(?, ?, ?, ?, ?, ?, ?)"); +$sth_my->execute(); +while (my @r = $sth_my->fetchrow_array) { + $sth_pg->execute(@r); + if (++$c % 1000 == 0) { + my $dt = time - $t0; + printf "\t%6d %6.1f %6.1f\n", $c, $dt, $c / $dt; + } +} + +$dbh_pg->commit(); +$dbh_pg->disconnect(); +$dbh_my->disconnect(); + + +sub read_conn { + my ($filename) = @_; + open (my $fh, '<', $filename) or die "cannot open $filename: $!"; + my $line = <$fh>; + chomp($line); + return split(/\s+/, $line); +}