[vhffs-dev] [1979] reworked postgresql dumps |
[ Thread Index |
Date Index
| More vhffs.org/vhffs-dev Archives
]
Revision: 1979
Author: gradator
Date: 2012-01-31 00:08:11 +0100 (Tue, 31 Jan 2012)
Log Message:
-----------
reworked postgresql dumps
Modified Paths:
--------------
trunk/vhffs-api/src/Vhffs/Robots/Pgsql.pm
trunk/vhffs-robots/src/pgsql_dump.pl
Modified: trunk/vhffs-api/src/Vhffs/Robots/Pgsql.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Pgsql.pm 2012-01-30 22:09:48 UTC (rev 1978)
+++ trunk/vhffs-api/src/Vhffs/Robots/Pgsql.pm 2012-01-30 23:08:11 UTC (rev 1979)
@@ -118,8 +118,71 @@
return $dbi;
}
+sub _dump {
+ my $pg = shift;
+ my $pgsqlconf = $pg->{main}->get_config->get_service('pgsql');
+ return undef unless defined $pgsqlconf;
+ # create the postgres password file
+ my ( $pgpassfile, $pgpasspath ) = Vhffs::Robots::tmpfile( $pg->{main} );
+ return undef unless defined $pgpassfile;
+ chmod( 0400, $pgpasspath );
+ print $pgpassfile '*:*:*:*:'.$pgsqlconf->{'password'}."\n";
+ close $pgpassfile;
+ my ( $tmpfile, $tmppath ) = Vhffs::Robots::tmpfile( $pg->{main} );
+ unless( defined $tmpfile ) {
+ unlink $pgpasspath;
+ return undef;
+ }
+ close $tmpfile;
+ my $ret;
+ $ENV{'PGPASSFILE'} = $pgpasspath;
+
+ my $childpid = open( my $output, '-|', $pgsqlconf->{'pgdump_path'}, '-U', $pgsqlconf->{'username'}, '-h', $pgsqlconf->{'host'}, '-b', '-Fc', '-Z0', '-f', $tmppath, $pg->get_dbname );
+ if ($childpid) {
+ # read process output
+ while(<$output>) {}
+
+ # wait for the child to finish
+ waitpid( $childpid, 0 );
+
+ # $? contains the return value, The high byte is the exit value of the process. The low 7 bits represent
+ # the number of the signal that killed the process, with the 8th bit indicating whether a core dump occurred.
+ # -- signal is 0 if no signal were sent to kill the process
+ # -- exit value is 0 if the process success
+ # -- core dump bit is 0 if no core dump were written to disk
+ # ---- so, $? contains 0 if everything went fine
+ $ret = $? >> 8 if $?;
+ }
+
+ delete $ENV{'PGPASSFILE'};
+ unlink $pgpasspath;
+
+ # Something went wrong, output is available in stderr
+ unless( $childpid and not defined $ret and -s $tmppath ) {
+ unlink $tmppath;
+ return undef;
+ }
+
+ return $tmppath;
+}
+
+sub dump_night {
+ my $pg = shift;
+
+ my $dir = $pg->get_group->get_dir;
+ return unless -d $dir;
+ my $file = $dir.'/'.$pg->get_dbname.'.pgsql.dump';
+
+ my $tmpfile = _dump($pg);
+ return undef unless defined $tmpfile;
+
+ chmod( 0440, $tmpfile );
+ chown( $pg->get_owner_uid , $pg->get_owner_gid , $tmpfile );
+ require File::Copy;
+ File::Copy::move( $tmpfile , $file );
+}
+
1;
-
Modified: trunk/vhffs-robots/src/pgsql_dump.pl
===================================================================
--- trunk/vhffs-robots/src/pgsql_dump.pl 2012-01-30 22:09:48 UTC (rev 1978)
+++ trunk/vhffs-robots/src/pgsql_dump.pl 2012-01-30 23:08:11 UTC (rev 1979)
@@ -36,6 +36,7 @@
use strict;
use utf8;
+
use lib '%VHFFS_LIB_DIR%';
use Vhffs::Main;
use Vhffs::User;
@@ -43,47 +44,21 @@
use Vhffs::Functions;
use Vhffs::Services::Pgsql;
use Vhffs::Robots;
+use Vhffs::Robots::Pgsql;
my $vhffs = init Vhffs::Main;
Vhffs::Robots::lock( $vhffs , 'dumppgsql' );
-my $pgsqlconfig = $vhffs->get_config->get_service('pgsql');
-die 'Error, pg_dump is not present on this system in path "'.$pgsqlconfig->{'pgdump_path'}.'"'."\n" unless( -x $pgsqlconfig->{'pgdump_path'} );
+my $pgsqlconf = $vhffs->get_config->get_service('pgsql');
+die 'Error, pg_dump is not present on this system in path "'.$pgsqlconf->{'pgdump_path'}.'"'."\n" unless -x $pgsqlconf->{'pgdump_path'};
-# create the postgres password file
-my $pgpassfile = '/tmp/pgpass_'.$$.'_';
-for (0 .. 9) { $pgpassfile .= ('a'..'z', 'A'..'Z', '0'..'9')[int rand 62] ; }
-
-umask 0377;
-open PGPASSFILE , '>'.$pgpassfile or die;
-print PGPASSFILE '*:*:*:*:'.$pgsqlconfig->{'password'}."\n";
-close PGPASSFILE;
-
-die 'Could not create pgpassfile in '.$pgpassfile."\n" unless -f $pgpassfile;
-
-umask 0337;
-
my $objs = Vhffs::Services::Pgsql::getall( $vhffs , Vhffs::Constants::ACTIVATED , undef , undef );
foreach my $obj ( @{$objs} )
{
- my $groupname = Vhffs::Group::get_name_by_gid( $vhffs , $obj->get_owner_gid );
- my $dbname = $obj->get_dbname;
-
- my $dir = Vhffs::Functions::hash_groupname( $groupname , $vhffs );
- next unless -d $dir;
-
- my $file = $dir.'/'.$dbname.'.pgsql.dump';
- my $cmd = 'PGPASSFILE='.$pgpassfile.' '.$pgsqlconfig->{'pgdump_path'}.' -U '.$pgsqlconfig->{'username'}.' -h '.$pgsqlconfig->{'host'}.' -b -Fc -Z6 -f '.$file.' '.$dbname;
-
- system( $cmd );
-
- #Change the chmod if the backup succeed
- chown( $obj->get_owner_uid , $obj->get_owner_gid , $file ) if -f $file;
+ Vhffs::Robots::Pgsql::dump_night( $obj );
}
-unlink $pgpassfile;
-
Vhffs::Robots::unlock( $vhffs , 'dumppgsql' );
exit 0;