From: Moritz Bunkus Date: Fri, 26 Apr 2013 08:19:12 +0000 (+0200) Subject: Shell-Script zum Verbinden mit Auth- oder User-DB via psql X-Git-Tag: release-3.1.0beta1~460 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=cdea8d0ed20c6585e06a9507a2aaf17dc1e41247;p=kivitendo-erp.git Shell-Script zum Verbinden mit Auth- oder User-DB via psql --- diff --git a/scripts/dbconnect.pl b/scripts/dbconnect.pl new file mode 100755 index 000000000..6eed2a0cf --- /dev/null +++ b/scripts/dbconnect.pl @@ -0,0 +1,59 @@ +#!/usr/bin/perl + +BEGIN { + use SL::System::Process; + my $exe_dir = SL::System::Process::exe_dir; + + unshift @INC, "${exe_dir}/modules/override"; # Use our own versions of various modules (e.g. YAML). + push @INC, "${exe_dir}/modules/fallback"; # Only use our own versions of modules if there's no system version. + unshift @INC, $exe_dir; +} + +use strict; +use warnings; + +use Data::Dumper; +use DBI; +use List::MoreUtils qw(any); +use SL::LxOfficeConf; + +our %lx_office_conf; +SL::LxOfficeConf->read; + +sub psql { + my ($title, %params) = @_; + print "Connecting to ${title} database '" . $params{db} . "' on " . $params{host} . ':' . $params{port} . " with PostgreSQL username " . $params{user} . "\n\n"; + print "If asked for the password use this: " . $params{password} . "\n\n"; + exec "psql", "-U", $params{user}, "-h", $params{host}, "-p", $params{port}, $params{db}; +} + +my $settings = $lx_office_conf{'authentication/database'}; +die "Missing configuration section 'authentication/database'" unless $settings; +die "Incomplete database settings" if any { !$settings->{$_} } qw (host db user); +$settings->{port} ||= 5432; + +psql("authentication", %{ $settings }) if !@ARGV; + +my $dbh = DBI->connect('dbi:Pg:dbname=' . $settings->{db} . ';host=' . $settings->{host} . ($settings->{port} ? ';port=' . $settings->{port} : ''), $settings->{user}, $settings->{password}) + or die "Database connection to authentication database failed: " . $DBI::errstr; + +my $user_id = $dbh->selectrow_array(qq|SELECT id FROM auth.user WHERE login = ?|, undef, $ARGV[0]) + or do { + $dbh->disconnect; + die "No such user in authentication database: " . $ARGV[0]; + }; + +my $href = $dbh->selectall_hashref(qq|SELECT cfg_key, cfg_value FROM auth.user_config WHERE user_id = ?|, 'cfg_key', undef, $user_id); +$dbh->disconnect; + +my %params = ( + host => $href->{dbhost}->{cfg_value}, + db => $href->{dbname}->{cfg_value}, + port => $href->{dbport}->{cfg_value} || 5432, + user => $href->{dbuser}->{cfg_value}, + password => $href->{dbpasswd}->{cfg_value}, +); + +die "Incomplete database settings for user " . $ARGV[0] if any { !$settings->{$_} } qw (host db user); + +psql($ARGV[0] . "'s", %params);