[vhffs-dev] [1975] reworked mysql dump, added Vhffs::Robots::tmpfile(), new configuration entry robots::tmpdir

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


Revision: 1975
Author:   gradator
Date:     2012-01-30 01:16:56 +0100 (Mon, 30 Jan 2012)
Log Message:
-----------
reworked mysql dump, added Vhffs::Robots::tmpfile(), new configuration entry robots::tmpdir

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Group.pm
    trunk/vhffs-api/src/Vhffs/Robots/Mysql.pm
    trunk/vhffs-api/src/Vhffs/Robots.pm
    trunk/vhffs-backend/conf/vhffs.conf.dist.in
    trunk/vhffs-robots/src/mysql_dump.pl

Modified: trunk/vhffs-api/src/Vhffs/Group.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Group.pm	2012-01-29 15:03:14 UTC (rev 1974)
+++ trunk/vhffs-api/src/Vhffs/Group.pm	2012-01-30 00:16:56 UTC (rev 1975)
@@ -331,6 +331,10 @@
 	return $self->{'gid'};
 }
 
+sub get_dir {
+	my $self = shift;
+    	return $self->{'main'}->get_config->get_datadir.'/groups/'.substr($self->get_groupname, 0, 1).'/'.substr($self->get_groupname, 1, 1).'/'.$self->get_groupname;
+}
 
 sub get_name_by_gid
 {

Modified: trunk/vhffs-api/src/Vhffs/Robots/Mysql.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Mysql.pm	2012-01-29 15:03:14 UTC (rev 1974)
+++ trunk/vhffs-api/src/Vhffs/Robots/Mysql.pm	2012-01-30 00:16:56 UTC (rev 1975)
@@ -41,6 +41,7 @@
 use Vhffs::Services::Mysql;
 use Vhffs::Constants;
 use Vhffs::Functions;
+use Vhffs::Robots;
 
 
 sub mysql_admin_db_connect
@@ -147,4 +148,59 @@
 	$db->blank_password;
 }
 
+sub _dump {
+	my $db = shift;
+	my $mysqlconf = $db->{main}->get_config->get_service('mysql');
+	return undef unless defined $mysqlconf;
+
+	my ( $tmpfile, $tmppath ) = Vhffs::Robots::tmpfile( $db->{main} );
+	return undef unless defined $tmpfile;
+
+	my $ret;
+	my $childpid = open( my $output, '-|', $mysqlconf->{'mysqldump_path'}, '-c', '-R', '--hex-blob', '-u', $mysqlconf->{'username'}, '-h', $mysqlconf->{'host'}, '-p'.$mysqlconf->{'password'}, $db->get_dbname );
+	if ($childpid) {
+		# Ensure output is in binary mode
+		binmode($output);
+
+		# read process output, write output to $tmpfile
+		while(<$output>) { print $tmpfile $_; }
+
+		# 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 $?;
+	}
+
+	close( $tmpfile );
+
+	# 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 $db = shift;
+
+	my $dir = $db->get_group->get_dir;
+	return unless -d $dir;
+	my $file = $dir.'/'.$db->get_dbname.'.mysql.dump';
+
+	my $tmpfile = _dump($db);
+	return undef unless defined $tmpfile;
+
+	chmod( 0440, $tmpfile );
+	chown( $db->get_owner_uid , $db->get_owner_gid , $tmpfile );
+	rename( $tmpfile , $file );
+}
+
 1;

Modified: trunk/vhffs-api/src/Vhffs/Robots.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots.pm	2012-01-29 15:03:14 UTC (rev 1974)
+++ trunk/vhffs-api/src/Vhffs/Robots.pm	2012-01-30 00:16:56 UTC (rev 1975)
@@ -180,6 +180,24 @@
 	return not defined $ret and -f $tarfile ? 1 : undef;
 }
 
+# Return an opened tmpfile (with its path)
+sub tmpfile {
+	my $vhffs = shift;
+	my $robotconf = $vhffs->get_config->get_robots;
+	my $path = $robotconf->{'tmpdir'};
+	return undef unless defined $path;
+	$path .= '/';
+	for (0 .. 15) { $path .= ('a'..'z', 'A'..'Z', '0'..'9')[int rand 62]; }
+	$path .= '-'.$$;
+	open( my $file, '>', $path );
+	if( defined $file ) {
+		binmode($file);
+	} else {
+		undef $path;
+	}
+	return ( $file, $path );
+}
+
 1;
 
 __END__

Modified: trunk/vhffs-backend/conf/vhffs.conf.dist.in
===================================================================
--- trunk/vhffs-backend/conf/vhffs.conf.dist.in	2012-01-29 15:03:14 UTC (rev 1974)
+++ trunk/vhffs-backend/conf/vhffs.conf.dist.in	2012-01-30 00:16:56 UTC (rev 1975)
@@ -633,12 +633,19 @@
 	use_lock	=	yes
 	lockfile	=	/var/lock/vhffs
 
+	# Where to store temporary files, like mysql of postgresql dumps.
+	# This is better if this is on your main storage block, this way move of generated
+	# files to final directory will be costless.
+	# You have to create this directory yourself, root:root, mode 700
+	tmpdir		=	/data/tmp
+
 	# Should we archive (.tar.gz and/or .dump.gz) deleted services ?
 	# This way, user mistakes can be handled easily
 	archive_deleted		= yes
 
-	# Complete path to archive directory
+	# Complete path to archive directory.
 	# Archives will be put into $archive_path/$unixtimestamp_groupname_servicetype_servicename.(tar|dump).gz
+	# You have to create this directory yourself, root:root, mode 700
 	archive_deleted_path	= /data/archives
 
 	# How long should we keep archives of deleted objects,

Modified: trunk/vhffs-robots/src/mysql_dump.pl
===================================================================
--- trunk/vhffs-robots/src/mysql_dump.pl	2012-01-29 15:03:14 UTC (rev 1974)
+++ trunk/vhffs-robots/src/mysql_dump.pl	2012-01-30 00:16:56 UTC (rev 1975)
@@ -44,31 +44,19 @@
 use Vhffs::Functions;
 use Vhffs::Services::Mysql;
 use Vhffs::Robots;
+use Vhffs::Robots::Mysql;
 
 my $vhffs = init Vhffs::Main; 
 
 Vhffs::Robots::lock( $vhffs , 'dumpmysql' );
 
 my $mysqlconf = $vhffs->get_config->get_service('mysql');
-die 'Error, mysqldump is not present on this system in path "'.$mysqlconf->{'mysqldump_path'}.'"'."\n" unless( -x $mysqlconf->{'mysqldump_path'} );
+die 'Error, mysqldump is not present on this system in path "'.$mysqlconf->{'mysqldump_path'}.'"'."\n" unless -x $mysqlconf->{'mysqldump_path'};
 
-umask 0337;
-
 my $objs  = Vhffs::Services::Mysql::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.'.mysql.dump';
-	my $cmd = $mysqlconf->{'mysqldump_path'}.' -c -R --hex-blob -u '.$mysqlconf->{'username'}.' -h '.$mysqlconf->{'host'}.' -p'.$mysqlconf->{'password'}.' '.$dbname.' > '.$file;
-
-	system( $cmd );
-
-	chown( $obj->get_owner_uid , $obj->get_owner_gid , $file ) if -f $file;
+	Vhffs::Robots::Mysql::dump_night( $obj );
 }
 
 Vhffs::Robots::unlock( $vhffs , 'dumpmysql' );


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