[vhffs-dev] [1463] fixed pgdump robot

[ Thread Index | Date Index | More vhffs.org/vhffs-dev Archives ]


Revision: 1463
Author:   gradator
Date:     2009-06-29 21:32:26 +0200 (Mon, 29 Jun 2009)

Log Message:
-----------
fixed pgdump robot

Modified Paths:
--------------
    trunk/vhffs-robots/src/pgsql_dump.pl


Modified: trunk/vhffs-robots/src/pgsql_dump.pl
===================================================================
--- trunk/vhffs-robots/src/pgsql_dump.pl	2009-06-27 08:05:25 UTC (rev 1462)
+++ trunk/vhffs-robots/src/pgsql_dump.pl	2009-06-29 19:32:26 UTC (rev 1463)
@@ -30,7 +30,7 @@
 # POSSIBILITY OF SUCH DAMAGE.
 
 ########################################################
-# This robot make dump for each MySQL database
+# This robot make dump for each PgSQL database
 # and put it on each group directory
 #
 
@@ -46,39 +46,44 @@
 
 my $vhffs = init Vhffs::Main; 
 
-Vhffs::Robots::lock( $vhffs , "dumppgsql" );
+Vhffs::Robots::lock( $vhffs , 'dumppgsql' );
 
-my $objs  = Vhffs::Services::Pgsql::getall( $vhffs , Vhffs::Constants::ACTIVATED , undef , undef );
 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 $obj;
-my $group;
-my $groupname;
+# create the postgres password file
+my $pgpassfile = '/tmp/pgpass_'.$$.'_';
+for (0 .. 9) { $pgpassfile .= ('a'..'z', 'A'..'Z', '0'..'9')[int rand 62] ; }
 
-die "Error, pg_dump is not present on this system in path \"$pgsqlconfig->{'pgdump_path'}\"\n" unless( -x $pgsqlconfig->{'pgdump_path'} );
+umask 0377;
+open PGPASSFILE , '>'.$pgpassfile or die;
+print PGPASSFILE '*:*:*:*:'.$pgsqlconfig->{'password'}."\n";
+close PGPASSFILE;
 
-foreach $obj ( @{$objs} )
+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} )
 {
-	$groupname = Vhffs::Group::get_name_by_gid( $vhffs , $obj->get_owner_gid );
+	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 );
-	if( -d $dir )
-	{
-		my $file = $dir."/".$dbname.".pgsql.dump";
-		my $cmd = "$pgsqlconfig->{'pgdump_path'} -U $pgsqlconfig->{'username'} -h $pgsqlconfig->{'host'} -b -Fc -Z6 -f $file $dbname";
-		system( $cmd );
+	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;
 
-		print $cmd."\n";
+	system( $cmd );
 
-		#Change the chmod if the backup succeed
-		if( -f $file )
-		{
-			chown( $obj->get_owner_uid , $obj->get_owner_gid , $file );
-			chmod( 0660 , $file );
-		}		
-	}
+	#Change the chmod if the backup succeed
+	chown( $obj->get_owner_uid , $obj->get_owner_gid , $file ) if -f $file;
 }
 
-Vhffs::Robots::unlock( $vhffs , "dumppgsql" );
+unlink $pgpassfile;
 
+Vhffs::Robots::unlock( $vhffs , 'dumppgsql' );
+
 exit 0;


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/