[vhffs-dev] [1980] mysql and postgresql databases are now archived upon deletion

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


Revision: 1980
Author:   gradator
Date:     2012-01-31 00:44:49 +0100 (Tue, 31 Jan 2012)
Log Message:
-----------
mysql and postgresql databases are now archived upon deletion

Modified Paths:
--------------
    trunk/vhffs-api/src/Vhffs/Robots/Mysql.pm
    trunk/vhffs-api/src/Vhffs/Robots/Pgsql.pm

Modified: trunk/vhffs-api/src/Vhffs/Robots/Mysql.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Mysql.pm	2012-01-30 23:08:11 UTC (rev 1979)
+++ trunk/vhffs-api/src/Vhffs/Robots/Mysql.pm	2012-01-30 23:44:49 UTC (rev 1980)
@@ -56,13 +56,15 @@
 	return DBI->connect( 'DBI:mysql:dbname=mysql;host='.$dbhost , $dbuser , $dbpass );
 }
 
-
+# TODO - rework that ...
 sub delete_db
 {
 	my $main = shift;
 	my $db = shift;
 	return unless defined $db;
 
+	my $robotconf = $main->get_config->get_robots;
+
 	my $dbi = mysql_admin_db_connect( $main );
 	return unless $dbi;
 
@@ -81,10 +83,19 @@
 	$query = 'DELETE FROM `columns_priv` WHERE User = \''.$db->get_dbname.'\'';
 	$dbi->do( $query ) or $db->add_history('Error while grant access to the database');
 
+	$dbi->do( 'FLUSH PRIVILEGES' );
+
+	# Dump the database after access are removed (thus setting the database read-only) and just before dropping it
+	my $tmpfile = _dump( $db );
+	if( defined $tmpfile ) {
+		my $file = $robotconf->{'archive_deleted_path'}.'/'.time().'_'.$db->get_group->get_groupname.'_mysql_'.$db->get_dbname.'.dump';
+		require File::Copy;
+		File::Copy::move( $tmpfile , $file );
+	}
+
 	$query = 'DROP DATABASE `'.$db->get_dbname.'`';
 	$dbi->do( $query ) or $db->add_history('Error while deleting the database');
 
-	$dbi->do( 'FLUSH PRIVILEGES' );
 	$dbi->disconnect;
 
 	$db->add_history( 'Robots finished the destruction' );

Modified: trunk/vhffs-api/src/Vhffs/Robots/Pgsql.pm
===================================================================
--- trunk/vhffs-api/src/Vhffs/Robots/Pgsql.pm	2012-01-30 23:08:11 UTC (rev 1979)
+++ trunk/vhffs-api/src/Vhffs/Robots/Pgsql.pm	2012-01-30 23:44:49 UTC (rev 1980)
@@ -78,11 +78,27 @@
 	my $main = shift;
 	my $pg = shift;
 
+	my $pgsqlconfig = $main->get_config->get_service('pgsql');
+	my $robotconf = $main->get_config->get_robots;
+
 	my $db = pgsql_admin_db_connect( $main );
+	return unless $db;
 	
 	if( ( defined $pg ) && ( $pg->get_status == Vhffs::Constants::TO_DELETE )  )
 	{
 		$pg->add_history("Ok, robots will erase all data");
+
+		# Replace user password with another password (we don't care which one), to set the database more or less read-only
+		$db->do('ALTER USER '.$pg->get_dbusername.' WITH PASSWORD ?', undef, $pgsqlconfig->{'password'});
+
+		# Dump the database after access are removed (thus setting the database read-only) and just before dropping it
+		my $tmpfile = _dump( $pg );
+		if( defined $tmpfile ) {
+			my $file = $robotconf->{'archive_deleted_path'}.'/'.time().'_'.$pg->get_group->get_groupname.'_pgsql_'.$pg->get_dbname.'.dump';
+			require File::Copy;
+			File::Copy::move( $tmpfile , $file );
+		}
+
 		$db->do("DROP DATABASE ".$pg->get_dbname );
 		$db->do("DROP USER " . $pg->get_dbusername );	
 		$pg->add_history("All data have been erased");


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